ITextSharp Image Scaling for Full Page

I am trying to scale the image so that it is full page in the PDF document. I am creating a document using iTextSharp. The image has the correct aspect ratio for the page, but I prefer the image to distort rather than fill the entire available area.

I currently have:

Dim Document As New Document(PageSize, 0, 0, 0, 0) ... Dim ContentImage = '''Method call to get image' Dim Content = iTextSharp.text.Image.GetInstance(ContentImage, New BackgroundColor) Content.SetAbsolutePosition(0, 0) Content.ScaleToFit(Document.PageSize.Width, Document.PageSize.Height) Document.Add(Content) 

Unfortunately, this does not take printer fields into account ...

I need the image to fit the printable area (as much as possible in PDF format)

Thanks in advance

+6
pdf itext
source share
3 answers

If you intend to do this empirically, then print the page with your code, as it scales to the page border, so that the image is painted black in the first half of an inch from the edge, if it can go to the edge, Measure the distance from each edge to black in inches and divide each at 72.0.

Name them: lm, rm, tm, bm (leftmost lower right margins.

 Dim pageWidth = document.PageSize.Width - (lm + rm); Dim pageHeight = document.PageSize.Height - (bm + tm); Content.SetAbsolutePosition(lm, bm); Content.ScaleToFit(pageWidth, pageHeight); Document.Add(Content) 
+8
source share

The print area depends on the printer, PDF files do not know anything about it. A PDF page can contain content from field to field. You can print a PDF file with the โ€œFit to Printerโ€ option to print the entire PDF page that is scaled to the printable area on the printer.

+3
source share

You can scale the image to fit the PDF page using the following code snippet.

Vb

 Dim img As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(System.Drawing.Image.FromStream(resourceStream), System.Drawing.Imaging.ImageFormat.Png) img.SetAbsolutePosition(0, 0) 'set the position to bottom left corner of pdf img.ScaleAbsolute(iTextSharp.text.PageSize.A7.Width, iTextSharp.text.PageSize.A7.Height) 'set the height and width of image to PDF page size 

FROM#

 iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(System.Drawing.Image.FromStream(resourceStream, System.Drawing.Imaging.ImageFormat.Png); img.SetAbsolutePosition(0, 0); // set the position to bottom left corner of pdf img.ScaleAbsolute(iTextSharp.text.PageSize.A7.Width,iTextSharp.text.PageSize.A7.Height); // set the height and width of image to PDF page size 

If you want the full code (C #), you can also link to the following link. Full code adds an image to all pages of an existing PDF.

fooobar.com/questions/881297 / ...

+1
source share

All Articles