Blog

 
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 || 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 1pagesi++) 
44:                       {
45:                           y1[i] = y2[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 reader.NumberOfPages;
53:                       Console.WriteLine("There are " " 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(documentnew 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 0;
65:                       int 0;
66:                       // step 4: we add content
67:                       while (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(readerj);
77:                           rotation reader.GetPageRotation(j);
78:                           if (rotation == 90 || rotation == 270
79:                           {
80:                               cb.AddTemplate(page0, -factorfactor0x1 dxy2[p] + dy rect.Height factor);
81:                           }
82:                           else 
83:                           {
84:                               cb.AddTemplate(pagefactor00factorx1 dxy2[p] + dy);
85:                           }
86:                           cb.SetRGBColorStroke(0xC00xC00xC0);
87:                           cb.Rectangle(x3 5fy2[p] - 5fx4 x3 10fy1[p] - y2[p] + 10f);
88:                           for (float y1[p] - 19y2[p]; -= 16
89:                           {
90:                               cb.MoveTo(x3l);
91:                               cb.LineTo(x4l);
92:                           }
93:                           cb.Rectangle(x1 dxy2[p] + dyrect.Width factorrect.Height factor);
94:                           cb.Stroke();
95:                           Console.WriteLine("Processed page " j);
96:                           p++;
97:                           if (== pages
98:                           {
99:                               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:   }