翻譯|使用教程|編輯:胡濤|2022-06-28 14:28:56.730|閱讀 322 次
概述:本文將介紹如何選擇定義的頁面大小或如何使用 C# 中的 Spire.Doc 為 Word 文檔設置自定義大小。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
在 Microsoft Word 中,默認頁面大小為 8.5x11 英寸字母。除此之外,MS Word 還提供了一些其他的預定義頁面尺寸,例如 Legal (5.4x14)、A4 (8.27x11.69)、A5 (5.83x8.27) 供用戶輕松更改頁面大小以滿足不同的需求。此外,如果您的頁面尺寸不在列表中,您可以從頁面設置中選擇自定義尺寸來自定義具有指定寬度和高度的頁面尺寸。
事實上,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-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) 選擇定義的頁面大小。
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-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產品
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn