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);
}
}