4

我已经将 iText 用于一些不同的实用程序,例如我们成功地合并和编辑 pdf 文件。现在我需要重叠 2 个 pdf 页面:

例如: 输入:PDF#1(1 页) PDF#2(1 页)

输出:PDF#3(1 页:这是 2 个输入页重叠的结果)

我不知道是否可以使用 iText 最新版本来做到这一点。我也在考虑使用 2 个输入 PDF 文件之一作为 PDF 输出文件的背景。

先感谢您。

4

1 回答 1

5

这实际上很容易做到。该PdfWriter对象有一个调用实例方法GetImportedPage(),该方法返回一个PdfImportedPage对象。这个对象可以传递给PdfContentByte'sAddTemplate()方法。

GetImportedPage()接受一个PdfReader对象和您想要获取的页码。您可以从 a的属性PdfContentByte实例中获取 a 。PdfWriterDirectContent

下面的代码是一个完整的工作 C# 2010 WinForms 应用程序,目标是 iTextSharp 5.1.2.0,它显示了这一切。它首先在桌面上创建两个文件,第一个文件只有纯红色背景,第二个文件只有一个段落。然后它将这两个重叠的文件合并到第三个文档中。有关其他注释,请参见代码。

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

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

        private void Form1_Load(object sender, EventArgs e) {
            //Folder that we'll work from
            string workingFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            string pdf1 = Path.Combine(workingFolder, "pdf1.pdf");//PDF with solid red background color
            string pdf2 = Path.Combine(workingFolder, "pdf2.pdf");//PDF with text
            string pdf3 = Path.Combine(workingFolder, "pdf3.pdf");//Merged PDF

            //Create a basic PDF filled with red, nothing special
            using (FileStream fs = new FileStream(pdf1, FileMode.Create, FileAccess.Write, FileShare.None)) {
                using (Document doc = new Document(PageSize.LETTER)) {
                    using (PdfWriter writer = PdfWriter.GetInstance(doc, fs)) {
                        doc.Open();
                        PdfContentByte cb = writer.DirectContent;
                        cb.SetColorFill(BaseColor.RED);
                        cb.Rectangle(0, 0, doc.PageSize.Width, doc.PageSize.Height);
                        cb.Fill();
                        doc.Close();
                    }
                }
            }

            //Create a basic PDF with a single line of text, nothing special
            using (FileStream fs = new FileStream(pdf2, FileMode.Create, FileAccess.Write, FileShare.None)) {
                using (Document doc = new Document(PageSize.LETTER)) {
                    using (PdfWriter writer = PdfWriter.GetInstance(doc, fs)) {
                        doc.Open();
                        doc.Add(new Paragraph("This is a test"));
                        doc.Close();
                    }
                }
            }

            //Create a basic PDF
            using (FileStream fs = new FileStream(pdf3, FileMode.Create, FileAccess.Write, FileShare.None)) {
                using (Document doc = new Document(PageSize.LETTER)) {
                    using (PdfWriter writer = PdfWriter.GetInstance(doc, fs)) {
                        doc.Open();

                        //Get page 1 of the first file
                        PdfImportedPage imp1 = writer.GetImportedPage(new PdfReader(pdf1), 1);
                        //Get page 2 of the second file
                        PdfImportedPage imp2 = writer.GetImportedPage(new PdfReader(pdf2), 1);
                        //Add the first file to coordinates 0,0
                        writer.DirectContent.AddTemplate(imp1, 0, 0);
                        //Since we don't call NewPage the next call will operate on the same page
                        writer.DirectContent.AddTemplate(imp2, 0, 0);
                        doc.Close();
                    }
                }
            }

            this.Close();
        }
    }
}
于 2012-04-12T14:58:42.797 回答