如何在MigraDoc库中设置所有页面的文档方向?

15

我正在使用 MigraDoc 编程生成包含文本、图片和表格的PDF文件。

我需要在文档对象中为所有页面设置Document方向,使其为横向(Landscape)。

因此,我尝试了以下方法。

document.DefaultPageSetup.Orientation = Orientation.Landscape;

但我收到了以下调试断言错误。

---------------------------
Assertion Failed: Abort=Quit, Retry=Debug, Ignore=Continue
---------------------------

DefaultPageSetup must not be modified

如果我点击忽略,它会继续并且方向确实是横向

然而,我想确保我正确地做了这件事。

所以问题是,如何使用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

更新:

解决方案:这是可行的代码,基于下面Thomas的答案(为了其他可能正在寻找此解决方案的人的利益)。

// 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适用于MigraDoc。实际上,这是一个MigraDoc问题,而不是PDFsharp问题。 - I liked the old Stack Overflow
1
嗨,Shiva,你刚刚帮我解决了问题。感谢你 :-) 尽管你的解决方案没有包含将修改后的 pageSetup 分配给 section 对象所需的信息。但无论如何,还是谢谢你 :-) - Maurice Klimek
1个回答

11

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

然后,您可以修改默认设置的副本,这样就不会有任何断言失败。

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

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

请参见PDFsharp论坛中的此帖子,其中使用Clone()创建DefaultPageSetup的副本:


谢谢Thomas!根据你的回答和链接,我修复了代码以克隆DefaultPageSetup并应用该设置。我也更新了我的问题,并附上了代码。 - Shiva

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接