By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,028 Members | 1,258 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,028 IT Pros & Developers. It's quick & easy.

using iText to convert Tiff to PDF

P: n/a
Hi all !

I have a multi-page Tiff image file that I want to convert to PDF. To do so
I am using iText library. The conversion is working, but the code execution
is very different according to wich
iTextSharp.text.Image.getInstance(...) signature I am using :
- using code 1 below, the conversion is fast enough but the resulting PDF
file is too big (1 817ko sample Tiff file is converted in less than 30
seconds to a 2 764ko PDF file)
- using code 2 below, resulting PDF size is good but the conversion is too
slow (1 817ko sample Tiff file is converted in about 10 minutes to a 1 780ko
PDF file)

code 1:
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
PdfWriter writer = PdfWriter.getInstance(document, new
System.IO.FileStream(pdfFileName, System.IO.FileMode.Create,
System.IO.FileAccess.Write));
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(tiffFileName);
numberOfPages =
bitmap.GetFrameCount(System.Drawing.Imaging.FrameD imension.Page);
document.Open();
PdfContentByte cb = writer.DirectContent;
for(int page = 0; page < numberOfPages; page++)
{
bitmap.SelectActiveFrame(System.Drawing.Imaging.Fr ameDimension.Page,
page);
System.IO.MemoryStream stream = new System.IO.MemoryStream();
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png)
iTextSharp.text.Image img =
iTextSharp.text.Image.getInstance(stream.ToArray() );
stream.Close();
img.scalePercent(72f / bitmap.HorizontalResolution * 100);
img.setAbsolutePosition(0, 0);
cb.addImage(img);
document.newPage();
}
document.Close();

code 2:
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
PdfWriter writer = PdfWriter.getInstance(document, new
System.IO.FileStream(pdfFileName, System.IO.FileMode.Create,
System.IO.FileAccess.Write));
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(tiffFileName);
numberOfPages =
bitmap.GetFrameCount(System.Drawing.Imaging.FrameD imension.Page);
document.Open();
PdfContentByte cb = writer.DirectContent;
for(int page = 0; page < numberOfPages; page++)
{
bitmap.SelectActiveFrame(System.Drawing.Imaging.Fr ameDimension.Page,
page);
iTextSharp.text.Image img =
iTextSharp.text.Image.getInstance(bitmap); // This step is really slow
img.scalePercent(72f / bitmap.HorizontalResolution * 100);
img.setAbsolutePosition(0, 0);
cb.addImage(img);
document.newPage();
}
document.Close();

If I use solution 1 with a Jpeg format instead of Png, the conversion is a
bit slower and the PDF file size is 22 901ko. I also tried changing Jpeg
compression value with this code :
System.Drawing.Imaging.ImageCodecInfo myImageCodecInfo =
GetEncoderInfo("image/jpeg");
System.Drawing.Imaging.Encoder myEncoder =
System.Drawing.Imaging.Encoder.Quality;
System.Drawing.Imaging.EncoderParameter myEncoderParameter= new
System.Drawing.Imaging.EncoderParameter(myEncoder, 50L);
System.Drawing.Imaging.EncoderParameters myEncoderParameters = new
System.Drawing.Imaging.EncoderParameters(1);
myEncoderParameters.Param[0] = myEncoderParameter;
//....
bitmap.Save(stream, myImageCodecInfo, myEncoderParameters);
//....
but the resulting PDF file is still 17 900ko.

If I use
iTextSharp.text.Image img =
iTextSharp.text.Image.getInstance(bitmap.Width, bitmap.Height, true,
iTextSharp.text.Element.CCITTG4, 0, stream.ToArray());
instead of
iTextSharp.text.Image img =
iTextSharp.text.Image.getInstance(stream.ToArray() );
in code 1, then the PDF is only filled with white pages and I have an error
box when I open the PDF in Acrobat Reader.

I tried both http://itextsharp.sourceforge.net/ and
http://www.ujihara.jp/iTextdotNET/en/. I am having the same problem with
both libraries. Any suggestion to have a fast conversion with small PDF size
would be appreciated.

Thanks in advance.

Nicolas
Jul 21 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.