Merge Pdfs Using iTextSharp, C#

Merge Pdfs Using iTextSharp, C#

Posted by: Shiv
Category :

I had to automate the process of merging a couple of pdfs and insert  an image on a particular page. Since this involved programming, I decided to use the iTextSharp Library. You can find the dll here.  iTextSharp is a tool that is used to create and manipulate pdfs.  It is really easy to create pdfs using this simple library rather than working with Adobe, since Adobe’s API is really expensive compared to iTextSharp, which is free! There are other Libraries like PdfSharp etc. but we decided to go with iTextSharp.

Now to dive in to the problem – I have to merge a pdf with an image into a new pdf with the option of adding more pdfs using the iTextSharp library.

With a little trial and error and a LOT of googling, I present my solution


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ItextSharp.Properties;
using iTextSharp.text;
using System.Collections;
using iTextSharp.text.pdf;
using System.IO;

Code: I’ve Written a modular code so that it can be reused and easily read

namespace ItextSharp
    public partial class Form1 : Form
         // Function call with parameters    
        public void GenerateBookPdf(string SourceFolder, string FileName, 
        string PdfPath, string ImagePath, int PageNumber)

            PdfImageMerge merge = new PdfImageMerge();
            //set the properties of the class
            merge.Source_Folder = SourceFolder;
            merge.Result_File = Path.Combine(SourceFolder, FileName + ".pdf");

            //Specify the page number

            merge.PageNumber = PageNumber;

              //You can merge any number of pdf’s together
             // Add a new file, together with a given docname to the fileList and
            // namelist collection , Multiple pdf’s can be merged together



        public class PdfImageMerge

            /// Add the image to the Pdf Page
            private Document AddImageToPdfPage(Document doc, string ImgPath)
                iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(ImgPath);
                logo.Alignment = Element.ALIGN_MIDDLE;
                return doc;


            public void StartMerge()

                Document document = new Document();


                    PdfWriter writePdf = PdfWriter.GetInstance(document,
                    new FileStream(result_file, FileMode.Create));


                    PdfContentByte cb = writePdf.DirectContent;
                    PdfImportedPage page;

                    int n = 0;
                    int pdf_rotation = 0;

                    foreach (string filename in files)
                        //The current file path
                        string filePath = source_folder + filename;

                        PdfReader readPdf = new PdfReader(filePath);

                        n = readPdf.NumberOfPages;

                        for (int i=1;i < n+1;i++)


                            if (i == 1)
                                Chunk fileChunk = new Chunk(" ");

                            //Specify the pagenumber and path number
                            if (PageNumber != null)
                                if (i == PageNumber)
                                    document = AddImageToPdfPage(document, ImagePath);

                            page = writePdf.GetImportedPage(readPdf, i);
                            pdf_rotation = readPdf.GetPageRotation(i);
                            if (pdf_rotation == 90 || pdf_rotation == 270)
                                cb.AddTemplate(page, 0, -1f, 1f, 0, 0,
                                cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
                catch (Exception e) { throw e; }
                finally { document.Close(); }

            public int pageNumber;
            public string source_folder;
            public string result_file;

            public string imagePath;

            public IList files = new ArrayList();

            /// Gets or Sets the SourceFolder
            public string Source_Folder
                get { return source_folder; }
                set { source_folder = value; }

            /// Gets or Sets the DestinationFile
            public string Result_File
                get { return result_file; }
                set { result_file = value; }
            // Gets or Sets the Image Path
            public string ImagePath
                get { return imagePath; }
                set { imagePath = value; }

            // Gets or Sets the Page number to insert image
            public int PageNumber
                get { return pageNumber; }
                set { pageNumber = value; }



This seems to solve my Problem and is working like a charm. In the windows application I can also specify the file source and destination dynamically. This code can be used as a base to do a lot of other modifications on the go. Try the library and use this code to merge your pdfs.

Happy coding…

About the Author

At 22, Shivanath Devinarayanan is Suyati’s youngest software developer. Another hard-core “techie”, Shiv is famous for his encyclopedic knowledge of multiple cuisines (you name it and he knows it!). And that is just the tip of the ice-berg! You want advise on the latest laptops and cell phones? The current fashion trends? Famous restaurants in and around Kerala? Shiv is the person to talk to! You can also mail him at However, his services do not come free of cost!

Leave a Comment

Your email address will not be published.