关于c#:NET-PDF中删除页面

3次阅读

共计 1214 个字符,预计需要花费 4 分钟才能阅读完成。

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 System
Imports Spire.Pdf

Module 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 Sub
End Module

删除成果示意:

以上代码援用均来自收费的 Free Spire.PDF for .NET 库。

正文完
 0