3

I am using Itextsharp to put some images into PDF. Some of these images are large (3600h by 2500w) and some bit small (1000h by 700w). My PDF page is 842H by 737H.

Some of these large images will not fit into potrait so i am flipping images where the width > height.

 img.RotationDegrees = 90f; 

Now my problem is how do i scale the images so they all scale and fit into the page perfectly?

I tried using

 var scalePercent=  (PDFPage.Width / img.Width) * 100;

 img.ScalePercent(scalePercent);

but this didnt work for most images, specially small images.

2 Answers 2

5

I know this response is mega late, but figured this may help others as I had the same issue. I did this to flip then resize:

Document pdfDoc = new Document(PageSize.LETTER, 10f, 10f, 10f, 0f);
pdfDoc.SetPageSize(iTextSharp.text.PageSize.LETTER.Rotate());
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(image);
img.SetAbsolutePosition(0, 0);
img.ScaleAbsoluteHeight(pdfDoc.PageSize.Height);
img.ScaleAbsoluteWidth(pdfDoc.PageSize.Width);

However note that I am setting a pageSize on the document to letter, so additional changes may need to be made if generating PDFs in different sizes.

3

You can use your code its works fine but you need one extra filter like this

   var scalePercent = (((doc.PageSize.Width / image.Width) * 100) - 4);
   img.ScalePercent(scalePercent);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.