473,434 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

using iText to convert Tiff to PDF

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
0 17815

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dominic | last post by:
Hi guys, In .NET, how can I convert a HTML file to TIFF efficiently? One possible way is that I can first use word automatation to load the HTML up and then print it to TIFF. Is that right?...
0
by: MC | last post by:
Hi, I'm developing an VB.NET app that converts TIFFs to PDFs using Office Imaging 2003 and Adobe Distiller 6.0 in the follwing way: 1. Using automation, I open the TIFF file with Office...
4
by: Pushp | last post by:
Hello friends, Can any body tell me the sample code to convert a Tiff image to a Gif or Jpeg format. The Resolution of picture is a big concern keeping in mind. Hope to get the response...
1
by: Prasad More | last post by:
Hello, I am trying to write a text on Multi-page TIFF image using C# and .NET GDI+. I have written following code to do this. When I execute this code I get "Invalid Parameter User. at...
3
by: David A. Osborn | last post by:
I currently have a class that has an enumeration of graphics extensions that it supports ie: Public Enum GraphicFormats JPG JPEG BMP TIFF End Enum
0
by: Nicolas Guilhot | last post by:
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...
0
by: gcorgnet | last post by:
Hi all, I have a set of Multipage Tiff documents that are to be sent to a web browser. So, I want to convert them to Gif (or Png) before sending them to the client. Those Tiff docs are bitonal...
0
by: shlim | last post by:
Currently I'm using VB.Net to perform a http/https multipart form post to a servlet. I'm able to perform the post using HttpWebrequest via GetRequestStream(). However, the servlet returned me with...
1
by: sainathparuchuri | last post by:
As a project requirement we need to convert word documents to tiff images. We have been using Microsoft Office Document Image Writer (MODI) for this puspose and doing absolutely well. But now this...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.