Click here to Skip to main content
12,945,927 members (37,095 online)
Click here to Skip to main content
Add your own
alternative version


97 bookmarked
Posted 31 Jul 2008

Simple .NET PDF Merger

, 17 Nov 2014 GPL3
Rate this:
Please Sign up or sign in to vote.
A simple .NET PDF merger that supports header and footer text.


There are a couple of PDF mergers available on the Internet. But, either they are commercial products or don't support printing of the header and/or footer text, which is particularly interesting, e.g., to print the page number.


The presented PDF merger uses the open source PDF library iTextSharp to process PDF files. The sample solution also includes a tiny Windows Forms application to demonstrate the functionality.

Using the code

For the merge process, the PDF library takes advantage of the PDF page events of the iTextSharp.text.pdf.PdfWriter object. During the initialization of the PdfPageEvent instance (which inherits from iTextSharp.text.pdf.IPdfPageEvent), necessary information for the header/footer text could be passed in to the constructor call:

<s>writer.PageEvent = new PdfPageEvents(/*Any type of information goes here*/);</s>

It is important that the header and the footer text, both, are rendered in the 'public void OnEndPage(PdfWriter writer, Document document)' method. The 'public void OnStartPage(PdfWriter writer, Document document)' is not accurate.

For performance reasons, this library is now leveraging the PdfCopy class from the iTextSharp library instead of the described method above.

Even though the shown sample is very basic, it generally gives a good overview of how the header and the footer can be populated, e.g., with picture(s), text(s)...

The source code

using System;
using System.Collections.Generic;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace HelveticSolutions.PdfLibrary
    public static class PdfMerger
        /// <summary>
        /// Merge pdf files.
        /// </summary>
        /// <param name="sourceFiles">PDF files being merged.</param>
        /// <returns></returns>
        public static byte[] MergeFiles(List<byte[]> sourceFiles)
            Document document = new Document();
            using (MemoryStream ms = new MemoryStream())
                PdfCopy copy = new PdfCopy(document, ms);
                int documentPageCounter = 0;

                // Iterate through all pdf documents
                for (int fileCounter = 0; fileCounter < sourceFiles.Count; fileCounter++)
                    // Create pdf reader
                    PdfReader reader = new PdfReader(sourceFiles[fileCounter]);
                    int numberOfPages = reader.NumberOfPages;

                    // Iterate through all pages
                    for (int currentPageIndex = 1; currentPageIndex <= numberOfPages; currentPageIndex++)
                        PdfImportedPage importedPage = copy.GetImportedPage(reader, currentPageIndex);
                        PdfCopy.PageStamp pageStamp = copy.CreatePageStamp(importedPage);

                        // Write header
                        ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_CENTER,
                            new Phrase("PDF Merger by Helvetic Solutions"), importedPage.Width / 2, importedPage.Height - 30,
                            importedPage.Width < importedPage.Height ? 0 : 1);

                        // Write footer
                        ColumnText.ShowTextAligned(pageStamp.GetOverContent(), Element.ALIGN_CENTER,
                            new Phrase(String.Format("Page {0}", documentPageCounter)), importedPage.Width / 2, 30,
                            importedPage.Width < importedPage.Height ? 0 : 1);




                return ms.GetBuffer();


  • 01.08.2008 - Article created.
  • 17.11.2014 - Updated implementation based on other user's feedback.


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


About the Author

Michael Ulmann
Architect Swissworx
Australia Australia
MCAD, MCPD Web Developer 2.0, MCPD Enterprise Developer 3.5

My company: Swissworx
My blog: Sitecore Experts

Hopp Schwiiz Smile | :)

You may also be interested in...

Comments and Discussions

QuestionGreat! Pin
AleBrozzo20-Mar-12 18:51
memberAleBrozzo20-Mar-12 18:51 
AnswerRe: Great! Pin
Michael Ulmann13-Nov-14 19:10
memberMichael Ulmann13-Nov-14 19:10 
QuestionTHANK YOU! Pin
Member 340683825-Jan-12 5:04
memberMember 340683825-Jan-12 5:04 
AnswerRe: THANK YOU! Pin
Michael Ulmann18-Mar-12 13:06
memberMichael Ulmann18-Mar-12 13:06 
GeneralRe: THANK YOU! Pin
Member 987752922-Sep-13 22:47
memberMember 987752922-Sep-13 22:47 
GeneralMy vote of 5 Pin
juniorsebr22-Jul-10 2:37
memberjuniorsebr22-Jul-10 2:37 
GeneralRe: My vote of 5 Pin
Michael Ulmann13-Nov-14 19:11
memberMichael Ulmann13-Nov-14 19:11 
GeneralPdfContentByte has a method called ShowTextAligned Pin
RubyPdf25-May-10 13:28
memberRubyPdf25-May-10 13:28 
please use this implement
_content.ShowTextAligned(Rectangle.ALIGN_CENTER, headerText, writer.PageSize.Width / 2, writer.PageSize.Height - 10, 0);

to replace the following implement,
_content.SetTextMatrix(GetCenterTextPosition(headerText, writer), writer.PageSize.Height - 10);

GeneralInformative article Pin
suraj mehare2-Mar-10 7:06
membersuraj mehare2-Mar-10 7:06 
GeneralRe: Informative article Pin
Michael Ulmann2-Mar-10 19:02
memberMichael Ulmann2-Mar-10 19:02 
QuestionHow to bookmark by each PDF creation time Pin
orlandito2030-Apr-09 22:08
memberorlandito2030-Apr-09 22:08 
QuestionHow to bookmark by each PDF file name Pin
Saam_cse27-Nov-08 22:49
memberSaam_cse27-Nov-08 22:49 
AnswerRe: How to bookmark by each PDF file name Pin
Michael Ulmann1-Dec-08 11:25
memberMichael Ulmann1-Dec-08 11:25 
GeneralRe: How to bookmark by each PDF file name Pin
Saam_cse1-Dec-08 23:23
memberSaam_cse1-Dec-08 23:23 
GeneralRe: How to bookmark by each PDF file name Pin
friendmastor25-Aug-09 15:24
memberfriendmastor25-Aug-09 15:24 
QuestionHow to pass arguments Pin
Väinölä Harri1-Aug-08 3:10
memberVäinölä Harri1-Aug-08 3:10 
AnswerRe: How to pass arguments Pin
Michael Ulmann1-Aug-08 14:38
memberMichael Ulmann1-Aug-08 14:38 
GeneralRe: How to pass arguments Pin
Väinölä Harri3-Aug-08 20:02
memberVäinölä Harri3-Aug-08 20:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170518.1 | Last Updated 17 Nov 2014
Article Copyright 2008 by Michael Ulmann
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid