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

PdfSharp wrongly interprets orientation
https://forum.pdfsharp.net/viewtopic.php?f=2&t=610
Page 1 of 1

Author:  billy anachronism [ Thu Jan 15, 2009 4:07 am ]
Post subject:  PdfSharp wrongly interprets orientation

hi,

I've got a pdf that says its rotation is 90 but adobe reader opens it upright in portrait mode. Does this mean that the original content is at 270 ?

My problem is that PDFsharp opens it as Landscape (because it determines orientation by rotation). Then for some reason PDFsharp will save the document as portrait even if i tell it not to.

Thanks,

billy.

Author:  billy anachronism [ Thu Jan 15, 2009 11:16 pm ]
Post subject: 

When I remove the /Rotate flag from all the pages they do indeed display at 270.

It seems that PdfSharp cannot recognise rotated content. If i open the pdf and save it with PdfSharp, it saves the document as landscape (even though it isn't). I assume this is because it reads the page has a rotate of 90, so it rotates it on its side. This has the nasty consequence of cutting of the content on the page because the content isn't at 90.

Surely someone else has come across this issue. Does anyone have any suggestions?

Author:  RichardD [ Tue May 25, 2010 4:36 pm ]
Post subject:  Re: PdfSharp wrongly interprets orientation

I've just run into exactly the same problem with v1.31.1789.0. My PDF page is 1460x475mm, with a rotation of 90. If I open it with PDFSharp and save it without making any changes, the page size has changed to 475x1460mm with a rotation of 90, which cuts off the right-hand side of the document.

I think the problem is in PdfSharp.Pdf\PdfPage.cs, line 166:
Code:
// THHO: MediaBox is always in Portrait mode (see Height, Width)
/*if (this.orientation == PageOrientation.Portrait)*/
  MediaBox = new PdfRectangle(0, 0, size.Width, size.Height);
/*else
  MediaBox = new PdfRectangle(0, 0, size.Height, size.Width);*/


In v1.2, this was:
Code:
if (this.orientation == PageOrientation.Portrait)
  MediaBox = new PdfRectangle(0, 0, size.Width, size.Height);
else
  MediaBox = new PdfRectangle(0, 0, size.Height, size.Width);


The hacky workaround I've used is to call this function after loading any document:
Code:
private static void HackPageRotation([NotNull] PdfDocument document)
{
    for (int index = 0; index < document.PageCount; index++)
    {
        var page = document.Pages[index];
        if (PageOrientation.Landscape == page.Orientation)
        {
            var value = page.MediaBox;
            var size = new XSize(value.Height, value.Width);
            page.MediaBox = new PdfRectangle(value.Location, size);
        }
    }
}

Author:  Thomas Hoevel [ Wed May 26, 2010 7:58 am ]
Post subject:  Re: PdfSharp wrongly interprets orientation

RichardD wrote:
I think the problem is in PdfSharp.Pdf\PdfPage.cs, line 166

This change (my change :oops: ) fixes a problem reported in August 2009 - so this change cannot be the cause of a problem reported in January 2009.

I haven't investigated this issue yet, therefore I cannot say anything about it.

Author:  moulf [ Wed Jul 07, 2010 12:22 pm ]
Post subject:  Re: PdfSharp wrongly interprets orientation

Hello, i'm encountering an issue with a rotated landscape PDF. (seems very related to me)
However i had to put your hack before saving the PDF and not after loading or page calculations were wrong.

After digging into the source code i saw the problem in PDFPage.cs line 563:
Code:
      internal override void WriteObject(PdfWriter writer)
      {
         // HACK: temporarily flip media box if Landscape
         PdfRectangle mediaBox = MediaBox;
         // TODO: Take /Rotate into account
         if (orientation == PageOrientation.Landscape)
             MediaBox = new PdfRectangle(mediaBox.X1, mediaBox.Y1, mediaBox.Y2, mediaBox.X2);


Removing this "hack" seems to fix my problem.

I've seen a message (25-03-2010) on SourceForge saying this bug had been corrected in internal builds of PDFSharp.
Is there a way to get a snapshot of current development sources?

Thanks.

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