如何设置MigraDoc的页面大小

本文关键字:MigraDoc 何设置 设置 | 更新日期: 2023-09-27 18:14:25

对不起,我只是PDFsharp的初学者。

如何设置文档的PageSize ?假设是A4。如何设置?这是我的代码。谢谢。

    Document document = new Document();
    // Add a section to the document
    Section section = document.AddSection();
    section.AddParagraph("dddddd");

    // Add a section to the document
    var table = section.AddTable();
    table.AddColumn("8cm");
    table.AddColumn("8cm");
    var row = table.AddRow();
    var paragraph = row.Cells[0].AddParagraph("Left text");
    paragraph.AddTab();
    paragraph.AddText("Right text");
    paragraph.Format.ClearAll();
    // TabStop at column width minus inner margins and borders:
    paragraph.Format.AddTabStop("27.7cm", TabAlignment.Right);
    row.Cells[1].AddParagraph("Second column");
    table.Borders.Width = 1;

如何设置MigraDoc的页面大小

A4为默认大小。

每个部分都有一个PageSetup属性,你可以设置页面大小,页边距等。

var section = document.LastSection;
section.PageSetup.PageFormat = PageFormat.A4;
section.PageSetup.TopMargin = "3cm";

你不应该修改defaultpagessetup,而应该使用Clone()PageFormatClone()不起作用,因为PageWidthPageHeight被设置为默认大小A4。
要获得字母格式,您可以使用以下代码覆盖PageWidthPageHeight:

var section = document.LastSection;
section.PageSetup = Document.DefaultPageSetup.Clone();
section.PageSetup.PageFormat = PageFormat.Letter; // Has no effect after Clone(), just for documentation purposes.
section.PageSetup.PageWidth = Unit.FromPoint(612);
section.PageSetup.PageHeight = Unit.FromPoint(792);
section.PageSetup.TopMargin = "3cm";

要获得字母格式,您可以使用以下代码重置PageWidthPageHeight,使PageFormat再次工作:

var section = document.LastSection;
section.PageSetup = Document.DefaultPageSetup.Clone();
section.PageSetup.PageWidth = Unit.Empty;
section.PageSetup.PageHeight = Unit.Empty;
section.PageSetup.PageFormat = PageFormat.Letter;
section.PageSetup.TopMargin = "3cm";

创建Clone()是有用的,如果你的代码使用例如左右边距来计算表的宽度等。如果您显式设置所有边距或不使用边距进行计算,则无需创建克隆。
如果您需要Clone(),您可以使用这里显示的方法来设置页面大小。