ITextSharp displays low quality image in PDF

I am using iTextSharp to print a PDF document. Everything is going fine until I print the company logo.

At first, I noticed that the logo was of poor quality, but after testing with several images, I understand that iTextSharp did not render it well. The test I did to say was to print the PDF using my code, and then edit the document using Acrobat 8.0, and I drew an image. Then they printed two documents and saw a noticeable difference. My question is, if anyone knows if this could be due to a zoom problem, when I cannot tell iTextSharp how it should display the image or is a limitation of iTextSharp.

The code for rendering the image is as follows:

Dim para As Paragraph = New Paragraph para.Alignment = Image.RIGHT_ALIGN para.Add(text) Dim imageFile As String = String.Format("{0}{1}", GetAppSetting("UploadDirectory"), myCompany.LogoUrl) Dim thisImage As Image = Image.GetInstance(imageFile) thisImage.Alignment = Image.LEFT_ALIGN para.Add(thisImage) 

Printed Images: alt text http://img710.imageshack.us/img710/4199/sshot2y.png

Image printed directly using iTextSharp

alt text http://img231.imageshack.us/img231/3610/sshot1z.png

Image edited and printed using Acrobat 8

EDIT: These logo images are downloaded from the download page, where the user downloads everything that the logo wants, and I scaled this image using the following code:

  Dim graph As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(newImage) graph.CompositingMode = Drawing.Drawing2D.CompositingMode.SourceOver graph.CompositingQuality = Drawing.Drawing2D.CompositingQuality.HighQuality graph.InterpolationMode = Drawing.Drawing2D.InterpolationMode.Bicubic graph.SmoothingMode = Drawing.Drawing2D.SmoothingMode.HighQuality graph.PixelOffsetMode = Drawing.Drawing2D.PixelOffsetMode.HighQuality graph.DrawImage(newImage, 0, 0, newWidth, newHeight) graph.Dispose() graph = Nothing 

This led to a loss of information from the original image, so when printing in pdf format, this loss of information was very noticeable, because iTextSharp somehow drew more than it was, regardless of how I scale. Therefore, I tried to save the image since it was originally, preventing the user from loading images larger than 200 KB and resizing the image so that I could maintain aspect ratio and use this resizing with the iTextSharp Image object before printing it. This solved my problem with a low quality printed image for these larger images, but as a result of the PDF document having a page break or just not fitting the page, it is strange because the image looks good in size but it behaves as if it is larger, This is a screen capture of a new image: alt text http://img38.imageshack.us/img38/5756/sshot3tc.png

EDIT 2:

When checking the iTextSharp image that is sent for printing, when scaling with ScaleAbsolute it does not show any changes, so the page is broken. But it is shown correctly how the image was successfully scaled, but there was no background β€œpaper”. The code used is as follows:

  Dim imageFile As String = String.Format("{0}{1}", GetAppSetting("UploadDirectory"), myCompany.LogoUrl) 

Dim thisImage As Image = Image.GetInstance (imageFile) thisImage.Alignment = Image.LEFT_ALIGN

  Dim newWidth As Integer = myCompany.LogoWidth Dim newHeight As Integer = myCompany.LogoHeight ResizeImageToMaxValues(newWidth, newHeight) thisImage.ScaleAbsolute(newWidth, newHeight) para.Add(thisImage) pdf.PdfDocument.Add(para) 

The ResizeImage () method changes the size and height in relation to the format and saves the maximum width and maximum height limits.

Please let me know if I need to provide more information. Thanks

+7
itextsharp
source share
8 answers

I also have extensive experience with iTextSharp rendering very clear and crisp images. I tried to add the image directly to the document, and first add it to the paragraph. Both give very clear results.

 Dim document As Document = New Document(pSize, 20, 20, 20, 20) PdfWriter.GetInstance(document, New FileStream(myPath & "Test.pdf", FileMode.Create)) document.Open() Dim png As Image = Image.GetInstance(myPath & "myImageFile.png") document.Add(png) Dim pgr As New Paragraph pgr.Add(png) document.Add(pgr) document.Close() 

