PDF 文档通过页面来出现文字、图片等元素,一个 PDF 文档通常有多个页面。有时, PDF 文档中会有一些空白的,或全是不必要、无关内容的页面,尤其是在那些从网络取得的 PDF 文档中。咱们能够用 Spire.PDF for .NET 来删除这些页面。本文将教大家如何应用 Spire.PDF for .NET 删除PDF页面。
引入DLL
一、 通过NuGet装置
右键单击解决方案中的依赖项,找到“治理NuGet包”,在其中搜寻“FreeSpire.PDF”并增加到援用项中。
复制上面代码到控制台装置
PM> Install-Package FreeSpire.PDF
二、 手动增加DLL
在Free Spire.PDF for .NET官网下载免费版后解压,在解决方案中找到依赖项,右键单击找到增加援用项,找到Spire.PDF.dll并增加到援用项中。
删除页面操作步骤
用此工具删除页面非常简单,通过解决甚至可实现疾速删除多个文件的多个页面,具体操作步骤如下:
- 创立 PdfDocument 的对象。
- 用 PdfDocument.LoadFromFile() 办法从磁盘载入 PDF 文档。
- 用 PdfDocument.Pages.RemoveAt() 办法删除第三页和第二页。
- 用 PdfDocument.SaveToFile() 办法保留 PDF 文档。
C# 代码
using System;using Spire.Pdf;namespace RemovePage{ internal class Program { static void Main(string[] args) { //创立 PdfDocument 的对象 PdfDocument pdf = new PdfDocument(); //从磁盘载入 PDF 文档 string input = @"D:\testp\示例.pdf"; pdf.LoadFromFile(input); //删除第三页和第二页 pdf.Pages.RemoveAt(1); pdf.Pages.RemoveAt(2); //保存文档 string output = "删除页面.pdf"; pdf.SaveToFile(output); } }}
VB.NET 代码
Imports SystemImports Spire.PdfModule Program Sub Main(args As String()) '创立 PdfDocument 的对象 Dim pdf As New PdfDocument() '从磁盘载入 PDF 文档 Dim input As String = "D:\testp\示例.pdf" pdf.LoadFromFile(input) '删除第三段和第二段 pdf.Pages.RemoveAt(2) pdf.Pages.RemoveAt(1) '保存文档 Dim output As String = "删除页面.pdf" pdf.SaveToFile(output) End SubEnd Module
删除成果示意:
以上代码援用均来自收费的Free Spire.PDF for .NET库。