如何在MigraDoc库中设置文档方向(适用于所有页面)?

我正在使用MigraDoc以编程方式生成包含文本,图像和表格的PDF文件。

我需要将文档对象中的文档方向 (对于所有页面)设置为Landscape

所以我尝试了以下内容。

 document.DefaultPageSetup.Orientation = Orientation.Landscape; 

但是我得到以下调试断言错误。

 --------------------------- Assertion Failed: Abort=Quit, Retry=Debug, Ignore=Continue --------------------------- DefaultPageSetup must not be modified 

如果我单击忽略 ,它会通过并且Orientation确实是Landscape

但是,我想确保我以正确的方式做到这一点。

所以问题是,如何使用MigraDoc库为Document中的所有页面设置文档方向?

这是代码的其余部分(因此它可以帮助您获取上下文)

 using System.Runtime.Remoting.Messaging; using MigraDoc.DocumentObjectModel; namespace MyNamespace.PdfReports { class Documents { public static Document CreateDocument() { // Create a new MigraDoc document Document document = new Document(); document.Info.Title = "The Title"; document.Info.Subject = "The Subject"; document.Info.Author = "Shiva"; document.DefaultPageSetup.Orientation = Orientation.Landscape; 

非常感谢!
-Shiva

更新:

解决方案:这是工作代码,基于托马斯的回答(为了其他可能正在寻找此解决方案的人的利益)。

 // Create a new MigraDoc document Document document = new Document(); //... //...... PageSetup pageSetup = document.DefaultPageSetup.Clone(); // set orientation pageSetup.Orientation = Orientation.Landscape; // ... set other page setting you want here... 

DefaultPageSetup.Clone()分配给您的部分的PageFormat并修改它。

然后,您修改默认设置的副本,并且没有断言将失败。

使用您的方法,所有文档都将默认为横向 – 而不仅仅是您为其设置的文档。

此答案仅适用于MigraDoc,因为只有MigraDoc使用DefaultPageSetup

在PDFsharp论坛中查看此post,其中Clone()用于创建DefaultPageSetup的副本: