• 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");
    }
    }
    }
  • 相关阅读:
    微服务多模块项目maven打包时报找不到依赖模块中的类
    Java数据结构 | PriorityQueue详解
    智慧客服,为保险服务强力打call
    Redis基本命令的学习和Jedis
    Stream流中的 max()和 sorted()方法
    聊聊GPU与CPU的区别
    Java基于SpringBoot的逍遥大药房管理平台
    虚拟机启动报错CPU被禁用
    归并排序
    Android 12(S) 图像显示系统 - 简单聊聊 SurfaceView 与 BufferQueue的关联(十三)
  • 原文地址:https://blog.csdn.net/m0_67129275/article/details/127120920