在 Microsoft Word 中,默认页面大小为 8.5x11 英寸字母。除此之外,MS Word 还提供了一些其他的预定义页面尺寸,例如 Legal (5.4x14)、A4 (8.27x11.69)、A5 (5.83x8.27) 供用户轻松更改页面大小以满足不同的需求。此外,如果您的页面尺寸不在列表中,您可以从页面设置中选择自定义尺寸来自定义具有指定宽度和高度的页面尺寸。
Spire.Doc 最新下载/product/3368/download
事实上,Spire.Doc 已经为程序员提供了类似的方法来设置 Word 页面大小。本文将介绍如何选择定义的页面大小或如何使用 C# 中的 Spire.Doc 为 Word 文档设置自定义大小。
代码片段:
第 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- to Word ) "+ "for .NET in C# and . ");
第 4 步:保存文件并开始查看。
doc.SaveToFile("result.docx", FileFormat.Docx);System.Diagnostics.Process.Start("result.docx");
输出:
1) 选择定义的页面大小。
2)自定义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- to Word ) "+ "for .NET in C# and . ");doc.SaveToFile("result.docx", FileFormat.Docx);System.Diagnostics.Process.Start("result.docx");}}}