使用iText进行Pdf合并/重叠

问题描述 投票:2回答:1

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

例如:INPUT:PDF#1(1页)PDF#2(1页)

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

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

先感谢您。

c# .net pdf itextsharp itext
1个回答
3
投票

这实际上很容易做到。 PdfWriter对象有一个名为GetImportedPage()的实例方法,它返回一个PdfImportedPage对象。这个对象可以传递给PdfContentByteAddTemplate()方法。

GetImportedPage()获取PdfReader对象和您想要的页码。你可以从PdfContentBytePdfWriter财产的实例中获得一个DirectContent

下面的代码是一个完整的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();
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.