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.getInsta nce(...) 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(PageSi ze.A4, 50, 50, 50, 50);
PdfWriter writer = PdfWriter.getIn stance(document , new
System.IO.FileS tream(pdfFileNa me, System.IO.FileM ode.Create,
System.IO.FileA ccess.Write));
System.Drawing. Bitmap bitmap = new System.Drawing. Bitmap(tiffFile Name);
numberOfPages =
bitmap.GetFrame Count(System.Dr awing.Imaging.F rameDimension.P age);
document.Open() ;
PdfContentByte cb = writer.DirectCo ntent;
for(int page = 0; page < numberOfPages; page++)
{
bitmap.SelectAc tiveFrame(Syste m.Drawing.Imagi ng.FrameDimensi on.Page,
page);
System.IO.Memor yStream stream = new System.IO.Memor yStream();
bitmap.Save(str eam, System.Drawing. Imaging.ImageFo rmat.Png)
iTextSharp.text .Image img =
iTextSharp.text .Image.getInsta nce(stream.ToAr ray());
stream.Close();
img.scalePercen t(72f / bitmap.Horizont alResolution * 100);
img.setAbsolute Position(0, 0);
cb.addImage(img );
document.newPag e();
}
document.Close( );
code 2:
Document document = new Document(PageSi ze.A4, 50, 50, 50, 50);
PdfWriter writer = PdfWriter.getIn stance(document , new
System.IO.FileS tream(pdfFileNa me, System.IO.FileM ode.Create,
System.IO.FileA ccess.Write));
System.Drawing. Bitmap bitmap = new System.Drawing. Bitmap(tiffFile Name);
numberOfPages =
bitmap.GetFrame Count(System.Dr awing.Imaging.F rameDimension.P age);
document.Open() ;
PdfContentByte cb = writer.DirectCo ntent;
for(int page = 0; page < numberOfPages; page++)
{
bitmap.SelectAc tiveFrame(Syste m.Drawing.Imagi ng.FrameDimensi on.Page,
page);
iTextSharp.text .Image img =
iTextSharp.text .Image.getInsta nce(bitmap); // This step is really slow
img.scalePercen t(72f / bitmap.Horizont alResolution * 100);
img.setAbsolute Position(0, 0);
cb.addImage(img );
document.newPag e();
}
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.ImageCo decInfo myImageCodecInf o =
GetEncoderInfo( "image/jpeg");
System.Drawing. Imaging.Encoder myEncoder =
System.Drawing. Imaging.Encoder .Quality;
System.Drawing. Imaging.Encoder Parameter myEncoderParame ter= new
System.Drawing. Imaging.Encoder Parameter(myEnc oder, 50L);
System.Drawing. Imaging.Encoder Parameters myEncoderParame ters = new
System.Drawing. Imaging.Encoder Parameters(1);
myEncoderParame ters.Param[0] = myEncoderParame ter;
//....
bitmap.Save(str eam, myImageCodecInf o, myEncoderParame ters);
//....
but the resulting PDF file is still 17 900ko.
If I use
iTextSharp.text .Image img =
iTextSharp.text .Image.getInsta nce(bitmap.Widt h, bitmap.Height, true,
iTextSharp.text .Element.CCITTG 4, 0, stream.ToArray( ));
instead of
iTextSharp.text .Image img =
iTextSharp.text .Image.getInsta nce(stream.ToAr ray());
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