Sir Edward
Sir Edward

Reputation: 71

Custom page size in PDFsharp

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();
// 

Upvotes: 6

Views: 9951

Answers (1)

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.

Upvotes: 8

Related Questions