1: | using System; | |
2: | using System.IO; | |
3: | ||
4: | using iTextSharp.text; | |
5: | using iTextSharp.text.pdf; | |
6: | ||
7: | ||
8: | ||
9: | namespace iTextSharp.tutorial.Chap01 | |
10: | { | |
11: | /// <summary> | |
12: | /// Concat,PDFMerge | |
13: | /// </summary> | |
14: | public class Concat | |
15: | { | |
16: | public Concat(String[] args) | |
17: | { | |
18: | if (args.Length < 3) | |
19: | { | |
20: | Console.Error.WriteLine("This tools needs at least 3 parameters:\njava Concat destfile file1 file2 [file3 ...]"); | |
21: | } | |
22: | else | |
23: | { | |
24: | try | |
25: | { | |
26: | int f = 1; | |
27: | // we create a reader for a certain document | |
28: | PdfReader reader = new PdfReader(args[f]); | |
29: | // we retrieve the total number of pages | |
30: | int n = reader.NumberOfPages; | |
31: | Console.WriteLine("There are " + n + " pages in the original file."); | |
32: | ||
33: | // step 1: creation of a document-object | |
34: | Document document = new Document(reader.GetPageSizeWithRotation(1)); | |
35: | // step 2: we create a writer that listens to the document | |
36: | PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(args[0], FileMode.Create)); | |
37: | // step 3: we open the document | |
38: | document.Open(); | |
39: | PdfContentByte cb = writer.DirectContent; | |
40: | PdfImportedPage page; | |
41: | int rotation; | |
42: | // step 4: we add content | |
43: | while (f < args.Length) | |
44: | { | |
45: | int i = 0; | |
46: | while (i < n) | |
47: | { | |
48: | i++; | |
49: | document.SetPageSize(reader.GetPageSizeWithRotation(i)); | |
50: | document.NewPage(); | |
51: | page = writer.GetImportedPage(reader, i); | |
52: | rotation = reader.GetPageRotation(i); | |
53: | if (rotation == 90 || rotation == 270) | |
54: | { | |
55: | cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height); | |
56: | } | |
57: | else | |
58: | { | |
59: | cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0); | |
60: | } | |
61: | Console.WriteLine("Processed page " + i); | |
62: | } | |
63: | f++; | |
64: | if (f < args.Length) | |
65: | { | |
66: | reader = new PdfReader(args[f]); | |
67: | // we retrieve the total number of pages | |
68: | n = reader.NumberOfPages; | |
69: | Console.WriteLine("There are " + n + " pages in the original file."); | |
70: | } | |
71: | } | |
72: | // step 5: we close the document | |
73: | document.Close(); | |
74: | } | |
75: | catch(Exception e) | |
76: | { | |
77: | Console.Error.WriteLine(e.Message); | |
78: | Console.Error.WriteLine(e.StackTrace); | |
79: | } | |
80: | } | |
81: | ||
82: | } | |
83: | } | |
84: | } |