tif 转pdf 通过iTextSharp

Hello,

I have a few questions with creating .tiff to pdf with iTextSharp. When I run tutorial from iTextSharp Chap0611, I got "A first chance exception of type 'System.Net.WebException' occurred in System.dll," but the file was created. My average one page .tiff file size is about 25KB, but the application takes about 5 to 10 mins to create one page. Is the file size slowing down the process? My pc is also relatively slow when I am running the application. I can't do anything but to wait for the application to finish.

Another thing is when I add the code to my application, I got "A first chance exception of type 'System.ArgumentException' occurred in System.dll." The file was created, but when I opened it, I got "Adobe Reader could not open 'test2.pdf' because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded)." My code runs forever, and it never finishes creating pdf so I had to kill it. I think I got the message because of that, but I don't know what is making the error.

Here is my code:

 

private void PDFIcon_Click(object sender, EventArgs e)

{

if (this.saveFileDialog1.ShowDialog() != DialogResult.OK)

{

return;

}

//user assigns pdf file name

String saveFileName = this.saveFileDialog1.FileName;

// creation of the document with a certain size and certain margins

Document document = new Document(PageSize.A4, 50, 50, 50, 50);

try

{

// creation of the different writers

PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(saveFileName, FileMode.Create));

System.Drawing.Bitmap bm = new System.Drawing.Bitmap("test.tif");

int total = bm.GetFrameCount(FrameDimension.Page);

// Which of the multiple images in the TIFF file do we want to load

// 0 refers to the first, 1 to the second and so on.

document.Open();

PdfContentByte cb = writer.DirectContent;

for (int k = 0; k < total; ++k)

{

bm.SelectActiveFrame(FrameDimension.Page, k);

iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(bm, null, true);

img.ScalePercent(72f / 200f * 100);

img.SetAbsolutePosition(0, 0);

cb.AddImage(img);

document.NewPage();

}

document.Close();

}

catch (Exception de)

{

Console.Error.WriteLine(de.Message);

Console.Error.WriteLine(de.StackTrace);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值