Duncan_McCloud
Duncan_McCloud

Reputation: 553

Pdf Merge/Overlap with iText

I have used iText for some various utility, such us merge and editing of pdf files with success. Now I need to overlap 2 pdf pages:

For Instance: INPUT: PDF#1 (1 Page) PDF#2 (1 Page)

OUTPUT: PDF#3 (1 Page: This is the result of the 2 Input Pages Overlapped)

I don't know if it's possible to do this with iText latest version. I am also considering to use one of the 2 input PDF Files as background for the PDF Output Files.

Thank you in advance.

Upvotes: 5

Views: 2700

Answers (1)

Chris Haas
Chris Haas

Reputation: 55457

It's actually pretty easy to do. The PdfWriter object has an instance method called GetImportedPage() which returns a PdfImportedPage object. This object can be passed to a PdfContentByte's AddTemplate() method.

GetImportedPage() takes a PdfReader object and the page number that you want to get. You can get a PdfContentByte from an instance of a PdfWriter's DirectContent property.

The code below is a full working C# 2010 WinForms app targeting iTextSharp 5.1.2.0 that shows this all off. It first creates two files on the desktop, the first with just a solid red background color and the second with just a paragraph. It then combines those two files overlapping into a third document. See the code for additional comments.

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

Upvotes: 5

Related Questions