简体   繁体   English

PdfSharp - 在Acrobat中打开文档时,PDF页面设置错误

[英]PdfSharp - Wrong PDF page settings when document is opened in Acrobat

I have written a piece of code which uses the PdfSharp library. 我编写了一段使用PdfSharp库的代码。 The instance of PdfSharp.Pdf.PdfDocument created saves to disk as expected. PdfSharp.Pdf.PdfDocument的实例已按预期保存到磁盘。 The right content is displayed, but onto the wrong page settings. 显示正确的内容,但显示在错误的页面设置上。

The default page settings for PdfSharp are: PdfSharp的默认页面设置为:

  1. PdfSharp.PageSizes.A4 PdfSharp.PageSizes.A4
  2. PdfSharp.PageOrientation.Portrait 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. 我创建了PdfDocument类的实例,并将一个新的PdfPage类实例添加到其Pages集合属性中。 Then, I setup the page like this: 然后,我像这样设置页面:

  1. PdfDocument.Pages[0].Size = PdfSharp.PageSizes.Letter PdfDocument.Pages [0] .Size = PdfSharp.PageSizes.Letter
  2. PdfDocument.Pages[0].Orientation = PdfSharp.PageOrientation.Landscape 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. Process.Start(myPdfFilename) - 然后Acrobat Reader打开我的文档。
  6. The page settings are A4 - Portrait... 页面设置为A4 - 纵向...

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. 我知道Acrobat Reader中有一个选项,允许用户在不改变文本方向的情况下更改页面方向。 No matter whether I check this option or not, still the wrong settings keep going. 无论我是否选中此选项,仍然会出现错误的设置。

Anyone has an idea? 有人有想法吗? Thanks! 谢谢!

For some strange reason, PdfSharp seems not to behave the same with both the following: 出于某些奇怪的原因,PdfSharp似乎与以下两种情况不同:

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. 示例1 - 即使在调用和调用PdfDocument.Save()方法之后页面设置正确,它似乎也没有将PdfPage类的实例与PdfDocument相关联。

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... 例2 - 这里也一样......

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 例3 - 这似乎解决了我的问题

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! 感谢您的任何意见和/或答案!

I examined this issue. 我检查了这个问题。 It seems you have to set "page.Size" before setting "page.Orientation" to landscape. 在将“page.Orientation”设置为landscape之前,您似乎必须设置“page.Size”。 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). 订单很重要 - 但解决方法很简单:在示例1中交换2行并完成(BTW:无需将Rotate设置为0)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM