抱歉,我只是 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()
, 因为PageWidth
and PageHeight
设置为默认尺寸 A4。
要获得 Letter 格式,您可以使用此代码覆盖PageWidth
and 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
and 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()
您可以使用此处显示的方法来设置页面大小。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)