C# 设置Word页面大小和页边距

2025-10-02 17:10:05

1、在E-iceblue官网或者Nuget网站上下载Free Spire.Doc for .NET的安装包后,注意在编辑代码时,添加引用Spire.Doc.dll到程序。dll文件可在安装路径下的Bin文件夹中获取。

C# 设置Word页面大小和页边距

1、using Spire.Doc;

using Spire.Doc.Documents;

namespace SetPageSize_Doc

{

    class Program

    {

        static void Main(string[] args)

        {

            //实例化一个Document对象

            Document doc = new Document();

            //载入测试的Word文档

            doc.LoadFromFile("test.docx");

            //获取第一个Section对象

            Section sec = doc.Sections[0];

            //设置纸张大小为信纸

            sec.PageSetup.PageSize = PageSize.Letter;

            //分别设置四个方向的页边距

            sec.PageSetup.Margins.Top = 20f;

            sec.PageSetup.Margins.Left = 30f;

            sec.PageSetup.Margins.Bottom = 20f;

            sec.PageSetup.Margins.Right = 30f;

            //把纸张方向设置为横向

            sec.PageSetup.Orientation = PageOrientation.Landscape;

            //保存并打开文档

            doc.SaveToFile("result.docx", FileFormat.Docx2010);

            System.Diagnostics.Process.Start("result.docx");

        }

    }

}

2、完成代码后,调试运行程序,生成文档。下图是调整页面大小以及页边距后的效果图。

C# 设置Word页面大小和页边距

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