You are here : Blog
Search_Blog Minimaliseren

KHSW Soft Blog Minimaliseren
Apr 7

Written by: Ken Huysmans
2006-04-07T11:32:07 

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);
}
}
}

Tags:

2 comment(s) so far...

Re: Merge PDF Files using iTextSharp

Hi,

I use PDF this tool to merge 2 PDF files from physical location, converted by crystal report. The file converted from crystal report is in Landscape orientaion.

When I use this tool to merge the 2 files into a single file, only the first page is displaying in Landscape and all the other pages are in portrait there by leaving the report useless. Since the contents are all getting cut.

What am I missing in here. I use Letter and not A4 size paper.

Any help is highly appreciated.

Regards,
Ramesh

By Ramesh on   2011-11-13T12:52:51

Re: Merge PDF Files using iTextSharp

Nice, inspiring peace of code! Thank you! I have written a simple library to achieve things like merge, split, convert images to pdf's- based on iTextSharp. You can download the source here: blog.mecum.biz/2011/11/how-master-xml-and-mistress-xsd-helped-itextsharp-out-of-the-claws-of-hippi-o-cratic-chaos-huggermugger/ - pure open source.

By Ilija Injac on   2011-11-13T12:53:20

Your name:
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
Enter the code shown above in the box below
Add Comment   Annuleren 


Copyright 2007 by KHSW Soft BVBA
PrivacybeleidGebruiksovereenkomst