I’m back with a helpful stuff for those people looking for concatenating the pdf files into a single consolidated pdf file. Also this piece of code exhibit use of iTextSharp open source library. You can get this library from below mentioned link.
iTextSharp Library
Ok, Lets start step by step
1) Download the library.
2) Create a Console application.
3) Add the reference to itextsharp.dll assembly.
4) Add below given code into appropriate files and add these files to your project.
5) Build this application.
6) Now you are set to go.
What this program do?
It a command line application which takes output pdf file name along with path as the first argument followed by list of the source file(i.e. source pdf files) names along with path for each file. On successful execution of the application all the pdf documents will get consolidated into output pdf file.
Program.cs
1: using System;2: namespace ConcatenatePDF3: {
4: /// <summary>5: ///6: /// </summary>7: class Program8: {
9: /// <summary>10: ///11: /// </summary>12: /// <param name="args"></param>13: static void Main(string[] args)14: {
15: if(args.Length>1)16: {
17: try18: {
19: string[] sourceFile = new string[args.GetUpperBound(0)];20: Array.Copy(args, 1, sourceFile, 0, args.GetUpperBound(0));
21: string strMsg=PDFOperations.ConcatenatePDF(sourceFile, args[0]);22: Console.WriteLine(strMsg);
23: Console.ReadKey();
24: } catch (Exception ex)25: {
26: Console.WriteLine(ex.Message);
27: }
28: }
29: }
30: }
31: }
PDFOperations.cs
1: #region Using Directives2: using System;3: using System.Collections.Generic;4: using System.IO;5: using System.Security;6: using System.Security.Permissions;7: using System.Text;8: using iTextSharp.text;9: using iTextSharp.text.pdf;10: #endregion11: namespace ConcatenatePDF12: { /// <summary>13: /// PDFOpertaions class holds the methods to perform various tasks as of now14: /// PDFOperations class have the method for concatenate the PDF file into a single pdf file.15: /// </summary>16: public class PDFOperations17: {
18: /// <summary>19: /// This Method can be used for concatenating the more than on pdf file into a single20: /// pdf file.21: /// </summary>22: /// <param name="sourcePdFfiles">This string arrays will hold the filenames along with the path to be23: /// concatenated24: /// </param>25: /// <param name="destinationPdfFile">Name of the output file along with path</param>26: public static String ConcatenatePDF(Array sourcePdFfiles, string destinationPdfFile)27: {
28: int numberOfPages;29: StringBuilder stringBuilder= new StringBuilder();30: List<string> srcPDFFileList = new List<string>();31: Document pdfDoc = new Document();32: FileIOPermission f2 = new FileIOPermission(FileIOPermissionAccess.Write | FileIOPermissionAccess.Read, destinationPdfFile);33: try34: {
35: f2.Demand();
36: PdfCopy copy = new PdfCopy(pdfDoc, new FileStream(destinationPdfFile, FileMode.CreateNew));37: pdfDoc.Open();
38: foreach (string file in sourcePdFfiles)39: {
40: if (File.Exists(file))41: {
42: PdfReader reader = new PdfReader(file);43: numberOfPages = reader.NumberOfPages;
44: for (int pageCount = 0; pageCount < numberOfPages; )45: {
46: copy.AddPage(copy.GetImportedPage(reader, ++pageCount));
47: }
48: } else49: {
50: stringBuilder.Append(String.Format("{0}: {1}", file, ": This file does not exists.\n"));51: }
52: }
53: pdfDoc.Close();
54: if (File.Exists(destinationPdfFile))55: {
56: stringBuilder.Append(String.Format("{0}: {1}", destinationPdfFile, ": created successfully.\n"));57: }
58: } catch (UnauthorizedAccessException s)59: {
60: stringBuilder.Append(String.Format(" Error Occurred :{0} \n", s.Message));61:
62: }
63: catch (SecurityException s)64: {
65: stringBuilder.Append(String.Format(" Error Occurred :{0} \n", s.Message));66: }
67: return stringBuilder.ToString();68: }
69: }
70: }
Please provide the feedback and revert for any clarification