Blog

 
1:   using System;
2:   using System.IO;
3:  
4:   using iTextSharp.text;
5:   using iTextSharp.text.xml;
6:  
7:   namespace iTextSharp.tutorial.Chap07
8:   {
9:       /// <summary>
10:       /// how a document is generated containing almost all the objects we discussed in the previous chapters
11:       /// </summary>
12:       public class Chap0701 
13:       {
14:       
15:           public  Chap0701() 
16:           {
17:           
18:               Console.WriteLine("Chapter 7 example 1: my first XML");
19:           
20:               // step 1: creation of a document-object
21:               Document document new Document();
22:           
23:               try 
24:               {
25:               
26:                   // step 2:
27:                   // we create a writer that listens to the document
28:                   // and directs a XML-stream to a file
29:                   XmlWriter.GetInstance(documentnew FileStream("Chap0701.xml"FileMode.Create), "itext.dtd");
30:               
31:                   // step 3: we open the document
32:                   document.Open();
33:               
34:                   // step 4: we add content to the document
35:                   Paragraph paragraph new Paragraph("Please visit my ");
36:                   Anchor anchor1 new Anchor("website (external reference)"FontFactory.GetFont(FontFactory.HELVETICA12Font.UNDERLINEnew Color(00255)));
37:                   anchor1.Reference "http://www.lowagie.com/iText/";
38:                   anchor1.Name "top";
39:                   paragraph.Add(anchor1);
40:                   document.Add(paragraph);
41:               
42:                   Paragraph entities new Paragraph("These are some special characters: <, >, &, \" and '");
43:                   document.Add(entities);
44:               
45:                   document.Add(new Paragraph("some books I really like:"));
46:                   List list;
47:                   ListItem listItem;
48:                   list new List(true15);
49:                   listItem new ListItem("When Harlie was one"FontFactory.GetFont(FontFactory.TIMES_ROMAN12));
50:                   listItem.Add(new Chunk(" by David Gerrold"FontFactory.GetFont(FontFactory.TIMES_ROMAN11Font.ITALIC)).SetTextRise(8.0f));
51:                   list.Add(listItem);
52:                   listItem new ListItem("The World according to Garp"FontFactory.GetFont(FontFactory.TIMES_ROMAN12));
53:                   listItem.Add(new Chunk(" by John Irving"FontFactory.GetFont(FontFactory.TIMES_ROMAN11Font.ITALIC)).SetTextRise(-8.0f));
54:                   list.Add(listItem);
55:                   listItem new ListItem("Decamerone"FontFactory.GetFont(FontFactory.TIMES_ROMAN12));
56:                   listItem.Add(new Chunk(" by Giovanni Boccaccio"FontFactory.GetFont(FontFactory.TIMES_ROMAN11Font.ITALIC)));
57:                   list.Add(listItem);
58:                   document.Add(list);
59:               
60:                   paragraph new Paragraph("some movies I really like:");
61:                   list new List(false10);
62:                   list.Add("Wild At Heart");
63:                   list.Add("Casablanca");
64:                   list.Add("When Harry met Sally");
65:                   list.Add("True Romance");
66:                   list.Add("Le mari de la coiffeuse");
67:                   paragraph.Add(list);
68:                   document.Add(paragraph);
69:               
70:                   document.Add(new Paragraph("Some authors I really like:"));
71:                   list new List(false20);
72:                   list.ListSymbol new Chunk("*"FontFactory.GetFont(FontFactory.HELVETICA20Font.BOLD));
73:                   listItem new ListItem("Isaac Asimov");
74:                   list.Add(listItem);
75:                   List sublist;
76:                   sublist new List(true10);
77:                   sublist.ListSymbol new Chunk(""FontFactory.GetFont(FontFactory.HELVETICA8));
78:                   sublist.Add("The Foundation Trilogy");
79:                   sublist.Add("The Complete Robot");
80:                   sublist.Add("Caves of Steel");
81:                   sublist.Add("The Naked Sun");
82:                   list.Add(sublist);
83:                   listItem new ListItem("John Irving");
84:                   list.Add(listItem);
85:                   sublist new List(true10);
86:                   sublist.ListSymbol new Chunk(""FontFactory.GetFont(FontFactory.HELVETICA8));
87:                   sublist.Add("The World according to Garp");
88:                   sublist.Add("Hotel New Hampshire");
89:                   sublist.Add("A prayer for Owen Meany");
90:                   sublist.Add("Widow for a year");
91:                   list.Add(sublist);
92:                   listItem new ListItem("Kurt Vonnegut");
93:                   list.Add(listItem);
94:                   sublist new List(true10);
95:                   sublist.ListSymbol new Chunk(""FontFactory.GetFont(FontFactory.HELVETICA8));
96:                   sublist.Add("Slaughterhouse 5");
97:                   sublist.Add("Welcome to the Monkey House");
98:                   sublist.Add("The great pianola");
99:                   sublist.Add("Galapagos");
100:                   list.Add(sublist);
101:                   document.Add(list);
102:               
103:                   paragraph new Paragraph("\n\n");
104:                   document.Add(paragraph);
105:               
106:                   Table table new Table(3);
107:                   table.BorderWidth 1;
108:                   table.BorderColor new Color(00255);
109:                   table.Padding 5;
110:                   table.Spacing 5;
111:                   Cell cell new Cell("header");
112:                   cell.Header true;
113:                   cell.Colspan 3;
114:                   table.AddCell(cell);
115:                   table.EndHeaders();
116:                   cell new Cell("example cell with colspan 1 and rowspan 2");
117:                   cell.Rowspan 2;
118:                   cell.BorderColor new Color(25500);
119:                   table.AddCell(cell);
120:                   table.AddCell("1.1");
121:                   table.AddCell("2.1");
122:                   table.AddCell("1.2");
123:                   table.AddCell("2.2");
124:                   table.AddCell("cell test1");
125:                   cell new Cell("big cell");
126:                   cell.Rowspan 2;
127:                   cell.Colspan 2;
128:                   table.AddCell(cell);
129:                   table.AddCell("cell test2");
130:                   document.Add(table);
131:               
132:                   Image jpeg Image.GetInstance("myKids.jpg");
133:                   document.Add(jpeg);
134:                   Image png Image.GetInstance(new Uri("http://www.lowagie.com/iText/examples/hitchcock.png"));
135:                   document.Add(png);
136:                   Anchor anchor2 new Anchor("please jump to a local destination"FontFactory.GetFont(FontFactory.HELVETICA12Font.NORMALnew Color(00255)));
137:                   anchor2.Reference "#top";
138:                   document.Add(anchor2);
139:               
140:                   document.Add(paragraph);
141:               
142:                   // we define some fonts
143:                   Font chapterFont FontFactory.GetFont(FontFactory.HELVETICA24Font.NORMALnew Color(25500));
144:                   Font sectionFont FontFactory.GetFont(FontFactory.HELVETICA20Font.NORMALnew Color(00255));
145:                   Font subsectionFont FontFactory.GetFont(FontFactory.HELVETICA18Font.BOLDnew Color(06464));
146:                   // we create some paragraphs
147:                   Paragraph blahblah new Paragraph("blah blah blah blah blah blah blaah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah");
148:                   Paragraph blahblahblah new Paragraph("blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blaah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah");
149:               
150:                   // this loop will create 7 chapters
151:                   for (int 18i++) 
152:                   {
153:                       Paragraph cTitle new Paragraph("This is chapter " ichapterFont);
154:                       Chapter chapter new Chapter(cTitlei);
155:                   
156:                       if (== 4
157:                       {
158:                           blahblahblah.Alignment Element.ALIGN_JUSTIFIED;
159:                           blahblah.Alignment Element.ALIGN_JUSTIFIED;
160:                           chapter.Add(blahblah);
161:                       }
162:                       if (== 5
163:                       {
164:                           blahblahblah.Alignment Element.ALIGN_CENTER;
165:                           blahblah.Alignment Element.ALIGN_RIGHT;
166:                           chapter.Add(blahblah);
167:                       }
168:                       // add a table in the 6th chapter
169:                       if (== 6
170:                       {
171:                           blahblah.Alignment Element.ALIGN_JUSTIFIED;
172:                           chapter.Add(table);
173:                       }
174:                       // in every chapter 3 sections will be added
175:                       for (int 14j++) 
176:                       {
177:                           Paragraph sTitle new Paragraph("This is section " " in chapter " isectionFont);
178:                           Section section chapter.AddSection(sTitle1);
179:                           // in all chapters except the 1st one, some extra text is added to section 3
180:                           if (== && 1
181:                           {
182:                               section.Add(blahblah);
183:                           }
184:                           // in every section 3 subsections are added
185:                           for (int 14k++) 
186:                           {
187:                               Paragraph subTitle new Paragraph("This is subsection " " of section " jsubsectionFont);
188:                               Section subsection section.AddSection(subTitle3);
189:                               if (== && == 3
190:                               {
191:                                   subsection.Add(blahblahblah);
192:                                   subsection.Add(table);
193:                               }
194:                               subsection.Add(blahblah);
195:                           }
196:                           if (== && 2
197:                           {
198:                               section.Add(blahblahblah);
199:                               section.Add(table);
200:                           }
201:                       }
202:                       document.Add(chapter);
203:                   }
204:               
205:               }
206:               catch(DocumentException de
207:               {
208:                   Console.Error.WriteLine(de.Message);
209:               }
210:               catch(IOException ioe
211:               {
212:                   Console.Error.WriteLine(ioe.Message);
213:               }
214:           
215:               // step 5: we close the document
216:               document.Close();
217:           }
218:       }
219:   }