This project has moved and is read-only. For the latest updates, please go here.


Rotate to landscape (90) then save reverses width and height.


After rotating a PDF to 90 degrees then saving the file the width and height are flipped incorrectly and the image isn't properly rotated.
  1. Using the attached file create a test case with the following code.
    public void Rotate_With90DegreeRotation_WillRevertToPortraitAfterSaving()
        var originalDocument = PdfReader.Open("0_degrees.pdf");
        var page = originalDocument.Pages[0];
        page.Rotate = 90;
        page.Orientation = PageOrientation.Landscape;
        // read the file again to check orienation
        var rotatedDocument = PdfReader.Open("90_degrees.pdf");
        var rotatedPage = rotatedDocument.Pages[0];
        var rotatedHeight = rotatedPage.Height;
        var rotatedWidth = rotatedPage.Width;
        var rotatedOrientation = rotatedPage.Orientation;
        Assert.IsTrue(rotatedWidth > rotatedHeight); // test fails here
  2. See result of test.
It appears that the save is doing something with the Media Box dimensions that may be causing this issue. I've implemented a work around in my code to make all 90 and 270 degree rotations set to a "Portrait" orientation before saving. This seem to fix the issue, but definitely a HACK.

Here's my work around code:c
            page.Rotate = 90;
            if (pageToRotate.Rotate == 90)
                // HACK to fix bug with PDF Sharp; setting Orientation will flip the Height and Width, but loading the image again will be incorrect
                pageToRotate.Height = originalWidth;
                pageToRotate.Width = originalHeight;
                // END HACK

                pageToRotate.Orientation = PageOrientation.Landscape;

                pageToRotate.Orientation = PageOrientation.Portrait;


file attachments

Closed Sep 15, 2016 at 2:17 PM by ThomasHoevel


ThomasHoevel wrote Sep 15, 2016 at 2:17 PM

Fixed in the internal builds using the change documented in the forum:

wrote Sep 15, 2016 at 2:17 PM

wrote Sep 15, 2016 at 2:18 PM