What about Creativity ?
Merge pdf files using C#
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.
[ad] Empty ad slot (#1)!
Here is the code:
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.
| Print article | This entry was posted by jf on October 3, 2008 at 4:00 pm, and is filed under .NET, C#, Software. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 4 months ago
re: http://www.wacdesigns.com/2008/10/03/merge-pdf-files-using-c/
Thought I’d share a variation of your code I created. Given my code (below under ‘Extension Method Code’), you can do things like:
var di = new DirectoryInfo( @”F:\DocGen\PDF” );
using( var fs = new FileStream( @”F:\DocGen\batch.pdf”, FileMode.Create ) )
{
di.MergePDFs( fs );
}
using( var fs = new FileStream( @”F:\DocGen\batch two.pdf”, FileMode.Create ) )
{
di.GetFiles().Where( f => f.Name.Contains( “122″ ) ).MergePDFs( fs );
}
var pdfFileContents = (IEnumerable)null; // get content files from db
using ( var zip = new ZipOutputStream( target ) ) // use any stream type you want
{
var entry = new ZipEntry( “Batch.pdf” );
zip.PutNextEntry( entry );
pdfFileContents.MergePDFs( zip );
}
Extension method code:
class PdfConvertFile
{
public string FileName { get; set; }
public byte[] Content { get; set; }
}
public static class ExtensionMethods
{
public static void MergePDFs( this DirectoryInfo source, Stream outputStream )
{
MergePDFs( source.GetFiles( “*.pdf” ), outputStream );
}
public static void MergePDFs( this IEnumerable files, Stream putputStream )
{
MergePDFs( files.Select( f => new PdfConvertFile { FileName = f.FullName } ), outputStream );
}
public static void MergePDFs( this IEnumerable files, Stream outputStream )
{
MergePDFs( files.Select( f => new PdfConvertFile { Content = f } ), outputStream );
}
private static void MergePDFs( this IEnumerable files, Stream outputStream )
{
var document = new iTextSharp.text.Document();
try
{
//Create a writer that listens to the document
PdfWriter writer = PdfWriter.GetInstance( document, outputStream );
//Open the document
document.Open();
var cb = writer.DirectContent;
PdfImportedPage page;
int n = 0;
int rotation = 0;
//Loops for each file that has been listed
foreach ( var file in files )
{
// we create a reader for the document
PdfReader reader = !string.IsNullOrEmpty( file.FileName )
? new PdfReader( file.FileName )
: new PdfReader( file.Content );
//Gets the number of pages to process
n = reader.NumberOfPages;
int i = 0;
while ( i < n )
{
i++;
document.SetPageSize( reader.GetPageSizeWithRotation( 1 ) );
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 );
}
}
}
}
finally { document.Close(); }
}
}
about 3 months ago
I have tried merging the pdfs to letter size.
The first was merged exactly to letter size. But the second document which was smallet than letter size didn’t get merged to letter size.
Can you please let me know how to merge pdfs to letter size irrespective of their size and content. please reply. Thanks in advance
about 2 months ago
Hi, Where can I find the MergerDocs method
about 1 month ago
I am trying to merge 2 pdf file and result will be a new pdf. But content of 1st pdf file copy on second pdf file and content of second pdf file get removed.
can any one help ?
thanks in advance.
Praveen Kumar
E-Bix software Pvt. Ltd.
about 1 month ago
@ Santosh check line 034.
about 1 month ago
good work but when I run it I keep getting an unarthorized permission exception, also for the addfile methood is this right.
string sourcefolder= _txtEnterSource.Text;
string destinationfile= _txtdestinationfile.Text;
string pathnname = _txtPathName.Text;
string pathname2 = _txtPathname2.Text;
MergeEx m = new MergeEx();
m.SourceFolder = sourcefolder;
m.DestinationFile = destinationfile;
m.AddFile(pathnname);
m.AddFile(pathname2);
m.Execute();
about 4 weeks ago
@Praveen: can you provide sample of your code and the PDF you are trying to merge, i’ll give it a try.
@Christopher: Please make sure that the source/destination folder has the the proper rights.
about 4 weeks ago
@Daniel, I got some issue, trying to do that too, with some big images in a PDF file, i haven’t looked into it, since the PDF i had to merge, were all standard formats. I think you will need to determine the size and then do the merging.
about 4 weeks ago
Very very GOOD!!!!!!