本主题介绍如何使用 C# 在 PDF 中插入分页符。它包含设置开发环境的详细信息、步骤列表以及示例代码,展示了如何使用 C# 在 PDF 文档中插入分页符。您将获得定义页面上添加分页符的位置的详细信息。
使用 C# 向 PDF 添加分页符的步骤
- 设置环境以使用 Aspose.PDF for .NET 添加分页符
- 将源 PDF 文件加载到 Document 类对象中,以插入分页符
- 创建另一个空的 PDF 文件以保存输出的 PDF 文件
- 实例化 PdfFileEditor 对象以处理 PDF
- 在指定位置添加分页符
- 保存输出
上述步骤描述了如何使用 C# 在 Adobe PDF 中插入分页符。加载源 PDF 文档,创建一个空 PDF 文档以保存输出,并实例化 PdfFileEditor 对象。通过提供页码和从文档底部开始的位置来调用 AddPageBreak() 方法。
使用 C# 在 PDF 中添加分页符的代码
using Aspose.Pdf; | |
using Aspose.Pdf.Facades; | |
using System; | |
namespace NewAsposeTests | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
new License().SetLicense("License.lic"); | |
// Load the source document | |
Document doc = new Document("sample.pdf"); | |
// Create a new destination PDF document | |
Document dest = new Document(); | |
// Instantiate the PdfFileEditor object | |
PdfFileEditor fileEditor = new PdfFileEditor(); | |
// Add a page break at the specified position | |
fileEditor.AddPageBreak(doc, dest, new PdfFileEditor.PageBreak[] | |
{ | |
new PdfFileEditor.PageBreak(3, 300) | |
}); | |
// Save the output file | |
dest.Save("PageBreak_out.pdf"); | |
Console.WriteLine("Add page break to PDF"); | |
} | |
} | |
} |
此示例代码演示了如何使用 C# 在 Adobe PDF 中插入分页符。PdfFileEditor 包含处理 PDF 文件中的内容的功能。使用 AddPageBreak() 方法,该方法采用源文档、输出文档和分页符数组,您可以在其中设置页码和距底部的距离(以像素为单位)以添加分页符。
这个简单示例教会了我们如何使用 C# 在 PDF 中添加分页符。要按页面拆分文档,请参阅 如何在 C# 中按页面拆分 PDF 文件 上的文章。