PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Tue Apr 20, 2021 5:50 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Thu Dec 10, 2009 5:29 am 
Offline

Joined: Thu Dec 10, 2009 5:01 am
Posts: 3
I'm writing an application that adds a watermark to PDF pages.

I'm using a test document that has the following pages:
A4 portrait
A3 landscape
A4 landscape
A3 portrait
The pages register correctly once loaded, in that the code objects have the correct sizes and orientation in memory .

However, once saved to a new file, all the pages revert to portrait orientation without affecting the content (i.e. the content is truncated width-wise, and there is a blank section at the bottom of each page).

This behaviour happens even if the pages are not altered in any way.

Steps to replicate the problem:

Create a PDF with any number of pages or sizes, but with at least one landscape page, call it 'testFile.pdf'

    Create a new VS C# project, Console application
    Add a Reference to the PDFSharp DLL
    Add the following code to the void Main:
    Code:
    PdfSharp.Pdf.PdfDocument PDFDoc = PdfSharp.Pdf.IO.PdfReader.Open("testFile.pdf");
    PDFDoc.Save("resultFile.PDF");

    Set the working directory and ensure the testFile.pdf is in that directory
    Compile and run.

the output file will contain pages of the right size, but all portrait orientation.
This has been tested on a PDF with a single A4 landscape page, and the output was still portrait.

I notice that PdfPage objects are created by default with portrait orientation. I'm not familiar enough with the code base to work out where this would get overriden by the actual size and orientation.


Top
 Profile  
Reply with quote  
PostPosted: Thu Dec 10, 2009 2:35 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3047
Location: Cologne, Germany
I cannot replicate this problem.

I used the output of the PageSizes sample and ran the Watermarks sample with that - and everything looked fine.
I was using PDFsharp 1.30 GDI+ build.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Fri Dec 11, 2009 3:59 am 
Offline

Joined: Thu Dec 10, 2009 5:01 am
Posts: 3
Interesting...I took the results of the PageSizes sample and ran that through the code sample above, and the orientations were fine (i.e. couldn't replicate problem). Also using 1.3 and GDI+ version.

I'm assuming therefore it's some aspect of the PDF's that's causing this?

The original PDF was created with Word 2007 printing to a PDF printer, the original displays fine with the correct orientation in both Acrobat 4 and 8, and the orientation loss appears in both Acrobat 4 and 8.
Edit: same issue occurs with PDF's created from Word 2007 using the SaveAsPDForXPS extension (
http://www.microsoft.com/downloads/details.aspx?FamilyID=4d951911-3e7e-4ae6-b059-a2e79ed87041&displaylang=en)...create a document in landscape, save it as PDF, open it in PDFsharp and save it again, and the orientation is portrait.

Any clues as to why this would happen?


Top
 Profile  
Reply with quote  
PostPosted: Fri Dec 11, 2009 7:10 am 
Offline

Joined: Thu Dec 10, 2009 5:01 am
Posts: 3
Further playing around has revealed a workaround.

Create a new blank document and import the existing pages to the new document, and save the new document...orientations are preserved.

To replicate:

    Create a PDF with any number of pages or sizes, but with at least one landscape page, call it 'testFile.pdf'
    Create a new VS C# project, Console application
    Add a Reference to the PDFSharp DLL
    Add the following code to the void Main:
    Code:
    PdfSharp.Pdf.PdfDocument PDFDoc = PdfSharp.Pdf.IO.PdfReader.Open("testFile.pdf", PdfDocumentOpenMode.Import );
    PdfSharp.Pdf.PdfDocument PDFNewDoc = new PdfSharp.Pdf.PdfDocument();
    for(int Pg=0;Pg<PDFDoc.Pages.Count;Pg++){
        PDFNewDoc.AddPage(PDFDoc.Pages[Pg]);
    }
    PDFNewDoc.Save("resultFile.PDF");

    Set the working directory and ensure the testFile.pdf is in that directory
    Compile and run.

Voila...page orientations are preserved.

Still no idea what's causing it though ;)


Top
 Profile  
Reply with quote  
PostPosted: Wed Aug 24, 2011 2:19 pm 
Offline

Joined: Wed Aug 24, 2011 2:15 pm
Posts: 3
GrumpyOldFart,

Your post is a little old but still very valuable. Thanks for posting it. It just saved me hours and hours of trying to track down the same issue.


Top
 Profile  
Reply with quote  
PostPosted: Mon Dec 16, 2013 9:00 pm 
Offline

Joined: Thu Jul 25, 2013 8:41 pm
Posts: 2
GrumpyOldFart,

Thanks for posting the solution.
This saved me A LOT OF hours and a big headaches.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 17 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group