tags:

views:

1105

answers:

2

I have written a piece of code which uses the PdfSharp library. The instance of PdfSharp.Pdf.PdfDocument created saves to disk as expected. The right content is displayed, but onto the wrong page settings.

The default page settings for PdfSharp are:

  1. PdfSharp.PageSizes.A4
  2. PdfSharp.PageOrientation.Portrait

My problem is that these settings seem to override the required settings.

I create the instance of PdfDocument class and adds a new instance of PdfPage class to its Pages collection property. Then, I setup the page like this:

  1. PdfDocument.Pages[0].Size = PdfSharp.PageSizes.Letter
  2. PdfDocument.Pages[0].Orientation = PdfSharp.PageOrientation.Landscape
  3. I draw the strings (this works fine)
  4. I save the document to disk (this works fine)
  5. Process.Start(myPdfFilename) - then Acrobat Reader opens with my document.
  6. The page settings are A4 - Portrait...

I'm quite confused. I know of an option within Acrobat Reader that allows the user to change the page orientation without changing the text direction. No matter whether I check this option or not, still the wrong settings keep going.

Anyone has an idea? Thanks!

+1  A: 

For some strange reason, PdfSharp seems not to behave the same with both the following:

Example 1 - It doesn't seem to associate the instance of PdfPage class to the PdfDocument even though the page settings are correct while calling and after having called the PdfDocument.Save() method.

var pdfDoc = new PdfDocument();
var pdfPage = pdfDoc.AddPage();
pdfPage.Orientation = PdfSharp.PageOrientation.Landscape;
pdfPage.Size = PdfSharp.PageSize.Letter;
pdfPage.Rotate = 0;
pdfDoc.Save(filename);

Example 2 - The same here...

var pdfDoc = new PdfDocument();
pdfDoc.Pages.Add();
pdfDoc.Pages[0].Orientation = PdfSharp.PageOrientation.Landscape;
pdfDoc.Pages[0].Size = PdfSharp.PageSize.Letter;
pdfDoc.Pages[0].Rotate = 0;
pdfDoc.Save(filename);

Example 3 - This seems to have solved my problem

var pdfPage = new PdfPage();
pdfPage.Orientation = PdfSharp.PageOrientation.Landscape;
pdfPage.Size = PdfSharp.PageSize.Letter;
pdfPage.Rotate = 0;
var pdfDoc = new PdfDocument();
pdfDoc.Pages.Add(pdfPage);
pdfDoc.Save(filename);

Anyone has any idea of what am I missing here? I seem to do the same in either of these examples, as far as I'm concerned.

Solution is:

var pdfPage = new PdfPage();
pdfPage.Size = PdfSharp.PageSize.Letter;
pdfPage.Orientation = PdfSharp.PageOrientation.Landscape;
pdfPage.Rotate = 0;
var pdfDoc = new PdfDocument();
pdfDoc.Pages.Add(pdfPage);
pdfDoc.Save(filename);

Set size first.

Thanks for any comments and/or answers!

Will Marcouiller
It seems that I have to specify twice the page orientation setting, otherwise it is not valid...
Will Marcouiller
I have noticed that if I set the orientation before the size, it's like the orientation was reset. However, when I set the size before the orientation, it looks like it works better. Very confusing all this...
Will Marcouiller
+1  A: 

I examined this issue. It seems you have to set "page.Size" before setting "page.Orientation" to landscape. This is a bug because the order shouldn't matter here.

Order matters - but the workaround is simple: swap 2 lines in Example 1 and you're done (BTW: no need to set Rotate to 0).

The best place to ask PDFsharp and MigraDoc Foundation related questions is the PDFsharp forum. The PDFsharp Team will not monitor stackoverflow.com on a regular basis.

PDFsharp Forum:
http://forum.pdfsharp.net/

PDFsharp Team
Thank you so much for your answer!It is not always obvious to assure that our workaround is good until another programmer comes in. This helps me a lot. Thank you!
Will Marcouiller
And thanks also for the PDFSharp's forum URL
Will Marcouiller