Skip to content

Height and Width Swapped in PDF Sharp 6.2.0 and Above on Some PDFs. #333

@hcornelisonJHA

Description

@hcornelisonJHA

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.

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 is attached here.

input.pdf

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions