1

Closed

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

description

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;
        originalDocument.Save("90_degrees.pdf");
        originalDocument.Close();
        originalDocument.Dispose();
    
        // 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;
        rotatedDocument.Close();
        rotatedDocument.Dispose();
    
        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;

            }
            else
            {
                pageToRotate.Orientation = PageOrientation.Portrait;

            }

file attachments

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

comments

ThomasHoevel wrote Sep 15, 2016 at 1:17 PM

Fixed in the internal builds using the change documented in the forum:
http://forum.pdfsharp.net/viewtopic.php?p=9591#p9591

wrote Sep 15, 2016 at 1:17 PM

wrote Sep 15, 2016 at 1:18 PM