c# 给word添加分页符
时间: 2023-12-08 21:04:22 浏览: 197
你可以使用 Microsoft.Office.Interop.Word 库来在 Word 文档中添加分页符。下面是一个示例代码,可以在 Word 文档的第二页之前添加一个分页符:
```csharp
using Microsoft.Office.Interop.Word;
// ...
// 打开 Word 文档
Application wordApp = new Application();
Document doc = wordApp.Documents.Open(@"C:\example.docx");
// 在第二页之前添加分页符
Range range = doc.Content;
range.Collapse(WdCollapseDirection.wdCollapseEnd);
range.InsertBreak(WdBreakType.wdPageBreak);
// 保存并关闭 Word 文档
doc.Save();
doc.Close();
wordApp.Quit();
```
需要注意的是,如果文档中已经存在多个分页符,该代码会在最后一个分页符之后添加一个新的分页符。如果需要在指定位置添加分页符,可以在 `Range` 对象中指定插入的位置。
阅读全文
相关推荐
















