How to convert image size to A4 format with iTextSharp

Asked

Viewed 718 times

1

Please, I am generating several images in a single PDF and it is running correctly. What I can’t do is leave the A4 size on the images because they’re too big, like 80% of the standard size. How can I decrease to fit on the A4? The Code is below. Grateful

iTextSharp.text.Document Doc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 80, 80, 80, 80);
            //Salve o documento
            string PDFOutput = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "C:\\Users\\Usuario\\Desktop\\imagensParaPDF\\Output.pdf");
            PdfWriter writer = PdfWriter.GetInstance(Doc, new FileStream(PDFOutput, FileMode.Create, FileAccess.Write, FileShare.Read));
  • What the program does is read a directory where you have images . jpeg and convert them to a single PDF

1 answer

2

Use the ScaleToFit.

In my example I put an image in the format Bitmap (imagemConvertida), I set the size I wanted with the ScaleToFit and left her justified with the Element.ALIGN_JUSTIFIED.

Example:

iTextSharp.text.Image imagem = iTextSharp.text.Image.GetInstance(imagemConvertida, System.Drawing.Imaging.ImageFormat.Png);
imagem.ScaleToFit(600f, 300f);
imagem.Alignment = Element.ALIGN_JUSTIFIED;
document.Add(imagem);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.