I usually use .png images, but I had the same success with jpeg, gif, etc.

Are you sure that when extracting an image in iTextSharp, is this the exact image you get when you are in Acrobat? I ask because it is not clear what is going on in your code:

 Dim imageFile As String=String.Format(..... 

EDIT

In addition, to make sure that the image is displayed at the expected size, save your images with a resolution of 72dpi. 72 dpi is what iTextSharp uses for everything (images, margins, etc.). Thus, a 100px x 100px image will be displayed as 100x100 on your pdf document. Then you don’t have to worry about scaling or recalibration. Each time you scale an image (up or down), you run the risk of introducing anti-aliasing artifacts (blur).

+5
source share

Besides the problem with the printer (see above), the final solution was a 3 X solution from your friend.

So, to rephrase, if you want the image to be 100 X 100 in a PDF file, make sure your image is 300 pixels x 300 pixels or more.

I am also trying to use 300 dpi images and I have not tested lower quality images.

This is my image upload code:

 try { string uri = Environment.CurrentDirectory + "/" + "pdfwithimage_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf"; string strImgJpg = Environment.CurrentDirectory + "/HeaderImage.jpg"; Image imgJpg = Image.GetInstance(System.Drawing.Image.FromFile(strImgJpg), new BaseColor(System.Drawing.Color.White)); using (Document pdf = new Document(PageSize.A4, 20, 20, 20, 20)) { if (pdf == null) { throw new NullReferenceException("PDF has not been instanciated"); } if (File.Exists(uri)) { File.Delete(uri); } using (PdfWriter pdfwriter = PdfWriter.GetInstance(pdf, new FileStream(uri, FileMode.Create))) { pdf.Open(); imgJpg.SetDpi(300, 300); imgJpg.ScaleToFit(100f, 100f); pdf.Add(imgJpg); pdf.Close(); } } } catch (Exception ex) { Console.WriteLine(ex.ToString()); Console.ReadLine(); } 
+6
source share

To make the rendering clear, try making the image 3 times the original image, then you can use ScalePercent or ScaleToFit to resize to a specific size.

Example:

 Image logo = Image.GetInstance(pdfReportData.CompanyLogoPath); logo.ScaleToFit(175f, 108f); document.Add(logo); 
+2
source share

This is strange. I get super-clear images in my pdf files. There are several differences between what I do and what you have. For example, I create an image as follows:

 Image instance = Image.GetInstance(System.Drawing.Image.FromFile(pathToImage), Color.WHITE); 

Also, since my image is too large to fit, I call:

 instance.ScalePercent(90f); 

Another difference is that I am adding the image directly to the Document , and not to Paragraph , although I doubt it.

Edit

And finally, my jpeg images.

Hope this helps.

+1
source share

The trick is to use large images and reduce them. If the scale is not set and the image has a natural size, it will be of poor quality. If the scale is set and the image is smaller than its natural size, the quality will be better.

Example of scaling up to 7 percent size (high quality):

 var logo = Image.GetInstance(RImages.logo_600_icon, BaseColor.WHITE); logo.ScalePercent(7); var cell = new PdfPCell(logo); cell.Border = 0; table.AddCell(cell); 
+1
source share

On my side, it has become a printer setup!

Changing the graphics to Raster (from Vector) made the images as sharp as they should have been, but they slowed the system down a lot.

The second solution (thanks to FD) was to change the graphics back to Vector, but to set "Image Processing" to Best.

We use RICOH Aficio MP C2050 PCL 6.

0
source share

Use large png images and then resize iTextSharp.

 logoImage.ScalePercent(24f); 
0
source share

I ran into the same problem. I was able to fix this by disabling compression. My PDFs that I generate are not very large, so the file size was not too expensive.

 var writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, stream); writer.SetPdfVersion(iTextSharp.text.pdf.PdfWriter.PDF_VERSION_1_7); writer.CompressionLevel = PdfStream.NO_COMPRESSION; 
0
source share

All Articles