2015-09-24 118 views
0

對不起,我只是PDFsharp的初學者。如何設置MigraDoc的頁面大小?

如何將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; 
+2

Document = file,Section =一組頁面,[etc](http://www.pdfsharp.net/wiki/MigraDoc_PageSetup.ashx)。 – Sinatr

+1

MigraDoc非常像Word:你有一個包含段落的文檔。 –

+0

是的。那正是我想要尋找的。謝謝。 –

回答

3

A4是默認尺寸。

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

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

你永遠不應該修改DefaultPageSetup,使用Clone()代替。 PageFormat不適用於Clone(),因爲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()是有用的,如果你的代碼使用eg左右邊距來計算表格寬度等。如果您明確設置所有邊距或不使用邊距進行計算,則無需創建克隆。
如果您需要Clone()您可以使用此處顯示的方法來設置頁面大小。

+0

謝謝。你的意思是'section.PageSetup.PageWidth =「21cm」; section.PageSetup.PageHeight =「29.7cm」;'?我可以嗎 ? –

+0

在我的文章中添加了一段代碼。 –

+0

請提出一個問題。我發現AddTab在Table和Paragraph中有不同的行爲。表中的AddTab會使內容正確對齊。像css'float:right'。但在段落中卻不是。正常添加一個標籤。這是真的嗎?請幫助澄清。 –