1: | using System; | |
2: | using System.IO; | |
3: | using System.Diagnostics; | |
4: | ||
5: | using iTextSharp.text; | |
6: | using iTextSharp.text.pdf; | |
7: | ||
8: | ||
9: | namespace iTextSharp.tutorial.Chap01 | |
10: | { | |
11: | /// <summary> | |
12: | /// When creating a document, you can also define left, right, upper and lower margins: | |
13: | /// </summary> | |
14: | /// <remarks> | |
15: | /// Measurements | |
16: | /// When creating a rectangle or choosing a margin, you might wonder what measurement unit is used: centimeters, inches or pixels. In fact, the default measurement system roughly corresponds to the various definitions of the typographic unit of measurement known as the point. There are 72 points in 1 inch. | |
17: | /// If you want to create a rectangle in PDF that has the size of an A4-page, you have to calculate the number of points: | |
18: | /// 21 cm / 2.54 = 8.2677 inch | |
19: | /// 8.2677 * 72 = 595 points | |
20: | ///29.7 cm / 2.54 = 11.6929 inch | |
21: | ///11.6929 * 72 = 842 points | |
22: | ///The default border of 36 points corresponds with half an inch. | |
23: | /// </remarks> | |
24: | public class Chap0104 | |
25: | { | |
26: | public Chap0104() | |
27: | { | |
28: | Console.WriteLine("Chapter 1 example 4: Margins"); | |
29: | ||
30: | // step 1: creation of a document-object | |
31: | Document document = new Document(PageSize.A5, 36, 72, 108, 180); | |
32: | ||
33: | try | |
34: | { | |
35: | ||
36: | // step 2: | |
37: | // we create a writer that listens to the document | |
38: | // and directs a PDF-stream to a file | |
39: | ||
40: | PdfWriter.GetInstance(document, new FileStream("Chap0104.pdf", FileMode.Create)); | |
41: | ||
42: | // step 3: we open the document | |
43: | document.Open(); | |
44: | ||
45: | // step 4: we Add a paragraph to the document | |
46: | Paragraph paragraph = new Paragraph(); | |
47: | paragraph.Alignment = Element.ALIGN_JUSTIFIED; | |
48: | for (int i = 0; i < 20; i++) | |
49: | { | |
50: | paragraph.Add("Hello World, Hello Sun, Hello Moon, Hello Stars, Hello Sea, Hello Land, Hello People. "); | |
51: | } | |
52: | document.Add(paragraph); | |
53: | ||
54: | } | |
55: | catch(DocumentException de) | |
56: | { | |
57: | Console.Error.WriteLine(de.Message); | |
58: | } | |
59: | catch(IOException ioe) | |
60: | { | |
61: | Console.Error.WriteLine(ioe.Message); | |
62: | } | |
63: | ||
64: | // step 5: we close the document | |
65: | document.Close(); | |
66: | ||
67: | } | |
68: | } | |
69: | } |