Word控件Spire.Doc 【页面设置】教程(11) ;如何在 C# 中设置 Word 文档的页面大小

在 Microsoft Word 中,默认页面大小为 8.5×11 英寸字母。除此之外,MS Word 还提供了一些其他的预定义页面尺寸,例如 Legal (5.4×14)、A4 (8.27×11.69)、A5 (5.83×8.27) 供用户轻松更改页面大小以满足不同的需求。此外,如果您的页面尺寸不在列表中,您可以从页面设置中选择自定义尺寸来自定义具有指定宽度和高度的页面尺寸。

代码片段:

第 1 步:创建一个带有空白部分的 Word 文档。

Document doc = new Document();Section section = doc.AddSection();

第 2 步:将页面大小设置为 A4。在 PageSize 类中,为您预先配置了许多已定义的页面大小。

section.PageSetup.PageSize = PageSize.A4;

但是,如果您想将页面设置为自定义大小,请将上面的代码替换为以下代码段。

section.PageSetup.PageSize = new System.Drawing.SizeF(500, 800);section.PageSetup.Orientation = PageOrientation.Portrait;

第 3 步:将一些文本附加到该部分。

Paragraph Para = section.AddParagraph();Para.AppendText("Spire.Doc for .NET, a professional .NET Word component, "+ "enables developers to perform a large range of tasks on Word document (from Version Word97-2003 to Word 2010) "+ "for .NET in C# and VB.NET. ");

第 4 步:保存文件并开始查看。

doc.SaveToFile("result.docx", FileFormat.Docx);System.Diagnostics.Process.Start("result.docx");

输出

1) 选择定义的页面大小。

如何在 C# 中设置 Word 文档的页面大小

2)自定义Word文档的大小。

如何在 C# 中设置 Word 文档的页面大小

完整代码

using System.Drawing;using Spire.Doc;using Spire.Doc.Documents;namespace CustomPageSize{class Program{static void Main(string[] args){Document doc = new Document();Section section = doc.AddSection();section.PageSetup.PageSize = PageSize.A4;//section.PageSetup.PageSize = new System.Drawing.SizeF(550, 800);//section.PageSetup.Orientation = PageOrientation.Portrait;Paragraph Para = section.AddParagraph();Para.AppendText("Spire.Doc for .NET, a professional .NET Word component, "+ "enables developers to perform a large range of tasks on Word document (from Version Word97-2003 to Word 2010) "+ "for .NET in C# and VB.NET. ");doc.SaveToFile("result.docx", FileFormat.Docx);System.Diagnostics.Process.Start("result.docx");}}}


欢迎下载|体验更多E-iceblue产品

获取更多信息请咨询在线客服  


标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年5月25日
下一篇 2022年5月25日

相关推荐

发表回复

登录后才能评论