Custom page size in PDFsharp
Asked Answered
F

1

6

Using PDFsharp .NET library, I need to set the page size in the PDF document same as that of the images.

Example, image no. 1 measures 152px x 1775px. Image no. 2 measures 100px x 1582px

The resulting PDF should have varying page size inside.

Please help.

With the following code, I am able to set the size of the images, the problem is now setting the page size.

PdfDocument doc = new PdfDocument();
//doc.Pages.Add(new PdfPage());
PdfPage page = doc.AddPage();

XGraphics gfx = XGraphics.FromPdfPage(doc.Pages[0]);

XImage xImage = XImage.FromFile(source);
gfx.DrawImage(xImage, 0, 0, xImage.PixelWidth, xImage.PixelHeight);

doc.Save(destinaton);
doc.Close();
// 
Flanker answered 18/8, 2015 at 20:14 Comment(0)
C
7

PDF pages do not have pixels, it's a vector format.

This code should do the trick:

page.Width = xImage.PixelWidth;
page.Height = xImage.PixelHeight;

IMHO the user experience will be better if all pages have the same size and images are scaled for "best fit". That's my preference, your mileage may vary.

Cai answered 19/8, 2015 at 7:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.