Blog

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