PDF 合并/与 iText 重叠

Dun*_*oud 4 .net c# pdf itext itextsharp

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

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

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

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

先感谢您。

Chr*_*aas 5

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

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

下面的代码是一个完整的 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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)