473,383 Members | 1,795 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,383 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 17787

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: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.