itextsharp修剪pdf文档的页面

Chr*_*son 9 .net c# itextsharp

我有一个pdf文档,其中包含我用c#以编程方式填写的表单字段.根据三个条件,我需要修剪(删除)该文档中的某些页面.

这可能吗?

条件1:我需要保留第1-4页,但删除第5页和第6页

对于条件2:我需要保留第1-4页但删除5并保持6

对于条件3:我需要保留1-5页但删除6

Mat*_*ger 20

使用PdfReader.SelectPages()与PdfStamper结合使用.以下代码使用iTextSharp 5.5.1.

public void SelectPages(string inputPdf, string pageSelection, string outputPdf)
{
    using (PdfReader reader = new PdfReader(inputPdf))
    {
        reader.SelectPages(pageSelection);

        using (PdfStamper stamper = new PdfStamper(reader, File.Create(outputPdf)))
        {
            stamper.Close();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后使用每个条件的正确页面选择调用此方法.

条件1:

SelectPages(inputPdf, "1-4", outputPdf);
Run Code Online (Sandbox Code Playgroud)

条件2:

SelectPages(inputPdf, "1-4,6", outputPdf);
Run Code Online (Sandbox Code Playgroud)

要么

SelectPages(inputPdf, "1-6,!5", outputPdf);
Run Code Online (Sandbox Code Playgroud)

条件3:

SelectPages(inputPdf, "1-5", outputPdf);
Run Code Online (Sandbox Code Playgroud)

以下是关于构成页面选择的iTextSharp源代码的注释.这是在SequenceList类中,用于处理页面选择:

/**
* This class expands a string into a list of numbers. The main use is to select a
* range of pages.
* <p>
* The general systax is:<br>
* [!][o][odd][e][even]start-end
* <p>
* You can have multiple ranges separated by commas ','. The '!' modifier removes the
* range from what is already selected. The range changes are incremental, that is,
* numbers are added or deleted as the range appears. The start or the end, but not both, can be ommited.
*/
Run Code Online (Sandbox Code Playgroud)


Chr*_*aas 7

您实际执行的操作不是删除文档中的页面,而是创建新文档,而只导入要保留的页面.下面是一个完整的WinForms应用程序(目标iTextSharp 5.1.1.0).该函数的最后一个参数removePagesFromPdf是要保留的页面数组.

下面的代码使用物理文件,但很容易转换为基于流的东西,这样你就不必写入磁盘了.

using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using iTextSharp.text.pdf;
using iTextSharp.text;


namespace Full_Profile1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //The files that we are working with
            string sourceFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            string sourceFile = Path.Combine(sourceFolder, "Test.pdf");
            string destFile = Path.Combine(sourceFolder, "TestOutput.pdf");

            //Remove all pages except 1,2,3,4 and 6
            removePagesFromPdf(sourceFile, destFile, 1, 2, 3, 4, 6);
            this.Close();
        }
        public void removePagesFromPdf(String sourceFile, String destinationFile, params int[] pagesToKeep)
        {
            //Used to pull individual pages from our source
            PdfReader r = new PdfReader(sourceFile);
            //Create our destination file
            using (FileStream fs = new FileStream(destinationFile, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                using (Document doc = new Document())
                {
                    using (PdfWriter w = PdfWriter.GetInstance(doc, fs))
                    {
                        //Open the desitination for writing
                        doc.Open();
                        //Loop through each page that we want to keep
                        foreach (int page in pagesToKeep)
                        {
                            //Add a new blank page to destination document
                            doc.NewPage();
                            //Extract the given page from our reader and add it directly to the destination PDF
                            w.DirectContent.AddTemplate(w.GetImportedPage(r, page), 0, 0);
                        }
                        //Close our document
                        doc.Close();
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)