C# 如何设置文档的页面大小?

C# 如何设置文档的页面大小?,c#,migradoc,C#,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 doc

对不起,我只是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,而应使用克隆()
PageFormat
不适用于
Clone()
,因为
PageWidth
PageHeight
设置为默认大小A4。
要获取字母格式,可以使用此代码覆盖
PageWidth
PageHeight

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";
要获取字母格式,可以使用此代码重置
PageWidth
PageHeight
以使
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()
,可以使用此处显示的方法设置页面大小。

谢谢。您的意思是
section.PageSetup.PageWidth=“21cm”;section.PageSetup.PageHeight=“29.7cm”?我可以吗?请回答一个问题。我发现AddTab在表和段落中有不同的行为。表中的AddTab将使内容向右对齐。像css
float:right
。但在这一段中,情况并非如此。只需像平常一样添加一个选项卡。这是真的吗?请帮助澄清。
AddTab()
就像在Word中按Tab键一样。效果取决于为段落设置的制表位。制表位可以是左对齐、右对齐、居中或十进制。评论不是提出新问题的合适地方。也可以看到这篇文章:Document=file,Section=group of pages,.doc很像Word:你有一个包含段落的文档。这就是我一直在寻找的。谢谢