PDFsharp & MigraDoc Forum
https://forum.pdfsharp.net/

Height and Width Swapped in PDF Sharp 6.2.0 and Above
https://forum.pdfsharp.net/viewtopic.php?f=3&t=4944
Page 1 of 1

Author:  heathc [ Sat Feb 14, 2026 2:46 pm ]
Post subject:  Height and Width Swapped in PDF Sharp 6.2.0 and Above

I am seeing issues starting with PDfSharp 6.2.0 and above, where the Height and Width can be swapped for some unknown reason.

When I run the code snippet below targeting PdfSharp 6.1.1, hitting the same PDF Input File, I get this result:
Image

Retargeting the application to use PdfSharp 6.2.4, I get this result, again, hitting the exact same input pdf file:
Image

See the code below.
Code:
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using System;
using System.IO;

string inputFilePath = "C:\\temp\\input.pdf";
string outputFilePath = "C:\\temp\\output-6.2.pdf";

if (File.Exists(outputFilePath))
   File.Delete(outputFilePath);

using var pdfDocument = new PdfDocument();
using var pdfForm = XPdfForm.FromFile(inputFilePath);

pdfForm.PageNumber = 1;

Console.WriteLine($"{inputFilePath} - XPdfForm.PointHeight :  {pdfForm.PointHeight}");
Console.WriteLine($"{inputFilePath} - XPdfForm.PixelHeight :  {pdfForm.PixelHeight}");
Console.WriteLine($"{inputFilePath} - XPdfForm.PointWidth  :  {pdfForm.PointWidth}");
Console.WriteLine($"{inputFilePath} - XPdfForm.PixelWidth  :  {pdfForm.PixelWidth}");

var sourcePageSizeInPoints = new XSize(pdfForm.PointWidth, pdfForm.PointHeight);
var pdfPage = pdfDocument.AddPage();

pdfPage.Width = XUnit.FromPoint(sourcePageSizeInPoints.Width);
pdfPage.Height = XUnit.FromPoint(sourcePageSizeInPoints.Height);

var pageRect = new XRect(0, 0, sourcePageSizeInPoints.Width, sourcePageSizeInPoints.Height);

using var gfx = XGraphics.FromPdfPage(pdfPage);

gfx.Save();
gfx.DrawImage(pdfForm, pageRect);
gfx.Restore();

pdfDocument.Save(outputFilePath);


This is causing my application some issue where the PDF Generation of this data is wrong, making some items output in Landscape mode when they should be Portrait, and vice versa.

A sample PDF that produces these inconsistent results between versions of PDF Sharp can be found here:
https://github.com/user-attachments/files/24964546/input.pdf

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/