簡體   English   中英

如何設置MigraDoc的頁面大小?

[英]How can set the page size of MigraDoc?

抱歉,我只是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;

A4是默認大小。

每個部分都有一個PageSetup屬性,您可以在其中設置頁面大小,邊距等。

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

您永遠不應該修改DefaultPageSetup,而是使用Clone() PageFormat不適用於Clone() ,因為PageWidthPageHeight設置為默認大小A4。
要獲取Letter格式,您可以使用此代碼覆蓋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";

要獲取Letter格式,您可以使用此代碼重置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() ,可以使用此處顯示的方法設置頁面大小。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM