Blog

 
1:   using System;
2:   using System.IO;
3:  
4:   using iTextSharp.text;
5:   using iTextSharp.text.pdf;
6:  
7:   namespace iTextSharp.tutorial.Chap09
8:   {
9:       /// <summary>
10:       /// FontFactory
11:       /// </summary>
12:       public class Chap0906 
13:       {
14:           public Chap0906() 
15:           {
16:           
17:               Console.WriteLine("Chapter 9 example 6: FontFactory");
18:           
19:               FontFactory.Register("c:\\winnt\\fonts\\comicbd.ttf");
20:               FontFactory.Register("c:\\winnt\\fonts\\comic.ttf");
21:               FontFactory.Register("c:\\winnt\\fonts\\msgothic.ttc");
22:               Console.WriteLine("These fonts were registered at the FontFactory");
23:               foreach(string fontName in FontFactory.RegisteredFonts
24:               {
25:                   Console.WriteLine(fontName);
26:               }
27:           
28:               // step 1: creation of a document-object
29:               Document document new Document();
30:           
31:               try 
32:               {
33:                   // step 2:
34:                   // we create a writer that listens to the document
35:                   // and directs a PDF-stream to a file
36:                   PdfWriter.GetInstance(documentnew FileStream("Chap0906.pdf"FileMode.Create));
37:               
38:                   // step 3: we open the document
39:                   document.Open();
40:               
41:                   // step 4: we add content to the document
42:                   Font font0 FontFactory.GetFont(BaseFont.HELVETICABaseFont.WINANSI12);
43:                   String text0 "This is the quite popular built in font '" BaseFont.HELVETICA "'.";
44:                   document.Add(new Paragraph(text0font0));
45:                   Font font1 FontFactory.GetFont("ComicSansMS"BaseFont.WINANSI12);
46:                   String text1 "This is the quite popular True Type font 'ComicSansMS'.";
47:                   document.Add(new Paragraph(text1font1));
48:                   Font font2 FontFactory.GetFont("ComicSansMS-Bold"BaseFont.WINANSI12);
49:                   String text2 "This is the quite popular True Type font 'ComicSansMS-Bold'.";
50:                   document.Add(new Paragraph(text2font2));
51:                   Font font3FontFactory.GetFont("MS-PGothic"BaseFont.IDENTITY_HBaseFont.EMBEDDED12);
52:                   String text3 "\u5951\u7d04\u8005\u4f4f\u6240\u30e9\u30a4\u30f3\uff11";
53:                   document.Add(new Paragraph(text3font3));
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:   }