iTextSharp的修剪PDF文档的页面 [英] itextsharp trimming pdf document's pages

查看:118
本文介绍了iTextSharp的修剪PDF文档的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PDF文档,有说我填写编程方式使用C#表单域。根据三个条件,我需要修剪(删除)某些页面从该文件。

I have a pdf document that has form fields that I'm filling out programatically with c#. Depending on three conditions, I need to trim (delete) some of the pages from that document.

这是可能做到?

有关条件1:我需要保持1-4的网页,但删除页面5,6

for condition 1: I need to keep pages 1-4 but delete pages 5 and 6

有关条件2:我需要保持1-4页,但删除5,保持6

for condition 2: I need to keep pages 1-4 but delete 5 and keep 6

有关条件3:我需要保持1-5页,但删除6

for condition 3: I need to keep pages 1-5 but delete 6

推荐答案

而不是删除文档中的你其实就是创建一个新的文件,只导入您要保留的网页页面。下面是一个完整的工作的WinForms应用程序,并认为(目标定位iTextSharp的5.1.1.0)。最后一个参数的函数 removePagesFromPdf 是网页保持一个数组。

Instead of deleting pages in a document what you actually do is create a new document and only import the pages that you want to keep. Below is a full working WinForms app that does that (targetting iTextSharp 5.1.1.0). The last parameter to the function removePagesFromPdf is an array of pages to keep.

以下作品中的code关闭物理文件,但会很容易地转换为基于流的东西,这样你就不必写入磁盘,如果你不想。

The code below works off of physical files but would be very easy to convert to something based on streams so that you don't have to write to disk if you don't want to.

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();
                    }
                }
            }
        }
    }
}

这篇关于iTextSharp的修剪PDF文档的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