0

I am trying to add image to PDF document. Image size is 145x53px.

But image looks bigger in PDF document.

How to fix the issue? I need that picture will be displayed as normal size.

Code:

FileStream fs = new FileStream( "C:\\Temp\\First PDF document.pdf", FileMode.Create);
Document document = new Document(PageSize.A4, 60, 25, 30, 30);  
PdfWriter writer = PdfWriter.GetInstance(document, fs);

document.Open();
        
// Add an image
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance("C:\\Temp\\Img\\test.png");
document.Add(img);
document.Add(new Paragraph("Original Width: " + img.Width.ToString()));
document.Add(new Paragraph("Original Height " + img.Height.ToString()));
document.Add(new Paragraph("Scaled Width: " + img.ScaledWidth.ToString()));
document.Add(new Paragraph("Scaled Height " + img.ScaledHeight.ToString()));

document.Close();
writer.Close();

PNG:

enter image description here

PDF:

enter image description here

Raf
  • 203
  • 2
  • 11
  • Hi, does [this](https://stackoverflow.com/questions/4325151/adding-an-image-to-a-pdf-using-itextsharp-and-scale-it-properly) help? On of the scaling method calls seems to be needed, to scale your image relative to your documents size. – nilsK Nov 25 '20 at 09:15
  • Hi @nilsK I tried to scale it but no luck. Tried img.ScaleAbsolute(145f, 53f); and img.ScaleToFit(145f, 53f); – Raf Nov 25 '20 at 09:22
  • PDF expects you to use dpi not pixels. – TaW Nov 25 '20 at 09:25
  • Your question doesn't make sense. PDF is vectorized, it's not bitmapped. I could tell you to just zoom out until it looks right. – Palle Due Nov 25 '20 at 09:30
  • How to set size in dpi? @TaW – Raf Nov 25 '20 at 09:35
  • img.SetResolution(...) – TaW Nov 25 '20 at 09:39
  • The units used by iText usually are points, pt, 1/72in; px on the other hand usually are seen as 1/96in. You have to scale accordingly. – mkl Nov 25 '20 at 10:38
  • When I do scaling, for examlple - img.ScalePercent(75f) - Quality of on image degraded. How to prevent it @mkl – Raf Nov 25 '20 at 10:58
  • *"When I do scaling, for examlple - img.ScalePercent(75f) - Quality of on image degraded. How to prevent it"* - That should not happen because iText does _not_ scale down the bitmap itself, it merely arranges the current transformation matrix at the time of image drawing so that the PDF viewer knows the desired size. Please share a PDF illustrating the quality loss. – mkl Nov 25 '20 at 11:25

0 Answers0