13

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

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

所以我尝试了以下方法。

document.DefaultPageSetup.Orientation = Orientation.Landscape;

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

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

DefaultPageSetup must not be modified

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

但是,我想确保我以正确的方式执行此操作。

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

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

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;

非常感谢!
-湿婆

更新:

解决方案:这是工作代码,基于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...
4

1 回答 1

9

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

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

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

此答案仅适用于MigraDoconly MigraDocuses DefaultPageSetup

请参阅PDFsharp 论坛中的此帖子,该帖子Clone()用于创建以下内容的副本DefaultPageSetup

于 2014-03-27T06:28:31.963 回答