.comment-link {margin-left:.6em;}

kHSw

Friday, April 07, 2006

Merge PDF Files using 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.

The code of the class I've written uses iText# and is based on the example code (Console Application) that can be found on
http://itextsharp.sourceforge.net/examples/Concat.cs . However, this code seems to target an out of date version of iText# and can't be compiled without fixing some lines...

This C#-class will allow you to merge multiple PDF's to one big PDF-file:

// Based on : http://itextsharp.sourceforge.net/examples/Concat.cs
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class PdfMerge
{
public static void MergeFiles(string destinationFile, string[] sourceFiles)
{
try
{
int f = 0;
// we create a reader for a certain document
PdfReader reader = new PdfReader(sourceFiles[f]);
// we retrieve the total number of pages
int n = reader.NumberOfPages;
Console.WriteLine("There are " + n + " pages in the original file.");
// step 1: creation of a document-object
Document document = new Document(reader.GetPageSizeWithRotation(1));
// step 2: we create a writer that listens to the document
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(destinationFile, FileMode.Create));
// step 3: we open the document
document.Open();
PdfContentByte cb = writer.DirectContent;
PdfImportedPage page;
int rotation;
// step 4: we add content
while (f < sourceFiles.Length)
{
int i = 0;
while (i < n)
{
i++;
document.SetPageSize(reader.GetPageSizeWithRotation(i));
document.NewPage();
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);
}
Console.WriteLine("Processed page " + i);
}
f++;
if (f < sourceFiles.Length)
{
reader = new PdfReader(sourceFiles[f]);
// we retrieve the total number of pages
n = reader.NumberOfPages;
Console.WriteLine("There are " + n + " pages in the original file.");
}
}
// step 5: we close the document
document.Close();
}
catch(Exception e)
{
Console.Error.WriteLine(e.Message);
Console.Error.WriteLine(e.StackTrace);
}
}
}

