1: | using System; | |
2: | using System.IO; | |
3: | ||
4: | using iTextSharp.text; | |
5: | using iTextSharp.text.pdf; | |
6: | ||
7: | ||
8: | namespace iTextSharp.tutorial.Chap01 | |
9: | { | |
10: | /// <summary> | |
11: | /// Handout | |
12: | /// </summary> | |
13: | public class Handout | |
14: | { | |
15: | public Handout(string[] args) | |
16: | { | |
17: | if (args.Length != 3) | |
18: | { | |
19: | Console.Error.WriteLine("This tools needs 3 parameters:\njava Handout srcfile destfile pages"); | |
20: | } | |
21: | else | |
22: | { | |
23: | try | |
24: | { | |
25: | int pages = int.Parse(args[2]); | |
26: | if (pages < 2 || pages > 8) | |
27: | { | |
28: | throw new DocumentException("You can't have " + pages + " pages on one page (minimum 2; maximum 8)."); | |
29: | } | |
30: | ||
31: | float x1 = 30f; | |
32: | float x2 = 280f; | |
33: | float x3 = 320f; | |
34: | float x4 = 565f; | |
35: | ||
36: | float[] y1 = new float[pages]; | |
37: | float[] y2 = new float[pages]; | |
38: | ||
39: | float height = (778f - (20f * (pages - 1))) / pages; | |
40: | y1[0] = 812f; | |
41: | y2[0] = 812f - height; | |
42: | ||
43: | for (int i = 1; i < pages; i++) | |
44: | { | |
45: | y1[i] = y2[i - 1] - 20f; | |
46: | y2[i] = y1[i] - height; | |
47: | } | |
48: | ||
49: | // we create a reader for a certain document | |
50: | PdfReader reader = new PdfReader(args[0]); | |
51: | // we retrieve the total number of pages | |
52: | int n = reader.NumberOfPages; | |
53: | Console.WriteLine("There are " + n + " pages in the original file."); | |
54: | ||
55: | // step 1: creation of a document-object | |
56: | Document document = new Document(PageSize.A4); | |
57: | // step 2: we create a writer that listens to the document | |
58: | PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(args[1], FileMode.Create)); | |
59: | // step 3: we open the document | |
60: | document.Open(); | |
61: | PdfContentByte cb = writer.DirectContent; | |
62: | PdfImportedPage page; | |
63: | int rotation; | |
64: | int j = 0; | |
65: | int p = 0; | |
66: | // step 4: we add content | |
67: | while (j < n) | |
68: | { | |
69: | j++; | |
70: | Rectangle rect = reader.GetPageSizeWithRotation(j); | |
71: | float factorx = (x2 - x1) / rect.Width; | |
72: | float factory = (y1[p] - y2[p]) / rect.Height; | |
73: | float factor = (factorx < factory ? factorx : factory); | |
74: | float dx = (factorx == factor ? 0f : ((x2 - x1) - rect.Width * factor) / 2f); | |
75: | float dy = (factory == factor ? 0f : ((y1[p] - y2[p]) - rect.Height * factor) / 2f); | |
76: | page = writer.GetImportedPage(reader, j); | |
77: | rotation = reader.GetPageRotation(j); | |
78: | if (rotation == 90 || rotation == 270) | |
79: | { | |
80: | cb.AddTemplate(page, 0, -factor, factor, 0, x1 + dx, y2[p] + dy + rect.Height * factor); | |
81: | } | |
82: | else | |
83: | { | |
84: | cb.AddTemplate(page, factor, 0, 0, factor, x1 + dx, y2[p] + dy); | |
85: | } | |
86: | cb.SetRGBColorStroke(0xC0, 0xC0, 0xC0); | |
87: | cb.Rectangle(x3 - 5f, y2[p] - 5f, x4 - x3 + 10f, y1[p] - y2[p] + 10f); | |
88: | for (float l = y1[p] - 19; l > y2[p]; l -= 16) | |
89: | { | |
90: | cb.MoveTo(x3, l); | |
91: | cb.LineTo(x4, l); | |
92: | } | |
93: | cb.Rectangle(x1 + dx, y2[p] + dy, rect.Width * factor, rect.Height * factor); | |
94: | cb.Stroke(); | |
95: | Console.WriteLine("Processed page " + j); | |
96: | p++; | |
97: | if (p == pages) | |
98: | { | |
99: | p = 0; | |
100: | document.NewPage(); | |
101: | } | |
102: | } | |
103: | // step 5: we close the document | |
104: | document.Close(); | |
105: | } | |
106: | catch(Exception e) | |
107: | { | |
108: | Console.Error.WriteLine(e.Message); | |
109: | Console.Error.WriteLine(e.StackTrace); | |
110: | } | |
111: | } | |
112: | ||
113: | } | |
114: | } | |
115: | } |