如何为word文档添加页面边框

本文关键字:边框 添加 文档 word | 更新日期: 2023-09-27 18:12:57

我想给word文档添加一个页面边框。这是我的代码

foreach (Microsoft.Office.Interop.Word.Section sectio in document.Sections)
{
    //Get the header range and add the header details.
    Microsoft.Office.Interop.Word.Border border = sectio.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderHorizontal];
    border.Visible = true;
    border.Application.ActiveDocument.Activate();
}

如何为word文档添加页面边框

也许你需要设置边框的样式、宽度和颜色

如果我试图通过VBA设置它,我写这样的代码

With Selection.Borders(wdBorderBottom)
        .LineStyle = Options.DefaultBorderLineStyle
        .LineWidth = Options.DefaultBorderLineWidth
        .Color = Options.DefaultBorderColor
    End With

下面是一个基于https://www.nuget.org/packages/FreeSpire.Doc/

的解决方案
//initialize an instance
Document document = new Document();
//load a document
document.LoadFromFile(@"Example.docx");
Section section = document.Sections[0];
//add page borders with special style and color
section.PageSetup.Borders.BorderType = BorderStyle.DoubleWave;
section.PageSetup.Borders.Color = Color.LightSeaGreen;
//set the spaces between border and text
section.PageSetup.Borders.Left.Space = 50;
section.PageSetup.Borders.Right.Space = 50;
//save
document.SaveToFile("PageBorders.docx", FileFormat.Docx);