Blog

 
1:   using System;
2:   using System.IO;
3:  
4:   using iTextSharp.text;
5:   using iTextSharp.text.pdf;
6:  
7:  
8:   namespace iTextSharp.tutorial.Chap02
9:   {
10:       /// <summary>
11:       /// Type 1 fonts
12:       /// </summary>
13:       public class Chap0201
14:       {
15:           public Chap0201()
16:           {
17:               Console.WriteLine("Chapter 2 example 1: Chunks and fonts");
18:           
19:               // step 1: creation of a document-object
20:               Document document = new Document();
21:           
22:               try 
23:               {
24:               
25:                   // step 2:
26:                   // we create a writer that listens to the document
27:                   // and directs a PDF-stream to a file
28:                   PdfWriter.GetInstance(document, new FileStream("Chap0201.pdf",FileMode.Create));
29:               
30:                   // step 3: we open the document
31:                   document.Open();
32:               
33:                   // step 4: we Add content to the document
34:                   Font[] fonts = new Font[14];
35:                   fonts[0] = FontFactory.GetFont(FontFactory.COURIER, 12, Font.NORMAL);
36:                   fonts[1] = FontFactory.GetFont(FontFactory.COURIER, 12, Font.BOLD);
37:                   fonts[2] = FontFactory.GetFont(FontFactory.COURIER, 12, Font.ITALIC);
38:                   fonts[3] = FontFactory.GetFont(FontFactory.COURIER, 12, Font.BOLD | Font.ITALIC);
39:                   fonts[4] = FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.NORMAL);
40:                   fonts[5] = FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.BOLD);
41:                   fonts[6] = FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.ITALIC);
42:                   fonts[7] = FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.BOLD | Font.ITALIC);
43:                   fonts[8] = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12, Font.NORMAL);
44:                   fonts[9] = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12, Font.BOLD);
45:                   fonts[10] = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12, Font.ITALIC);
46:                   fonts[11] = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12, Font.BOLD | Font.ITALIC);
47:                   fonts[12] = FontFactory.GetFont(FontFactory.SYMBOL, 12, Font.NORMAL);
48:                   fonts[13] = FontFactory.GetFont(FontFactory.ZAPFDINGBATS, 12, Font.NORMAL);
49:                   for (int i = 0; i < 14; i++) 
50:                   {
51:                       Chunk chunk = new Chunk("This is some", fonts[i]);
52:                       document.Add(new Phrase(chunk));
53:                       document.Add(new Phrase(new Chunk(" font. ",
54:                           fonts[i]).SetTextRise((i % 2 == 0) ? -6 : 6)));
55:                   }
56:                   document.Add(new Phrase(new Chunk("This text is underlined",
57:                       FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.UNDERLINE))));
58:                   document.Add(new Phrase(new Chunk("This font is of type ITALIC | STRIKETHRU",
59:                       FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.ITALIC | Font.STRIKETHRU))));
60:                   Chunk ck = new Chunk("This text has a yellow background color", FontFactory.GetFont(FontFactory.HELVETICA, 12));
61:                   ck.SetBackground(new Color(0xFF, 0xFF, 0x00));
62:                   document.Add(new Phrase(ck));
63:               }
64:               catch(DocumentException de) 
65:               {
66:                   Console.Error.WriteLine(de.Message);
67:               }
68:               catch(IOException ioe) 
69:               {
70:                   Console.Error.WriteLine(ioe.Message);
71:               }
72:           
73:               // step 5: we close the document
74:               document.Close();
75:  
76:           }
77:       }
78:   }