1: #region Using Directives
2: 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: #endregion
11: namespace ConcatenatePDF
12: { /// <summary>
13: /// PDFOpertaions class holds the methods to perform various tasks as of now
14: /// PDFOperations class have the method for concatenate the PDF file into a single pdf file.
15: /// </summary>
16: public class PDFOperations
17: {
18: /// <summary>
19: /// This Method can be used for concatenating the more than on pdf file into a single
20: /// pdf file.
21: /// </summary>
22: /// <param name="sourcePdFfiles">This string arrays will hold the filenames along with the path to be
23: /// concatenated
24: /// </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: try
34: {
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: } else
49: {
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: }