25 Comments:

  • Very good piece of code.

    By Anonymous Anonymous, at 1:19 PM  

  • Thanks. That was helpful

    By Anonymous Anonymous, at 9:14 PM  

  • Just a note, you're missing || in one of your if statements and I think your while loop should be while(i <= n)

    By Anonymous Anonymous, at 5:45 PM  

  • Simply awesome. Life saver.

    By Anonymous Anonymous, at 1:06 AM  

  • Just a note, you're missing || in one of your if statements and I think your while loop should be while(i <= n)


    a || is missing. That is the only issue. Two wrongs don't make a right.

    By Anonymous Anonymous, at 9:49 PM  

  • This comment has been removed by the author.

    By Blogger Unknown, at 6:36 PM  

  • Thanks for the code you saved a lot of time.
    I found somethin. looks like this
    "reader = new PdfReader(sourceFiles[f]);"
    must be added inside the while loop.

    Thanks for your code again.

    By Anonymous Anonymous, at 6:42 PM  

  • Very useful for me, thanks.
    However, when I'm calling the function from a button_click event from an asp.net/c# page(modified the code as per need),I'm gettin' error as 'Object reference not set to an instance of an object'.

    I know it's not the code's fault, but I'm not finding my error. I'm a novice, would you please help?

    By Blogger Dexter, at 11:24 AM  

  • Your code is really useful but I must find a way of merging PDF documents together without each one being on a new page. Each separate PDF document needs to be in a new paragraph rather than starting a new page. Any help will be gratefully received.

    By Blogger Angleterre2007Erasmus, at 11:52 AM  

  • cool stuff,thx!

    By Anonymous Anonymous, at 8:19 PM  

  • This code is awesome, very helpful. The only issue i seem to be having is that i have arcofields in my documents that i am join together, and i want to keep them editable. By merging them using this method it creates a flat file.

    By Anonymous Anonymous, at 7:36 AM  

  • thanks might

    By Anonymous Anonymous, at 11:20 AM  

  • this is great thank you so much!

    By Anonymous Anonymous, at 5:28 AM  

  • Excellent job.
    Here is a modified version allowing you to insert a pdf instead of appending it.


    public static void InsertPDFFile(string destinationFile, string fileA, string fileB, int insertAfter)
    {
    int f = 0;
    // we create a reader for a certain document
    PdfReader readerA = new PdfReader(fileA);
    PdfReader readerB = new PdfReader(fileB);

    // we retrieve the total number of pages
    int nA = readerA.NumberOfPages;
    int nB = readerB.NumberOfPages;

    // step 1: creation of a document-object
    iTextSharp.text.Document document = new iTextSharp.text.Document(readerA.GetPageSizeWithRotation(1));

    // step 2: we create a writer that listens to the document
    PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(destinationFile, FileMode.Create));
    // step 3: we open the document
    document.Open();
    PdfContentByte cb = writer.DirectContent;
    PdfImportedPage page;
    int rotation;
    // step 4: we add content
    int i = 0;
    int j = 0;
    while (i < nA)
    {


    if (i == insertAfter)
    {
    while (j < nB)
    {
    j++;
    document.SetPageSize(readerB.GetPageSizeWithRotation(j));
    document.NewPage();
    page = writer.GetImportedPage(readerB, 1);

    rotation = readerB.GetPageRotation(j);
    if (rotation == 90 || rotation == 270)
    {
    cb.AddTemplate(page, 0, -1f, 1f, 0, 0, readerB.GetPageSizeWithRotation(j).Height);
    }
    else
    {
    cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
    }

    }
    }

    i++;
    document.SetPageSize(readerA.GetPageSizeWithRotation(i));
    document.NewPage();

    page = writer.GetImportedPage(readerA, i);

    rotation = readerA.GetPageRotation(i);
    if (rotation == 90 || rotation == 270)
    {
    cb.AddTemplate(page, 0, -1f, 1f, 0, 0, readerA.GetPageSizeWithRotation(i).Height);
    }
    else
    {
    cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
    }

    }

    // step 5: we close the document
    document.Close();

    }

    By Blogger Virgil Hretcanu, at 12:55 AM  

  • Hi, Virgil Hretcanu.
    i did ur code and it works but the thing is how do i insert the pdf file to the code and where do i get output ?
    i need help, thanks! =)

    By Anonymous Anonymous, at 9:05 AM  

  • Actually the merged file doesnot shows the datas which i had inserted on each pdf file..I had filled the each pdf form using acrofield concept.Single files has been created successfully with datas,But when i merge the datas are not displayed.Almost i had tried all the methods in the WEB. ANy ideas on this..Why the merged file doesnot shows he data?...Any ideas share it mates...

    By Blogger vijay, at 8:55 AM  

  • Anyone has got a solution for vb.net?

    By Blogger Johnny Cache, at 3:12 AM  

  • Of course this is a value able code, can you please let me know that why Table of content (if one of merging files list contain TOC) got remove after file merging?
    But Adobe Acrobat not remove Table of content during merging ?

    By Blogger Muhammad Faisal Rana, at 7:23 AM  

  • wow, super. I needed 20min and all was o.k. Before I searched for 3 hours!

    By Anonymous Anonymous, at 9:29 PM  

  • Hi kHSw.

    Thanks for the very useful piece of code.

    This code merges my PDF files. But I amgetting a major problem.
    The hyperlinks and navigations created in the PDF using SetOutlines() and SetLocalGoTo() are lost when you merge two files.

    Can you please suggest any chnage I can make to preserve the links while merging

    By Blogger Lingayat Marriage Admin, at 11:08 AM  

  • Hi can any one help me with this.

    I want a PDF where I am able to print the column heading on each page

    When we normally print the PDf file using a datatable it prints the column header in first page only, rest pages only have data and not column heading.

    Regards

    By Blogger Gajendra, at 12:15 PM  

  • Hello Everyone,

    the code is great but I've a little problem.
    I'm runnin this code for a large number of pdf (more than 500) and after a long wait somenthing crash specially when document.close() start working.
    Someone has a solution for it?
    Thank you in advance

    By Blogger Andy, at 3:55 PM  

  • can someone help me with this? how do I use this class?

    By Anonymous Anonymous, at 4:21 PM  

  • Thank you very much, this is a great example and works well. This is helping me both in the doing and the learning.

    By Anonymous Anonymous, at 10:53 PM  

  • Can you please tell me how to merge two documents to a letter size if tehy are of different size. Please help me asap...

    By Anonymous Anonymous, at 8:31 PM  

Post a Comment

<< Home


 
Stefanie Worm is het liefste vrouwtje van de wereld.
Melina is de liefste schatsie van de wereld (Erik De Maeyer).