Merge pdf files using C#

October 3rd, 2008 by jf | Filed under .NET, C#, Work.

Recently I had to create an assembly component using C# to merge multiple PDF files into one file. The specification was pretty straight forward:
1) Merge two of more PDF document into a single output PDF File.
2) Used on an ASP.NET Page

After a few minutes of GOOG, I came up to this “ITextSharp

iText# (iTextSharp) is a port of the iText open source java library written entirely in C# for the .NET platform. iText# is a library that allows you to generate PDF files on the fly. It is implemented as an assembly.

It must be noted that the assembly is coded and compiled using the .NET Framework 1.1. You might want to migrate to the version 2.0 or 3.5 of the .NET Framework.

With a some more research on PDF merging. I was able to create a class that will make use of the ITextSharp assembly and perform as the merge pdf operation as needed.

Here is the code:

?View Code CSHARP
 
using iTextSharp.text;
using iTextSharp.text.pdf;
 
public class MergeEx
{
#region Fields
private string sourcefolder;
private string destinationfile;
private IList fileList = new ArrayList();
#endregion
 
#region Public Methods
///
/// Add a new file, together with a given docname to the fileList and namelist collection
///
///  public void AddFile(string pathnname)
{
fileList.Add(pathnname);
}
 
///
/// Generate the merged PDF
///
public void Execute()
{
MergeDocs();
}
#endregion
 
#region Private Methods
///
/// Merges the Docs and renders the destinationFile
///
///  ///  private void MergeDocs()
{
 
//Step 1: Create a Docuement-Object
Document document = new Document();
try
{
//Step 2: we create a writer that listens to the document
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(destinationfile, FileMode.Create));
 
//Step 3: Open the document
document.Open();
 
PdfContentByte cb = writer.DirectContent;
PdfImportedPage page;
 
int n = 0;
int rotation = 0;
 
//Loops for each file that has been listed
foreach (string filename in fileList)
{
//The current file path
string filePath = sourcefolder + filename;
 
// we create a reader for the document
PdfReader reader = new PdfReader(filePath);
 
//Gets the number of pages to process
n = reader.NumberOfPages;
 
int i = 0;
while (i < n)
{
i++;
document.SetPageSize(reader.GetPageSizeWithRotation(1));
document.NewPage();
 
//Insert to Destination on the first page
if (i == 1)
{
Chunk fileRef = new Chunk(" ");
fileRef.SetLocalDestination(filename);
document.Add(fileRef);
}
 
page = writer.GetImportedPage(reader, i);
rotation = reader.GetPageRotation(i);
if (rotation == 90 || rotation == 270)
{
cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
}
else
{
cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
}
}
}
catch (Exception e) { throw e; }
finally { document.Close(); }
}
#endregion
 
#region Properties
///
/// Gets or Sets the SourceFolder
///
public string SourceFolder
{
get { return sourcefolder; }
set {sourcefolder = value; }
}
 
///
/// Gets or Sets the DestinationFile
///
public string DestinationFile
{
get { return destinationfile; }
set { destinationfile = value; }
}
#endregion
}

To use the MergeEx class:
1) Initialize the class
2) Set the SourceFolder and DestinationFile properties
3) Using the AddFile method, add the source file names that need to be merged (Filename only since the SourceFolder has already been set)
4) Call the Execute Method

If everything works fine you will find your Merged PDF Document at your stated destination file.

The code is pretty much self decribed. If there is any question, i can always be contacted via this post.

Tags: , ,

15 Responses to “Merge pdf files using C#”

  1. Jaime | 31/10/08

    hello, i am using this code and the result pdf file have other size. This to many large.

    you know it can happen

  2. jf | 1/11/08

    This can be possible. Since i think the PDF document is “spitted” converted in some other format and then when the new document is created inserted in it.

    Can you please specify me what is the content of the document in question that is becoming larger that the original file ?

  3. Masri Yousef | 15/01/09

    Hi
    Than’s for this code , i’m not profisional programmer and
    i’m sorry for my bad english
    i have aproblem when i use Execute Method
    my initialize code :
    MergeEx mr = new MergeEx();
    mr.SourceFolder = Server.MapPath(”Document/”);
    mr.AddFile(Server.MapPath(”Document/5.pdf”));
    mr.AddFile(Server.MapPath(”Document/5.pdf”));
    mr.Execute();
    and i get error message like this :
    Exception Details: System.ArgumentNullException: Path cannot be null.
    Parameter name: path

    Source Error:

    Line 108: }
    Line 109: catch (Exception e)
    Line 110: { throw e; }
    Line 111: finally
    Line 112: { document.Close(); }

    wht’s wrong with my path

  4. jf | 15/01/09

    Make sure that your source folder and destination folder are properly registered and that they exist.

  5. levstik | 4/02/09

    try with
    mr.SourceFolder = Server.MapPath(”Document”);

    without a slash at the end

  6. npriddy | 5/02/09

    when ever i use this code to merge 2 pdf’s i’m returned blank pages. Does adobe not support templates anymore?

  7. jf | 5/02/09

    When a blank PDF document is returned, normally it means that something went wrong and the merging did not succeed. Did you get any Exceptions ? What do you mean by Template ?

  8. Franco T. Robles | 25/03/09

    Great contribution, the class worked fine with me.

  9. PDFBinder | 14/06/09

    For more inspiration on how to deal with merging og PDF documents using iTextSharp, have a look at the open-source PDFBinder project: http://code.google.com/p/pdfbinder/

  10. Nassos.NET | 2/07/09

    Hello there,
    is it possible to create a document in a MemoryStream instead of a File?
    Thnx in advance
    Nassos.

  11. jf | 2/07/09

    //Step 2: we create a writer that listens to the document
    PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(destinationfile, FileMode.Create));

    PdfWriter.GetInstance takes a stream as parameter, you can just change the FileStream to memory stream here and this should work.

  12. Dc | 29/07/09

    Awesome!

    Works like a charm.
    Great work. Thanks.

  13. Balaji Birajdar | 25/11/09

    This is a excellent piece of code. It works fine except a issue specific to my PDFs. I have some hyperlinks in my PDF. Those links do not work when I merge the PDF using this code.

    Any thoughts on this?

  14. flower | 13/01/10

    hi ,i work in a class with itexsharp.
    when the dll works in my machine it merges pdf files very well ,but when it work in a server doesn’t merge.Does the server need a package of c# distributable? or i have problems with Permissions for write? what problems can be succeced? any ideas?note: didn’t exists excepcions

  15. jf | 19/01/10

    Hi flower,

    You should install the .NET framework 1.0/2.0 on your server and it should work. I have a compiled version of ITextSharp for the .net framework 2.0, did you recompile it in 2.0 ?

Share Your Thoughts