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


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

    Spire.Doc for.NET 最新下载icon-default.png?t=M85Bhttps://www.evget.com/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-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");
    }
    }
    }
  • 相关阅读:
    C# 调用 Rust 来删除文件夹 ( 包含大量软链接 和 无效链接)
    (九)类特殊成员(属性和方法)
    浅析Java责任链模式实现
    编写bat脚本调用hexview进行软件签名
    如何知道你的Linux内核占用的内存大小?
    AtCoder Beginner Contest 278 G.Generalized Subtraction Game(思维题/博弈 multi-sg)
    SAP MDG —— MDG on S/4HANA 2022 创新汇总(Central Governance)
    ps dp在live video模式下播放视频
    tortoisegit 教程
    【力扣 - 和为K的子数组】
  • 原文地址:https://blog.csdn.net/m0_67129275/article/details/127120920