Chapter 11: Choosing the right font

This is a code example of iText PDF, discover more.

10th October 2015
admin-marketing

Switch code for this example

FontTypes.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part3.chapter11;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.Document;
  14. import com.itextpdf.text.DocumentException;
  15. import com.itextpdf.text.Font;
  16. import com.itextpdf.text.Paragraph;
  17. import com.itextpdf.text.pdf.BaseFont;
  18. import com.itextpdf.text.pdf.PdfWriter;
  19. import com.itextpdf.text.pdf.draw.LineSeparator;
  20.  
  21. public class FontTypes {
  22.  
  23.     /** The resulting PDF file. */
  24.     public static String RESULT
  25.         = "results/part3/chapter11/font_types.pdf";
  26.     /** Some text. */
  27.     public static String TEXT
  28.         = "quick brown fox jumps over the lazy dog\nQUICK BROWN FOX JUMPS OVER THE LAZY DOG";
  29.    
  30.     /** Paths to and encodings of fonts we're going to use in this example */
  31.     public static String[][] FONTS = {
  32.         {BaseFont.HELVETICA, BaseFont.WINANSI},
  33.         {"resources/fonts/cmr10.afm", BaseFont.WINANSI},
  34.         {"resources/fonts/cmr10.pfm", BaseFont.WINANSI},
  35.         //{"c:/windows/fonts/ARBLI__.TTF", BaseFont.WINANSI},
  36.         {"c:/windows/fonts/arial.ttf", BaseFont.WINANSI},
  37.         {"c:/windows/fonts/arial.ttf", BaseFont.IDENTITY_H},
  38.         {"resources/fonts/Puritan2.otf", BaseFont.WINANSI},
  39.         {"c:/windows/fonts/msgothic.ttc,0", BaseFont.IDENTITY_H},
  40.         {"KozMinPro-Regular", "UniJIS-UCS2-H"}
  41.     };
  42.  
  43.     /**
  44.      * Creates a PDF document.
  45.      * @param filename the path to the new PDF document
  46.      * @throws    DocumentException
  47.      * @throws    IOException
  48.      */
  49.     public void createPdf(String filename) throws IOException, DocumentException {
  50.         // step 1
  51.         Document document = new Document();
  52.         // step 2
  53.         PdfWriter.getInstance(document, new FileOutputStream(filename));
  54.         // step 3
  55.         document.open();
  56.         // step 4
  57.         BaseFont bf;
  58.         Font font;
  59.         for (int i = 0; i < FONTS.length; i++) {
  60.             bf = BaseFont.createFont(FONTS[i][0], FONTS[i][1], BaseFont.EMBEDDED);
  61.             document.add(new Paragraph(
  62.                     String.format("Font file: %s with encoding %s", FONTS[i][0], FONTS[i][1])));
  63.             document.add(new Paragraph(
  64.                     String.format("iText class: %s", bf.getClass().getName())));
  65.             font = new Font(bf, 12);
  66.             document.add(new Paragraph(TEXT, font));
  67.             document.add(new LineSeparator(0.5f, 100, null, 0, -5));
  68.         }
  69.         // step 5
  70.         document.close();
  71.     }
  72.  
  73.     /**
  74.      * Main method.
  75.      *
  76.      * @param    args    no arguments needed
  77.      * @throws DocumentException
  78.      * @throws IOException
  79.      */
  80.     public static void main(String[] args) throws IOException, DocumentException {
  81.         new FontTypes().createPdf(RESULT);
  82.     }
  83. }
TTCExample.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part3.chapter11;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.Chunk;
  14. import com.itextpdf.text.Document;
  15. import com.itextpdf.text.DocumentException;
  16. import com.itextpdf.text.Font;
  17. import com.itextpdf.text.Paragraph;
  18. import com.itextpdf.text.pdf.BaseFont;
  19. import com.itextpdf.text.pdf.PdfWriter;
  20.  
  21. public class TTCExample {
  22.  
  23.     /** The resulting PDF file. */
  24.     public static final String RESULT
  25.         = "results/part3/chapter11/ttc_example.pdf";
  26.     /** The path to the font. */
  27.     public static final String FONT = "c:/windows/fonts/msgothic.ttc";
  28.    
  29.     /**
  30.      * Creates a PDF document.
  31.      * @param filename the path to the new PDF document
  32.      * @throws DocumentException
  33.      * @throws IOException
  34.      * @throws    DocumentException
  35.      * @throws    IOException
  36.      */
  37.     public void createPdf(String filename) throws IOException, DocumentException {
  38.         // step 1
  39.         Document document = new Document();
  40.         // step 2
  41.         PdfWriter.getInstance(document, new FileOutputStream(filename));
  42.         // step 3: we open the document
  43.         document.open();
  44.         // step 4
  45.  
  46.         BaseFont bf;
  47.         Font font;
  48.         String[] names = BaseFont.enumerateTTCNames(FONT);
  49.         for (int i = 0; i < names.length; i++) {
  50.             bf = BaseFont.createFont(String.format("%s,%s", FONT, i),
  51.                 BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  52.             font = new Font(bf, 12);
  53.             document.add(new Paragraph("font " + i + ": " + names[i], font));
  54.             document.add(new Paragraph("Rash\u00f4mon", font));
  55.             document.add(new Paragraph("Directed by Akira Kurosawa", font));
  56.             document.add(new Paragraph("\u7f85\u751f\u9580", font));
  57.             document.add(Chunk.NEWLINE);
  58.         }
  59.         // step 5: we close the document
  60.         document.close();
  61.     }
  62.  
  63.     /**
  64.      * Main method.
  65.      *
  66.      * @param    args    no arguments needed
  67.      * @throws DocumentException
  68.      * @throws IOException
  69.      */
  70.     public static void main(String[] args) throws IOException, DocumentException {
  71.         new TTCExample().createPdf(RESULT);
  72.     }
  73. }
EncodingNames.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part3.chapter11;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.Chunk;
  14. import com.itextpdf.text.Document;
  15. import com.itextpdf.text.DocumentException;
  16. import com.itextpdf.text.Paragraph;
  17. import com.itextpdf.text.Rectangle;
  18. import com.itextpdf.text.pdf.BaseFont;
  19. import com.itextpdf.text.pdf.PdfWriter;
  20.  
  21. public class EncodingNames {
  22.  
  23.     /** The resulting PDF file. */
  24.     public static final String RESULT
  25.         = "results/part3/chapter11/font_encodings.pdf";
  26.     /** The path to the font. */
  27.     public static final String[] FONT = {
  28.         "c:/windows/fonts/ARBLI__.TTF",
  29.         "resources/fonts/Puritan2.otf",
  30.         "c:/windows/fonts/arialbd.ttf"
  31.     };
  32.    
  33.  
  34.     /**
  35.      * Creates a PDF document.
  36.      * @param filename the path to the new PDF document
  37.      * @throws DocumentException
  38.      * @throws IOException
  39.      * @throws    DocumentException
  40.      * @throws    IOException
  41.      */
  42.     public void createPdf(String filename) throws IOException, DocumentException {
  43.         // step 1
  44.         Document document = new Document(new Rectangle(350, 842));
  45.         // step 2
  46.         PdfWriter.getInstance(document, new FileOutputStream(filename));
  47.         // step 3
  48.         document.open();
  49.         // step 4
  50.         showEncodings(document, FONT[0]);
  51.         showEncodings(document, FONT[1]);
  52.         document.newPage();
  53.         showEncodings(document, FONT[2]);
  54.         // step 5
  55.         document.close();
  56.     }
  57.    
  58.     /**
  59.      * Writes the available encodings of a font to the document.
  60.      * @param document the document to which the encodings have to be written
  61.      * @param font     the font
  62.      * @throws DocumentException
  63.      * @throws IOException
  64.      */
  65.     public void showEncodings(Document document, String font) throws DocumentException, IOException {
  66.         BaseFont bf = BaseFont.createFont(font, BaseFont.WINANSI, BaseFont.EMBEDDED);
  67.         document.add(new Paragraph("PostScript name: " + bf.getPostscriptFontName()));
  68.         document.add(new Paragraph("Available code pages:"));
  69.         String[] encoding = bf.getCodePagesSupported();
  70.         for (int i = 0; i < encoding.length; i++) {
  71.             document.add(new Paragraph("encoding[" + i + "] = " + encoding[i]));
  72.         }
  73.         document.add(Chunk.NEWLINE);
  74.     }
  75.  
  76.     /**
  77.      * Main method.
  78.      *
  79.      * @param    args    no arguments needed
  80.      * @throws DocumentException
  81.      * @throws IOException
  82.      */
  83.     public static void main(String[] args)
  84.         throws IOException, DocumentException {
  85.         new EncodingNames().createPdf(RESULT);
  86.     }
  87. }
EncodingExample.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part3.chapter11;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.Chunk;
  14. import com.itextpdf.text.Document;
  15. import com.itextpdf.text.DocumentException;
  16. import com.itextpdf.text.Font;
  17. import com.itextpdf.text.Paragraph;
  18. import com.itextpdf.text.pdf.BaseFont;
  19. import com.itextpdf.text.pdf.PdfWriter;
  20.  
  21. public class EncodingExample {
  22.  
  23.     /** The resulting PDF file. */
  24.     public static final String RESULT
  25.         = "results/part3/chapter11/encoding_example.pdf";
  26.     /** The path to the font. */
  27.     public static final String FONT
  28.         = "c:/windows/fonts/arialbd.ttf";
  29.     /** Movie information. */
  30.     public static final String[][] MOVIES = {
  31.         {
  32.             "Cp1252",
  33.             "A Very long Engagement (France)",
  34.             "directed by Jean-Pierre Jeunet",
  35.             "Un long dimanche de fian\u00e7ailles"
  36.         },
  37.         {
  38.             "Cp1250",
  39.             "No Man's Land (Bosnia-Herzegovina)",
  40.             "Directed by Danis Tanovic",
  41.             "Nikogar\u0161nja zemlja"
  42.         },
  43.         {
  44.             "Cp1251",
  45.             "You I Love (Russia)",
  46.             "directed by Olga Stolpovskaja and Dmitry Troitsky",
  47.             "\u042f \u043b\u044e\u0431\u043b\u044e \u0442\u0435\u0431\u044f"
  48.         },
  49.         {
  50.             "Cp1253",
  51.             "Brides (Greece)",
  52.             "directed by Pantelis Voulgaris",
  53.             "\u039d\u03cd\u03c6\u03b5\u03c2"
  54.         }
  55.     };
  56.    
  57.     /**
  58.      * Creates a PDF document.
  59.      * @param filename the path to the new PDF document
  60.      * @throws DocumentException
  61.      * @throws IOException
  62.      * @throws    DocumentException
  63.      * @throws    IOException
  64.      */
  65.     public void createPdf(String filename) throws IOException, DocumentException {
  66.         // step 1
  67.         Document document = new Document();
  68.         // step 2
  69.         PdfWriter.getInstance(document, new FileOutputStream(filename));
  70.         // step 3
  71.         document.open();
  72.         // step 4
  73.         BaseFont bf;
  74.         for (int i = 0; i < 4; i++) {
  75.             bf = BaseFont.createFont(FONT, MOVIES[i][0], BaseFont.EMBEDDED);
  76.             document.add(new Paragraph("Font: " + bf.getPostscriptFontName()
  77.                     + " with encoding: " + bf.getEncoding()));
  78.             document.add(new Paragraph(MOVIES[i][1]));
  79.             document.add(new Paragraph(MOVIES[i][2]));
  80.             document.add(new Paragraph(MOVIES[i][3], new Font(bf, 12)));
  81.             document.add(Chunk.NEWLINE);
  82.         }
  83.         // step 5
  84.         document.close();
  85.     }
  86.    
  87.     /**
  88.      * Main method.
  89.      *
  90.      * @param    args    no arguments needed
  91.      * @throws DocumentException
  92.      * @throws IOException
  93.      */
  94.     public static void main(String[] args)
  95.         throws IOException, DocumentException {
  96.         new EncodingExample().createPdf(RESULT);
  97.     }
  98. }
UnicodeExample.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. package part3.chapter11;
  8.  
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11.  
  12. import com.itextpdf.text.Chunk;
  13. import com.itextpdf.text.Document;
  14. import com.itextpdf.text.DocumentException;
  15. import com.itextpdf.text.Font;
  16. import com.itextpdf.text.Paragraph;
  17. import com.itextpdf.text.pdf.BaseFont;
  18. import com.itextpdf.text.pdf.PdfWriter;
  19.  
  20. public class UnicodeExample extends EncodingExample {
  21.  
  22.     /** The resulting PDF file. */
  23.     public static final String RESULT
  24.         = "results/part3/chapter11/unicode_example.pdf";
  25.    
  26.     /**
  27.      * Creates a PDF document.
  28.      * @param filename the path to the new PDF document
  29.      * @throws DocumentException
  30.      * @throws IOException
  31.      * @throws    DocumentException
  32.      * @throws    IOException
  33.      */
  34.     public void createPdf(String filename) throws IOException, DocumentException {
  35.         // step 1
  36.         Document document = new Document();
  37.         // step 2
  38.         PdfWriter.getInstance(document, new FileOutputStream(filename));
  39.         // step 3: we open the document
  40.         document.open();
  41.         // step 4
  42.         BaseFont bf;
  43.         for (int i = 0; i < 4; i++) {
  44.             bf = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  45.             document.add(new Paragraph("Font: " + bf.getPostscriptFontName()
  46.                     + " with encoding: " + bf.getEncoding()));
  47.             document.add(new Paragraph(MOVIES[i][1]));
  48.             document.add(new Paragraph(MOVIES[i][2]));
  49.             document.add(new Paragraph(MOVIES[i][3], new Font(bf, 12)));
  50.             document.add(Chunk.NEWLINE);
  51.         }
  52.         // step 5: we close the document
  53.         document.close();
  54.     }
  55.    
  56.     /**
  57.      * Main method.
  58.      *
  59.      * @param    args    no arguments needed
  60.      * @throws DocumentException
  61.      * @throws IOException
  62.      */
  63.     public static void main(String[] args)
  64.         throws IOException, DocumentException {
  65.         new UnicodeExample().createPdf(RESULT);
  66.     }
  67. }
FontFileAndSizes.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. package part3.chapter11;
  8.  
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11.  
  12. import com.itextpdf.text.Document;
  13. import com.itextpdf.text.DocumentException;
  14. import com.itextpdf.text.Font;
  15. import com.itextpdf.text.Paragraph;
  16. import com.itextpdf.text.pdf.BaseFont;
  17. import com.itextpdf.text.pdf.PdfWriter;
  18.  
  19. public class FontFileAndSizes {
  20.  
  21.     /** The names of the resulting PDF files. */
  22.     public static final String[] RESULT = {
  23.         "results/part3/chapter11/font_not_embedded.pdf",
  24.         "results/part3/chapter11/font_embedded.pdf",
  25.         "results/part3/chapter11/font_embedded_less_glyphs.pdf",
  26.         "results/part3/chapter11/font_compressed.pdf",
  27.         "results/part3/chapter11/font_full.pdf"
  28.     };
  29.     /** The path to the font. */
  30.     public static final String FONT = "c:/windows/fonts/arial.ttf";
  31.     /** Some text. */
  32.     public static String TEXT
  33.         = "quick brown fox jumps over the lazy dog";
  34.     /** Some text. */
  35.     public static String OOOO
  36.         = "ooooo ooooo ooo ooooo oooo ooo oooo ooo";
  37.    
  38.     /**
  39.      * Creates a PDF document.
  40.      * @param filename the path to the new PDF document
  41.      * @throws DocumentException
  42.      * @throws IOException
  43.      * @throws    DocumentException
  44.      * @throws    IOException
  45.      */
  46.     public void createPdf(String filename, BaseFont bf, String text)
  47.         throws IOException, DocumentException {
  48.         // step 1
  49.         Document document = new Document();
  50.         // step 2
  51.         PdfWriter.getInstance(document, new FileOutputStream(filename));
  52.         // step 3
  53.         document.open();
  54.         // step 4
  55.         document.add(new Paragraph(text, new Font(bf, 12)));
  56.         // step 5
  57.         document.close();
  58.     }
  59.    
  60.     /**
  61.      * Main method.
  62.      *
  63.      * @param    args    no arguments needed
  64.      * @throws DocumentException
  65.      * @throws IOException
  66.      */
  67.     public static void main(String[] args) throws IOException, DocumentException {
  68.         FontFileAndSizes ffs = new FontFileAndSizes();
  69.         BaseFont bf;
  70.         bf = BaseFont.createFont(FONT, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
  71.         ffs.createPdf(RESULT[0], bf, TEXT);
  72.         bf = BaseFont.createFont(FONT, BaseFont.WINANSI, BaseFont.EMBEDDED);
  73.         ffs.createPdf(RESULT[1], bf, TEXT);
  74.         ffs.createPdf(RESULT[2], bf, OOOO);
  75.         bf = BaseFont.createFont(FONT, BaseFont.WINANSI, BaseFont.EMBEDDED);
  76.         bf.setCompressionLevel(9);
  77.         ffs.createPdf(RESULT[3], bf, TEXT);
  78.         bf = BaseFont.createFont(FONT, BaseFont.WINANSI, BaseFont.EMBEDDED);
  79.         bf.setSubset(false);
  80.         ffs.createPdf(RESULT[4], bf, TEXT);
  81.     }
  82. }
Type3Example.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. package part3.chapter11;
  8.  
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11.  
  12. import com.itextpdf.text.BaseColor;
  13. import com.itextpdf.text.Chunk;
  14. import com.itextpdf.text.Document;
  15. import com.itextpdf.text.DocumentException;
  16. import com.itextpdf.text.Font;
  17. import com.itextpdf.text.Paragraph;
  18. import com.itextpdf.text.pdf.GrayColor;
  19. import com.itextpdf.text.pdf.PdfContentByte;
  20. import com.itextpdf.text.pdf.PdfWriter;
  21. import com.itextpdf.text.pdf.Type3Font;
  22.  
  23. public class Type3Example {
  24.    
  25.     /** The resulting PDF file. */
  26.     public static final String RESULT
  27.         = "results/part3/chapter11/type3_example.pdf";
  28.  
  29.     /**
  30.      * Creates a PDF document.
  31.      * @param filename the path to the new PDF document
  32.      * @throws DocumentException
  33.      * @throws IOException
  34.      * @throws    DocumentException
  35.      * @throws    IOException
  36.      */
  37.     public void createPdf(String filename) throws IOException, DocumentException {
  38.         // step 1
  39.         Document document = new Document();
  40.         // step 2
  41.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
  42.         // step 3
  43.         document.open();
  44.         // step 4
  45.         Type3Font t3 = new Type3Font(writer, true);
  46.         PdfContentByte d = t3.defineGlyph('D', 600, 0, 0, 600, 700);
  47.         d.setColorStroke(new BaseColor(0xFF, 0x00, 0x00));
  48.         d.setColorFill(new GrayColor(0.7f));
  49.         d.setLineWidth(100);
  50.         d.moveTo(5, 5);
  51.         d.lineTo(300, 695);
  52.         d.lineTo(595, 5);
  53.         d.closePathFillStroke();
  54.         PdfContentByte s = t3.defineGlyph('S', 600, 0, 0, 600, 700);
  55.         s.setColorStroke(new BaseColor(0x00, 0x80, 0x80));
  56.         s.setLineWidth(100);
  57.         s.moveTo(595,5);
  58.         s.lineTo(5, 5);
  59.         s.lineTo(300, 350);
  60.         s.lineTo(5, 695);
  61.         s.lineTo(595, 695);
  62.         s.stroke();
  63.         Font f = new Font(t3, 12);
  64.         Paragraph p = new Paragraph();
  65.         p.add("This is a String with a Type3 font that contains a fancy Delta (");
  66.         p.add(new Chunk("D", f));
  67.         p.add(") and a custom Sigma (");
  68.         p.add(new Chunk("S", f));
  69.         p.add(").");
  70.         document.add(p);
  71.         // step 5: we close the document
  72.         document.close();
  73.     }
  74.    
  75.     /**
  76.      * Main method.
  77.      *
  78.      * @param    args    no arguments needed
  79.      * @throws DocumentException
  80.      * @throws IOException
  81.      */
  82.     public static void main(String[] args) throws IOException, DocumentException {
  83.         new Type3Example().createPdf(RESULT);
  84.     }
  85. }
CJKExample.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part3.chapter11;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.Chunk;
  14. import com.itextpdf.text.Document;
  15. import com.itextpdf.text.DocumentException;
  16. import com.itextpdf.text.Font;
  17. import com.itextpdf.text.Paragraph;
  18. import com.itextpdf.text.pdf.BaseFont;
  19. import com.itextpdf.text.pdf.PdfWriter;
  20.  
  21. public class CJKExample {
  22.  
  23.     /** The resulting PDF file. */
  24.     public static final String RESULT
  25.         = "results/part3/chapter11/cjk_example.pdf";
  26.     /** Movies, their director and original title */
  27.     public static final String[][] MOVIES = {
  28.         {
  29.             "STSong-Light", "UniGB-UCS2-H",
  30.             "Movie title: House of The Flying Daggers (China)",
  31.             "directed by Zhang Yimou",
  32.             "\u5341\u950a\u57cb\u4f0f"
  33.         },
  34.         {
  35.             "KozMinPro-Regular", "UniJIS-UCS2-H",
  36.             "Movie title: Nobody Knows (Japan)",
  37.             "directed by Hirokazu Koreeda",
  38.             "\u8ab0\u3082\u77e5\u3089\u306a\u3044"
  39.         },
  40.         {
  41.             "HYGoThic-Medium", "UniKS-UCS2-H",
  42.             "Movie title: '3-Iron' aka 'Bin-jip' (South-Korea)",
  43.             "directed by Kim Ki-Duk",
  44.             "\ube48\uc9d1"
  45.         }
  46.     };
  47.    
  48.     /**
  49.      * Creates a PDF document.
  50.      * @param filename the path to the new PDF document
  51.      * @throws DocumentException
  52.      * @throws IOException
  53.      * @throws    DocumentException
  54.      * @throws    IOException
  55.      */
  56.     public void createPdf(String filename) throws IOException, DocumentException {
  57.         // step 1
  58.         Document document = new Document();
  59.         // step 2
  60.         PdfWriter.getInstance(document, new FileOutputStream(filename));
  61.         // step 3
  62.         document.open();
  63.         // step 4
  64.         BaseFont bf;
  65.         Font font;
  66.        
  67.         for (int i = 0; i < 3; i++) {
  68.             bf = BaseFont.createFont(MOVIES[i][0], MOVIES[i][1], BaseFont.NOT_EMBEDDED);
  69.             font = new Font(bf, 12);
  70.             document.add(new Paragraph(bf.getPostscriptFontName(), font));
  71.             for (int j = 2; j < 5; j++)
  72.                 document.add(new Paragraph(MOVIES[i][j], font));
  73.             document.add(Chunk.NEWLINE);
  74.         }
  75.  
  76.        
  77.         // step 5
  78.         document.close();
  79.     }
  80.  
  81.     /**
  82.      * Main method.
  83.      *
  84.      * @param    args    no arguments needed
  85.      * @throws DocumentException
  86.      * @throws IOException
  87.      */
  88.     public static void main(String[] args) throws IOException, DocumentException {
  89.         new CJKExample().createPdf(RESULT);
  90.     }
  91. }
VerticalTextExample1.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part3.chapter11;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.Chunk;
  14. import com.itextpdf.text.Document;
  15. import com.itextpdf.text.DocumentException;
  16. import com.itextpdf.text.Element;
  17. import com.itextpdf.text.Font;
  18. import com.itextpdf.text.Phrase;
  19. import com.itextpdf.text.Rectangle;
  20. import com.itextpdf.text.pdf.BaseFont;
  21. import com.itextpdf.text.pdf.PdfWriter;
  22. import com.itextpdf.text.pdf.VerticalText;
  23.  
  24. public class VerticalTextExample1 {
  25.  
  26.     /** The resulting PDF file. */
  27.     public static final String RESULT
  28.         = "results/part3/chapter11/vertical_text_1.pdf";
  29.     /** A Japanese String. */
  30.     public static final String MOVIE = "\u4e03\u4eba\u306e\u4f8d";
  31.     /** The facts. */
  32.     public static final String TEXT1
  33.        = "You embarrass me. You're overestimating me. "
  34.        + "Listen, I'm not a man with any special skill, "
  35.        + "but I've had plenty of experience in battles; losing battles, all of them.";
  36.     /** The conclusion. */
  37.     public static final String TEXT2
  38.        = "In short, that's all I am. Drop such an idea for your own good.";
  39.    
  40.     /**
  41.      * Creates a PDF document.
  42.      * @param filename the path to the new PDF document
  43.      * @throws DocumentException
  44.      * @throws IOException
  45.      * @throws    DocumentException
  46.      * @throws    IOException
  47.      */
  48.     public void createPdf(String filename) throws IOException, DocumentException {
  49.         // step 1
  50.         Document document = new Document(new Rectangle(420, 600));
  51.         // step 2
  52.         PdfWriter writer
  53.             = PdfWriter.getInstance(document, new FileOutputStream(filename));
  54.         // step 3: we open the document
  55.         document.open();
  56.         // step 4
  57.         BaseFont bf = BaseFont.createFont(
  58.             "KozMinPro-Regular", "UniJIS-UCS2-V", BaseFont.NOT_EMBEDDED);
  59.         Font font = new Font(bf, 20);
  60.         VerticalText vt = new VerticalText(writer.getDirectContent());
  61.         vt.setVerticalLayout(390, 570, 540, 12, 30);
  62.         vt.addText(new Chunk(MOVIE, font));
  63.         vt.go();
  64.         vt.addText(new Phrase(TEXT1, font));
  65.         vt.go();
  66.         vt.setAlignment(Element.ALIGN_RIGHT);
  67.         vt.addText(new Phrase(TEXT2, font));
  68.         vt.go();
  69.         // step 5: we close the document
  70.         document.close();
  71.     }
  72.  
  73.     /**
  74.      * Main method.
  75.      *
  76.      * @param    args    no arguments needed
  77.      * @throws DocumentException
  78.      * @throws IOException
  79.      */
  80.     public static void main(String[] args) throws IOException, DocumentException {
  81.         new VerticalTextExample1().createPdf(RESULT);
  82.     }
  83. }
VerticalTextExample2.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part3.chapter11;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.Document;
  14. import com.itextpdf.text.DocumentException;
  15. import com.itextpdf.text.Element;
  16. import com.itextpdf.text.Font;
  17. import com.itextpdf.text.Phrase;
  18. import com.itextpdf.text.Rectangle;
  19. import com.itextpdf.text.pdf.BaseFont;
  20. import com.itextpdf.text.pdf.PdfWriter;
  21. import com.itextpdf.text.pdf.VerticalText;
  22.  
  23. public class VerticalTextExample2 extends VerticalTextExample1 {
  24.  
  25.     /** The resulting PDF file. */
  26.     public static final String RESULT
  27.         = "results/part3/chapter11/vertical_text_2.pdf";
  28.    
  29.     /**
  30.      * Creates a PDF document.
  31.      * @param filename the path to the new PDF document
  32.      * @throws DocumentException
  33.      * @throws IOException
  34.      * @throws    DocumentException
  35.      * @throws    IOException
  36.      */
  37.     public void createPdf(String filename) throws IOException, DocumentException {
  38.         // step 1
  39.         Document document = new Document(new Rectangle(420, 600));
  40.         // step 2
  41.         PdfWriter writer
  42.             = PdfWriter.getInstance(document, new FileOutputStream(filename));
  43.         // step 3: we open the document
  44.         document.open();
  45.         // step 4
  46.         BaseFont bf = BaseFont.createFont(
  47.             "KozMinPro-Regular", "Identity-V", BaseFont.NOT_EMBEDDED);
  48.         Font font = new Font(bf, 20);
  49.         VerticalText vt = new VerticalText(writer.getDirectContent());
  50.         vt.setVerticalLayout(390, 570, 540, 12, 30);
  51.         font = new Font(bf, 20);
  52.         vt.addText(new Phrase(convertCIDs(TEXT1), font));
  53.         vt.go();
  54.         vt.setAlignment(Element.ALIGN_RIGHT);
  55.         vt.addText(new Phrase(convertCIDs(TEXT2), font));
  56.         vt.go();
  57.         // step 5: we close the document
  58.         document.close();
  59.     }
  60.  
  61.     /**
  62.      * Converts the CIDs of the horizontal characters of a String
  63.      * into a String with vertical characters.
  64.      * @param text The String with the horizontal characters
  65.      * @return A String with vertical characters
  66.      */
  67.     public String convertCIDs(String text) {
  68.         char cid[] = text.toCharArray();
  69.         for (int k = 0; k < cid.length; ++k) {
  70.             char c = cid[k];
  71.             if (c == '\n')
  72.                 cid[k] = '\uff00';
  73.             else
  74.                 cid[k] = (char) (c - ' ' + 8720);
  75.         }
  76.         return new String(cid);
  77.     }
  78.  
  79.     /**
  80.      * Main method.
  81.      *
  82.      * @param    args    no arguments needed
  83.      * @throws DocumentException
  84.      * @throws IOException
  85.      */
  86.     public static void main(String[] args) throws IOException, DocumentException {
  87.         new VerticalTextExample2().createPdf(RESULT);
  88.     }
  89. }
RightToLeftExample.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part3.chapter11;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.Document;
  14. import com.itextpdf.text.DocumentException;
  15. import com.itextpdf.text.Font;
  16. import com.itextpdf.text.PageSize;
  17. import com.itextpdf.text.Paragraph;
  18. import com.itextpdf.text.pdf.BaseFont;
  19. import com.itextpdf.text.pdf.ColumnText;
  20. import com.itextpdf.text.pdf.PdfWriter;
  21.  
  22. public class RightToLeftExample {
  23.  
  24.     /** The resulting PDF file. */
  25.     public static final String RESULT
  26.         = "results/part3/chapter11/right_to_left.pdf";
  27.     /** A movie title. */
  28.     public static final String MOVIE
  29.         = "\u05d4\u05d0\u05e1\u05d5\u05e0\u05d5\u05ea \u05e9\u05dc \u05e0\u05d9\u05e0\u05d4";
  30.    
  31.     /**
  32.      * Creates a PDF document.
  33.      * @param filename the path to the new PDF document
  34.      * @throws DocumentException
  35.      * @throws IOException
  36.      * @throws    DocumentException
  37.      * @throws    IOException
  38.      */
  39.     public void createPdf(String filename) throws IOException, DocumentException {
  40.         // step 1
  41.         Document document = new Document(PageSize.A4);
  42.         // step 2
  43.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
  44.         // step 3
  45.         document.open();
  46.         // step 4
  47.         BaseFont bf = BaseFont.createFont(
  48.             "c:/windows/fonts/arial.ttf", BaseFont.IDENTITY_H, true);
  49.         Font font = new Font(bf, 14);
  50.         document.add(new Paragraph("Movie title: Nina's Tragedies"));
  51.         document.add(new Paragraph("directed by Savi Gabizon"));
  52.         ColumnText column = new ColumnText(writer.getDirectContent());
  53.         column.setSimpleColumn(36, 770, 569, 36);
  54.         column.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
  55.         column.addElement(new Paragraph(MOVIE, font));
  56.         column.go();
  57.         // step 5
  58.         document.close();
  59.     }
  60.  
  61.     /**
  62.      * Main method.
  63.      *
  64.      * @param    args    no arguments needed
  65.      * @throws DocumentException
  66.      * @throws IOException
  67.      */
  68.     public static void main(String[] args) throws IOException, DocumentException {
  69.         new RightToLeftExample().createPdf(RESULT);
  70.     }
  71.  
  72. }
SayPeace.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part3.chapter11;
  9.  
  10. import java.io.FileInputStream;
  11. import java.io.FileOutputStream;
  12. import java.io.IOException;
  13.  
  14. import javax.xml.parsers.FactoryConfigurationError;
  15. import javax.xml.parsers.ParserConfigurationException;
  16. import javax.xml.parsers.SAXParser;
  17. import javax.xml.parsers.SAXParserFactory;
  18.  
  19. import org.xml.sax.Attributes;
  20. import org.xml.sax.InputSource;
  21. import org.xml.sax.SAXException;
  22. import org.xml.sax.helpers.DefaultHandler;
  23.  
  24. import com.itextpdf.text.Chunk;
  25. import com.itextpdf.text.Document;
  26. import com.itextpdf.text.DocumentException;
  27. import com.itextpdf.text.Element;
  28. import com.itextpdf.text.ExceptionConverter;
  29. import com.itextpdf.text.Font;
  30. import com.itextpdf.text.PageSize;
  31. import com.itextpdf.text.Paragraph;
  32. import com.itextpdf.text.pdf.BaseFont;
  33. import com.itextpdf.text.pdf.GrayColor;
  34. import com.itextpdf.text.pdf.PdfContentByte;
  35. import com.itextpdf.text.pdf.PdfPCell;
  36. import com.itextpdf.text.pdf.PdfPTable;
  37. import com.itextpdf.text.pdf.PdfWriter;
  38.  
  39. /**
  40.  * This example was written by Bruno Lowagie. It is part of the book 'iText in
  41.  * Action' by Manning Publications.
  42.  * ISBN: 1932394796
  43.  * http://www.1t3xt.com/docs/book.php
  44.  * http://www.manning.com/lowagie/
  45.  */
  46.  
  47. public class SayPeace extends DefaultHandler {
  48.  
  49.     /** The resulting PDF file. */
  50.     public static final String RESULT
  51.         = "results/part3/chapter11/say_peace.pdf";
  52.     /** The XML file with the text. */
  53.     public static final String RESOURCE
  54.         = "resources/xml/say_peace.xml";
  55.    
  56.     /** The font that is used for the peace message. */
  57.     public Font f;
  58.  
  59.     /** The document to which we are going to add our message. */
  60.     protected Document document;
  61.  
  62.     /** The StringBuffer that holds the characters. */
  63.     protected StringBuffer buf = new StringBuffer();
  64.    
  65.     /** The table that holds the text. */
  66.     protected PdfPTable table;
  67.    
  68.     /** The current cell. */
  69.     protected PdfPCell cell;
  70.  
  71.     /**
  72.      * @see org.xml.sax.ContentHandler#startElement(java.lang.String,
  73.      *      java.lang.String, java.lang.String, org.xml.sax.Attributes)
  74.      */
  75.     public void startElement(String uri, String localName, String qName,
  76.             Attributes attributes) throws SAXException {
  77.         if ("message".equals(qName)) {
  78.             buf = new StringBuffer();
  79.             cell = new PdfPCell();
  80.             cell.setBorder(PdfPCell.NO_BORDER);
  81.             if ("RTL".equals(attributes.getValue("direction"))) {
  82.                 cell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
  83.             }
  84.         }
  85.         else if ("pace".equals(qName)) {
  86.             table = new PdfPTable(1);
  87.             table.setWidthPercentage(100);
  88.         }
  89.     }
  90.  
  91.     /**
  92.      * @see org.xml.sax.ContentHandler#endElement(java.lang.String,
  93.      *      java.lang.String, java.lang.String)
  94.      */
  95.     public void endElement(String uri, String localName, String qName)
  96.             throws SAXException {
  97.         if ("big".equals(qName)) {
  98.             Chunk bold = new Chunk(strip(buf), f);
  99.             bold.setTextRenderMode(
  100.                     PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE, 0.5f,
  101.                     GrayColor.GRAYBLACK);
  102.             Paragraph p = new Paragraph(bold);
  103.             p.setAlignment(Element.ALIGN_LEFT);
  104.             cell.addElement(p);
  105.             buf = new StringBuffer();
  106.         }
  107.         else if ("message".equals(qName)) {
  108.             Paragraph p = new Paragraph(strip(buf), f);
  109.             p.setAlignment(Element.ALIGN_LEFT);
  110.             cell.addElement(p);
  111.             table.addCell(cell);
  112.             buf = new StringBuffer();
  113.         }
  114.         else if ("pace".equals(qName)) {
  115.             try {
  116.                 document.add(table);
  117.             } catch (DocumentException e) {
  118.                 throw new ExceptionConverter(e);
  119.             }
  120.         }
  121.     }
  122.  
  123.     /**
  124.      * @see org.xml.sax.ContentHandler#characters(char[], int, int)
  125.      */
  126.     public void characters(char[] ch, int start, int length)
  127.             throws SAXException {
  128.         buf.append(ch, start, length);
  129.     }
  130.  
  131.     /**
  132.      * Replaces all the newline characters by a space.
  133.      *
  134.      * @param buf
  135.      *            the original StringBuffer
  136.      * @return a String without newlines
  137.      */
  138.     protected String strip(StringBuffer buf) {
  139.         int pos;
  140.         while ((pos = buf.indexOf("\n")) != -1)
  141.             buf.replace(pos, pos + 1, " ");
  142.         while (buf.charAt(0) == ' ')
  143.             buf.deleteCharAt(0);
  144.         return buf.toString();
  145.     }
  146.  
  147.     /**
  148.      * Creates the handler to read the peace message.
  149.      *
  150.      * @param document
  151.      * @param is
  152.      * @throws FactoryConfigurationError
  153.      * @throws ParserConfigurationException
  154.      * @throws SAXException
  155.      * @throws FactoryConfigurationError
  156.      * @throws ParserConfigurationException
  157.      * @throws IOException
  158.      * @throws DocumentException
  159.      */
  160.     public SayPeace(Document document, InputSource is)
  161.             throws ParserConfigurationException, SAXException,
  162.             FactoryConfigurationError, DocumentException, IOException {
  163.         this.document = document;
  164.         f = new Font(BaseFont.createFont("c:/windows/fonts/arialuni.ttf",
  165.                 BaseFont.IDENTITY_H, BaseFont.EMBEDDED));
  166.         SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
  167.         parser.parse(is, this);
  168.     }
  169.  
  170.     /**
  171.      * Generates a PDF with a Peace message in English, Arabic and Hebrew.
  172.      *
  173.      * @param args no arguments needed
  174.      */
  175.     public static void main(String[] args) {
  176.         // step 1:
  177.         Document document = new Document(PageSize.A4);
  178.         try {
  179.             // step 2:
  180.             PdfWriter.getInstance(document, new FileOutputStream(RESULT));
  181.             // step 3:
  182.             document.open();
  183.             // step 4:
  184.             new SayPeace(document,
  185.                 new InputSource(new FileInputStream(RESOURCE)));
  186.         } catch (Exception e) {
  187.             System.err.println(e.getMessage());
  188.         }
  189.         // step 5: we close the document
  190.         document.close();
  191.     }
  192. }
Diacritics1.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part3.chapter11;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.Document;
  14. import com.itextpdf.text.DocumentException;
  15. import com.itextpdf.text.Element;
  16. import com.itextpdf.text.Font;
  17. import com.itextpdf.text.Image;
  18. import com.itextpdf.text.Paragraph;
  19. import com.itextpdf.text.pdf.BaseFont;
  20. import com.itextpdf.text.pdf.GrayColor;
  21. import com.itextpdf.text.pdf.PdfWriter;
  22.  
  23. public class Diacritics1 {
  24.  
  25.     /** The resulting PDF file. */
  26.     public static final String RESULT
  27.         = "results/part3/chapter11/diacritics1.pdf";
  28.     /** A movie title. */
  29.     public static final String MOVIE
  30.         = "\u0e1f\u0e49\u0e32\u0e17\u0e30\u0e25\u0e32\u0e22\u0e42\u0e08\u0e23";
  31.     /** Movie poster */
  32.     public static final String POSTER
  33.         = "resources/posters/0269217.jpg";
  34.     /** Fonts */
  35.     public static final String[] FONTS = {
  36.         "c:/windows/fonts/angsa.ttf",
  37.         "c:/windows/fonts/arialuni.ttf"
  38.     };
  39.    
  40.     /**
  41.      * Creates a PDF document.
  42.      * @param filename the path to the new PDF document
  43.      * @throws DocumentException
  44.      * @throws IOException
  45.      * @throws    DocumentException
  46.      * @throws    IOException
  47.      */
  48.     public void createPdf(String filename) throws IOException, DocumentException {
  49.         // step 1
  50.         Document document = new Document();
  51.         // step 2
  52.         PdfWriter.getInstance(document, new FileOutputStream(filename));
  53.         // step 3
  54.         document.open();
  55.         // step 4
  56.         BaseFont bf;
  57.         Font font;
  58.         Image img = Image.getInstance(POSTER);
  59.         img.scalePercent(50);
  60.         img.setBorderWidth(18f);
  61.         img.setBorder(Image.BOX);
  62.         img.setBorderColor(GrayColor.GRAYWHITE);
  63.         img.setAlignment(Element.ALIGN_LEFT | Image.TEXTWRAP);
  64.         document.add(img);
  65.         document.add(new Paragraph(
  66.             "Movie title: Tears of the Black Tiger (Thailand)"));
  67.         document.add(new Paragraph("directed by Wisit Sasanatieng"));
  68.         for (int i = 0; i < 2; i++) {
  69.             bf = BaseFont.createFont(FONTS[i], BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  70.             document.add(new Paragraph("Font: " + bf.getPostscriptFontName()));
  71.             font = new Font(bf, 20);
  72.             document.add(new Paragraph(MOVIE, font));
  73.         }
  74.         // step 5
  75.         document.close();
  76.     }
  77.  
  78.     /**
  79.      * Main method.
  80.      *
  81.      * @param    args    no arguments needed
  82.      * @throws DocumentException
  83.      * @throws IOException
  84.      */
  85.     public static void main(String[] args) throws IOException, DocumentException {
  86.         new Diacritics1().createPdf(RESULT);
  87.     }
  88. }
Diacritics2.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part3.chapter11;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.Document;
  14. import com.itextpdf.text.DocumentException;
  15. import com.itextpdf.text.Font;
  16. import com.itextpdf.text.Paragraph;
  17. import com.itextpdf.text.pdf.BaseFont;
  18. import com.itextpdf.text.pdf.PdfWriter;
  19.  
  20.  
  21. public class Diacritics2 {
  22.  
  23.     /** The resulting PDF file. */
  24.     public static final String RESULT
  25.         = "results/part3/chapter11/diacritics2.pdf";
  26.     /** A movie title. */
  27.     public static final String MOVIE
  28.         = "Tomten \u00a8ar far till alla barnen";
  29.     /** Fonts */
  30.     public static final String[] FONTS = {
  31.         "c:/windows/fonts/arial.ttf",
  32.         "c:/windows/fonts/cour.ttf"
  33.     };
  34.    
  35.     /**
  36.      * Creates a PDF document.
  37.      * @param filename the path to the new PDF document
  38.      * @throws DocumentException
  39.      * @throws IOException
  40.      * @throws    DocumentException
  41.      * @throws    IOException
  42.      */
  43.     public void createPdf(String filename) throws IOException, DocumentException {
  44.         // step 1
  45.         Document document = new Document();
  46.         // step 2
  47.         PdfWriter.getInstance(document, new FileOutputStream(filename));
  48.         // step 3
  49.         document.open();
  50.         // step 4
  51.         document.add(new Paragraph("Movie title: In Bed With Santa (Sweden)"));
  52.         document.add(new Paragraph("directed by Kjell Sundvall"));
  53.         BaseFont bf = BaseFont.createFont(FONTS[0], BaseFont.CP1252, BaseFont.EMBEDDED);
  54.         Font font = new Font(bf, 12);
  55.         bf.setCharAdvance('\u00a8', -100);
  56.         document.add(new Paragraph(MOVIE, font));
  57.         bf = BaseFont.createFont(FONTS[1], BaseFont.CP1252, BaseFont.EMBEDDED);
  58.         bf.setCharAdvance('\u00a8', 0);
  59.         font = new Font(bf, 12);
  60.         document.add(new Paragraph(MOVIE, font));
  61.         // step 5
  62.         document.close();
  63.     }
  64.  
  65.     /**
  66.      * Main method.
  67.      *
  68.      * @param    args    no arguments needed
  69.      * @throws DocumentException
  70.      * @throws IOException
  71.      */
  72.     public static void main(String[] args) throws IOException, DocumentException {
  73.         new Diacritics2().createPdf(RESULT);
  74.     }
  75. }
Monospace.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part3.chapter11;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.Document;
  14. import com.itextpdf.text.DocumentException;
  15. import com.itextpdf.text.Font;
  16. import com.itextpdf.text.Paragraph;
  17. import com.itextpdf.text.pdf.BaseFont;
  18. import com.itextpdf.text.pdf.PdfWriter;
  19.  
  20. public class Monospace {
  21.  
  22.     /** The resulting PDF file. */
  23.     public static final String RESULT
  24.         = "results/part3/chapter11/monospace.pdf";
  25.     /** A movie title. */
  26.     public static final String MOVIE
  27.         = "Aanrijding in Moscou";
  28.    
  29.     /**
  30.      * Creates a PDF document.
  31.      * @param filename the path to the new PDF document
  32.      * @throws DocumentException
  33.      * @throws IOException
  34.      * @throws    DocumentException
  35.      * @throws    IOException
  36.      */
  37.     public void createPdf(String filename) throws IOException, DocumentException {
  38.         // step 1
  39.         Document document = new Document();
  40.         // step 2
  41.         PdfWriter.getInstance(document, new FileOutputStream(filename));
  42.         // step 3: we open the document
  43.         document.open();
  44.         // step 4
  45.         BaseFont bf1 = BaseFont.createFont("c:/windows/fonts/arial.ttf",
  46.             BaseFont.CP1252, BaseFont.EMBEDDED);
  47.         Font font1 = new Font(bf1, 12);
  48.         document.add(new Paragraph("Movie title: Moscou, Belgium", font1));
  49.         document.add(new Paragraph("directed by Christophe Van Rompaey", font1));
  50.         document.add(new Paragraph(MOVIE, font1));
  51.         BaseFont bf2 = BaseFont.createFont("c:/windows/fonts/cour.ttf",
  52.             BaseFont.CP1252, BaseFont.EMBEDDED);
  53.         Font font2 = new Font(bf2, 12);
  54.         document.add(new Paragraph(MOVIE, font2));
  55.         BaseFont bf3 = BaseFont.createFont("c:/windows/fonts/arialbd.ttf",
  56.             BaseFont.CP1252, BaseFont.EMBEDDED);
  57.         Font font3 = new Font(bf3, 12);
  58.         int widths[] = bf3.getWidths();
  59.         for (int k = 0; k < widths.length; ++k) {
  60.             if (widths[k] != 0)
  61.                 widths[k] = 600;
  62.         }
  63.         bf3.setForceWidthsOutput(true);
  64.         document.add(new Paragraph(MOVIE, font3));
  65.         // step 5: we close the document
  66.         document.close();
  67.     }
  68.  
  69.     /**
  70.      * Main method.
  71.      *
  72.      * @param    args    no arguments needed
  73.      * @throws DocumentException
  74.      * @throws IOException
  75.      */
  76.     public static void main(String[] args) throws IOException, DocumentException {
  77.         new Monospace().createPdf(RESULT);
  78.     }
  79. }
ExtraCharSpace.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part3.chapter11;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.Chunk;
  14. import com.itextpdf.text.Document;
  15. import com.itextpdf.text.DocumentException;
  16. import com.itextpdf.text.Font;
  17. import com.itextpdf.text.Paragraph;
  18. import com.itextpdf.text.pdf.BaseFont;
  19. import com.itextpdf.text.pdf.PdfWriter;
  20.  
  21. public class ExtraCharSpace {
  22.  
  23.     /** The resulting PDF file. */
  24.     public static final String RESULT
  25.         = "results/part3/chapter11/character_spacing.pdf";
  26.     /** A movie title. */
  27.     public static final String MOVIE
  28.         = "Aanrijding in Moscou";
  29.    
  30.     /**
  31.      * Creates a PDF document.
  32.      * @param filename the path to the new PDF document
  33.      * @throws DocumentException
  34.      * @throws IOException
  35.      * @throws    DocumentException
  36.      * @throws    IOException
  37.      */
  38.     public void createPdf(String filename) throws IOException, DocumentException {
  39.         // step 1
  40.         Document document = new Document();
  41.         // step 2
  42.         PdfWriter.getInstance(document, new FileOutputStream(filename));
  43.         // step 3
  44.         document.open();
  45.         // step 4
  46.         BaseFont bf1 = BaseFont.createFont("c:/windows/fonts/arial.ttf",
  47.             BaseFont.CP1252, BaseFont.EMBEDDED);
  48.         Font font1 = new Font(bf1, 12);
  49.         document.add(new Paragraph("Movie title: Moscou, Belgium", font1));
  50.         document.add(new Paragraph("directed by Christophe Van Rompaey", font1));
  51.         Chunk chunk = new Chunk(MOVIE, font1);
  52.         chunk.setCharacterSpacing(10);
  53.         document.add(new Paragraph(chunk));
  54.         // step 5
  55.         document.close();
  56.     }
  57.  
  58.     /**
  59.      * Main method.
  60.      *
  61.      * @param    args    no arguments needed
  62.      * @throws DocumentException
  63.      * @throws IOException
  64.      */
  65.     public static void main(String[] args) throws IOException, DocumentException {
  66.         new ExtraCharSpace().createPdf(RESULT);
  67.     }
  68. }
Ligatures1.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part3.chapter11;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.Document;
  14. import com.itextpdf.text.DocumentException;
  15. import com.itextpdf.text.Font;
  16. import com.itextpdf.text.Paragraph;
  17. import com.itextpdf.text.pdf.BaseFont;
  18. import com.itextpdf.text.pdf.PdfWriter;
  19.  
  20. public class Ligatures1 {
  21.  
  22.     /** The resulting PDF file. */
  23.     public static final String RESULT
  24.         = "results/part3/chapter11/ligatures_1.pdf";
  25.  
  26.     /**
  27.      * Creates a PDF document.
  28.      * @param filename the path to the new PDF document
  29.      * @throws DocumentException
  30.      * @throws IOException
  31.      * @throws    DocumentException
  32.      * @throws    IOException
  33.      */
  34.     public void createPdf(String filename) throws IOException, DocumentException {
  35.         // step 1
  36.         Document document = new Document();
  37.         // step 2
  38.         PdfWriter.getInstance(document, new FileOutputStream(filename));
  39.         // step 3: we open the document
  40.         document.open();
  41.         // step 4
  42.         BaseFont bf = BaseFont.createFont(
  43.             "c:/windows/fonts/arial.ttf", BaseFont.CP1252, BaseFont.EMBEDDED);
  44.         Font font = new Font(bf, 12);
  45.         document.add(new Paragraph("Movie title: Love at First Hiccough (Denmark)", font));
  46.         document.add(new Paragraph("directed by Tomas Villum Jensen", font));
  47.         document.add(new Paragraph("K\u00e6rlighed ved f\u00f8rste hik", font));
  48.         document.add(new Paragraph(ligaturize("Kaerlighed ved f/orste hik"), font));
  49.         // step 5: we close the document
  50.         document.close();
  51.     }
  52.  
  53.     /**
  54.      * Method that makes the ligatures for the combinations 'a' and 'e'
  55.      * and for '/' and 'o'.
  56.      * @param s a String that may have the combinations ae or /o
  57.      * @return a String where the combinations are replaced by a unicode character
  58.      */
  59.     public String ligaturize(String s) {
  60.         int pos;
  61.         while ((pos = s.indexOf("ae")) > -1) {
  62.             s = s.substring(0, pos) + '\u00e6' + s.substring(pos + 2);
  63.         }
  64.         while ((pos = s.indexOf("/o")) > -1) {
  65.             s = s.substring(0, pos) + '\u00f8' + s.substring(pos + 2);
  66.         }
  67.         return s;
  68.     }
  69.  
  70.     /**
  71.      * Main method.
  72.      *
  73.      * @param    args    no arguments needed
  74.      * @throws DocumentException
  75.      * @throws IOException
  76.      */
  77.     public static void main(String[] args) throws IOException, DocumentException {
  78.         new Ligatures1().createPdf(RESULT);
  79.     }
  80. }
Ligatures2.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part3.chapter11;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.Document;
  14. import com.itextpdf.text.DocumentException;
  15. import com.itextpdf.text.Font;
  16. import com.itextpdf.text.Paragraph;
  17. import com.itextpdf.text.pdf.BaseFont;
  18. import com.itextpdf.text.pdf.ColumnText;
  19. import com.itextpdf.text.pdf.PdfWriter;
  20.  
  21. public class Ligatures2 {
  22.  
  23.     /** The resulting PDF file. */
  24.     public static final String RESULT
  25.         = "results/part3/chapter11/ligatures_2.pdf";
  26.     /** Correct movie title. */
  27.     public static final String MOVIE
  28.         = "\u0644\u0648\u0631\u0627\u0646\u0633 \u0627\u0644\u0639\u0631\u0628";
  29.     /** Correct movie title. */
  30.     public static final String MOVIE_WITH_SPACES
  31.         = "\u0644 \u0648 \u0631 \u0627 \u0646 \u0633   \u0627 \u0644 \u0639 \u0631 \u0628";
  32.    
  33.     /**
  34.      * Creates a PDF document.
  35.      * @param filename the path to the new PDF document
  36.      * @throws DocumentException
  37.      * @throws IOException
  38.      * @throws    DocumentException
  39.      * @throws    IOException
  40.      */
  41.     public void createPdf(String filename) throws IOException, DocumentException {
  42.         // step 1
  43.         Document document = new Document();
  44.         // step 2
  45.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
  46.         // step 3
  47.         document.open();
  48.         // step 4
  49.         BaseFont bf = BaseFont.createFont(
  50.             "c:/windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  51.         Font font = new Font(bf, 20);
  52.         document.add(new Paragraph("Movie title: Lawrence of Arabia (UK)"));
  53.         document.add(new Paragraph("directed by David Lean"));
  54.         document.add(new Paragraph("Wrong: " + MOVIE, font));
  55.         ColumnText column = new ColumnText(writer.getDirectContent());
  56.         column.setSimpleColumn(36, 730, 569, 36);
  57.         column.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
  58.         column.addElement(new Paragraph("Wrong: " + MOVIE_WITH_SPACES, font));
  59.         column.addElement(new Paragraph(MOVIE, font));
  60.         column.go();
  61.         // step 5
  62.         document.close();
  63.     }
  64.  
  65.     /**
  66.      * Main method.
  67.      *
  68.      * @param    args    no arguments needed
  69.      * @throws DocumentException
  70.      * @throws IOException
  71.      */
  72.     public static void main(String[] args) throws IOException, DocumentException {
  73.         new Ligatures2().createPdf(RESULT);
  74.     }
  75. }
FontFactoryExample.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part3.chapter11;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.Chunk;
  14. import com.itextpdf.text.Document;
  15. import com.itextpdf.text.DocumentException;
  16. import com.itextpdf.text.Font;
  17. import com.itextpdf.text.FontFactory;
  18. import com.itextpdf.text.Paragraph;
  19. import com.itextpdf.text.pdf.BaseFont;
  20. import com.itextpdf.text.pdf.PdfWriter;
  21.  
  22. public class FontFactoryExample {
  23.  
  24.     /** The resulting PDF file. */
  25.     public static final String RESULT
  26.         = "results/part3/chapter11/font_factory.pdf";
  27.  
  28.     /**
  29.      * Creates a PDF document.
  30.      * @param filename the path to the new PDF document
  31.      * @throws DocumentException
  32.      * @throws IOException
  33.      * @throws    DocumentException
  34.      * @throws    IOException
  35.      */
  36.     public void createPdf(String filename) throws IOException, DocumentException {
  37.         // step 1
  38.         Document document = new Document();
  39.         // step 2
  40.         PdfWriter.getInstance(document, new FileOutputStream(filename));
  41.         // step 3
  42.         document.open();
  43.         // step 4:
  44.         Font font = FontFactory.getFont("Times-Roman");
  45.         document.add(new Paragraph("Times-Roman", font));
  46.         Font fontbold = FontFactory.getFont("Times-Roman", 12, Font.BOLD);
  47.         document.add(new Paragraph("Times-Roman, Bold", fontbold));
  48.         document.add(Chunk.NEWLINE);
  49.         FontFactory.register("c:/windows/fonts/garabd.ttf", "my_bold_font");
  50.         Font myBoldFont = FontFactory.getFont("my_bold_font");
  51.         BaseFont bf = myBoldFont.getBaseFont();
  52.         document.add(new Paragraph(bf.getPostscriptFontName(), myBoldFont));
  53.         String[][] name = bf.getFullFontName();
  54.         for (int i = 0; i < name.length; i++) {
  55.             document.add(new Paragraph(name[i][3] + " (" + name[i][0]
  56.                     + "; " + name[i][1] + "; " + name[i][2] + ")"));
  57.         }
  58.         Font myBoldFont2 = FontFactory.getFont("Garamond vet");
  59.         document.add(new Paragraph("Garamond Vet", myBoldFont2));
  60.         document.add(Chunk.NEWLINE);
  61.         document.add(new Paragraph("Registered fonts:"));
  62.         FontFactory.registerDirectory("resources/fonts");
  63.         for (String f : FontFactory.getRegisteredFonts()) {
  64.             document.add(new Paragraph(f, FontFactory.getFont(f, "", BaseFont.EMBEDDED)));
  65.         }
  66.         document.add(Chunk.NEWLINE);
  67.         Font cmr10 = FontFactory.getFont("cmr10");
  68.         cmr10.getBaseFont().setPostscriptFontName("Computer Modern Regular");
  69.         Font computerModern = FontFactory.getFont("Computer Modern Regular", "", BaseFont.EMBEDDED);
  70.         document.add(new Paragraph("Computer Modern", computerModern));
  71.         document.add(Chunk.NEWLINE);
  72.         FontFactory.registerDirectories();
  73.         for (String f : FontFactory.getRegisteredFamilies()) {
  74.             document.add(new Paragraph(f));
  75.         }
  76.         document.add(Chunk.NEWLINE);
  77.         Font garamond = FontFactory.getFont("garamond", BaseFont.WINANSI, BaseFont.EMBEDDED);
  78.         document.add(new Paragraph("Garamond", garamond));
  79.         Font garamondItalic
  80.             = FontFactory.getFont("Garamond", BaseFont.WINANSI, BaseFont.EMBEDDED, 12, Font.ITALIC);
  81.         document.add(new Paragraph("Garamond-Italic", garamondItalic));
  82.         // step 5
  83.         document.close();
  84.     }
  85.  
  86.  
  87.     /**
  88.      * Main method.
  89.      *
  90.      * @param    args    no arguments needed
  91.      * @throws DocumentException
  92.      * @throws IOException
  93.      */
  94.     public static void main(String[] args) throws IOException, DocumentException {
  95.         new FontFactoryExample().createPdf(RESULT);
  96.     }
  97. }
FontSelectionExample.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part3.chapter11;
  9.  
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.BaseColor;
  14. import com.itextpdf.text.Document;
  15. import com.itextpdf.text.DocumentException;
  16. import com.itextpdf.text.Font;
  17. import com.itextpdf.text.FontFactory;
  18. import com.itextpdf.text.PageSize;
  19. import com.itextpdf.text.Paragraph;
  20. import com.itextpdf.text.Phrase;
  21. import com.itextpdf.text.pdf.BaseFont;
  22. import com.itextpdf.text.pdf.FontSelector;
  23. import com.itextpdf.text.pdf.PdfWriter;
  24.  
  25. /**
  26.  * This example was written by Bruno Lowagie. It is part of the book 'iText in
  27.  * Action' by Manning Publications.
  28.  * ISBN: 1932394796
  29.  * http://www.1t3xt.com/docs/book.php
  30.  * http://www.manning.com/lowagie/
  31.  */
  32.  
  33. public class FontSelectionExample {
  34.  
  35.     /** The resulting PDF file. */
  36.     public static final String RESULT
  37.         = "results/part3/chapter11/font_selection.pdf";
  38.     /** Some text */
  39.     public static final String TEXT
  40.         = "These are the protagonists in 'Hero', a movie by Zhang Yimou:\n"
  41.             + "\u7121\u540d (Nameless), \u6b98\u528d (Broken Sword), "
  42.             + "\u98db\u96ea (Flying Snow), \u5982\u6708 (Moon), "
  43.             + "\u79e6\u738b (the King), and \u9577\u7a7a (Sky).";
  44.  
  45.     /**
  46.      * Creates a PDF document.
  47.      * @param filename the path to the new PDF document
  48.      * @throws DocumentException
  49.      * @throws IOException
  50.      * @throws    DocumentException
  51.      * @throws    IOException
  52.      */
  53.     public void createPdf(String filename) throws IOException, DocumentException {
  54.         // step 1
  55.         Document document = new Document(PageSize.A4);
  56.         // step 2
  57.         PdfWriter.getInstance(document, new FileOutputStream(filename));
  58.         // step 3: we open the document
  59.         document.open();
  60.         // step 4:
  61.         FontSelector selector = new FontSelector();
  62.         Font f1 = FontFactory.getFont(FontFactory.TIMES_ROMAN, 12);
  63.         f1.setColor(BaseColor.BLUE);
  64.         Font f2 = FontFactory.getFont("MSung-Light",
  65.                 "UniCNS-UCS2-H", BaseFont.NOT_EMBEDDED);
  66.         f2.setColor(BaseColor.RED);
  67.         selector.addFont(f1);
  68.         selector.addFont(f2);
  69.         Phrase ph = selector.process(TEXT);
  70.         document.add(new Paragraph(ph));
  71.         // step 5: we close the document
  72.         document.close();
  73.     }
  74.  
  75.     /**
  76.      * Main method.
  77.      *
  78.      * @param    args    no arguments needed
  79.      * @throws DocumentException
  80.      * @throws IOException
  81.      */
  82.     public static void main(String[] args) throws IOException, DocumentException {
  83.         new FontSelectionExample().createPdf(RESULT);
  84.     }
  85. }
Peace.java
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7.  
  8. package part3.chapter11;
  9.  
  10. import java.io.FileInputStream;
  11. import java.io.FileOutputStream;
  12.  
  13. import javax.xml.parsers.SAXParser;
  14. import javax.xml.parsers.SAXParserFactory;
  15.  
  16. import org.xml.sax.Attributes;
  17. import org.xml.sax.InputSource;
  18. import org.xml.sax.SAXException;
  19. import org.xml.sax.helpers.DefaultHandler;
  20.  
  21. import com.itextpdf.text.Document;
  22. import com.itextpdf.text.FontFactory;
  23. import com.itextpdf.text.PageSize;
  24. import com.itextpdf.text.pdf.BaseFont;
  25. import com.itextpdf.text.pdf.FontSelector;
  26. import com.itextpdf.text.pdf.PdfPCell;
  27. import com.itextpdf.text.pdf.PdfPTable;
  28. import com.itextpdf.text.pdf.PdfWriter;
  29.  
  30. public class Peace extends DefaultHandler {
  31.  
  32.     /** The resulting PDF file. */
  33.     public static final String RESULT
  34.         = "results/part3/chapter11/peace.pdf";
  35.    
  36.     /** The XML file with the text. */
  37.     public static final String RESOURCE
  38.         = "resources/xml/peace.xml";
  39.    
  40.     /** Paths to and encodings of fonts we're going to use in this example */
  41.     public static String[][] FONTS = {
  42.         {"c:/windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H},
  43.         {"resources/fonts/abserif4_5.ttf", BaseFont.IDENTITY_H},
  44.         {"resources/fonts/damase.ttf", BaseFont.IDENTITY_H},
  45.         {"resources/fonts/fsex2p00_public.ttf", BaseFont.IDENTITY_H}
  46.     };
  47.    
  48.     /** Holds he fonts that can be used for the peace message. */
  49.     public FontSelector fs;
  50.  
  51.     /** The columns that contains the message. */
  52.     protected PdfPTable table;
  53.  
  54.     /** The language. */
  55.     protected String language;
  56.  
  57.     /** The countries. */
  58.     protected String countries;
  59.  
  60.     /** Indicates when the text should be written from right to left. */
  61.     protected boolean rtl;
  62.  
  63.     /** The StringBuffer that holds the characters. */
  64.     protected StringBuffer buf = new StringBuffer();
  65.  
  66.     /**
  67.      * Creates the handler for the pace.xml file.
  68.      */
  69.     public Peace() {
  70.         fs = new FontSelector();
  71.         for (int i = 0; i < FONTS.length; i++) {
  72.             fs.addFont(FontFactory.getFont(FONTS[i][0], FONTS[i][1], BaseFont.EMBEDDED));
  73.         }
  74.         table = new PdfPTable(3);
  75.         table.getDefaultCell().setPadding(3);
  76.         table.getDefaultCell().setUseAscender(true);
  77.         table.getDefaultCell().setUseDescender(true);
  78.     }
  79.  
  80.     /**
  81.      * @see org.xml.sax.ContentHandler#startElement(java.lang.String,
  82.      *      java.lang.String, java.lang.String, org.xml.sax.Attributes)
  83.      */
  84.     public void startElement(String uri, String localName, String qName,
  85.             Attributes attributes) throws SAXException {
  86.         if ("pace".equals(qName)) {
  87.             buf = new StringBuffer();
  88.             language = attributes.getValue("language");
  89.             countries = attributes.getValue("countries");
  90.             if ("RTL".equals(attributes.getValue("direction"))) {
  91.                 rtl = true;
  92.             } else {
  93.                 rtl = false;
  94.             }
  95.         }
  96.     }
  97.  
  98.     /**
  99.      * @see org.xml.sax.ContentHandler#endElement(java.lang.String,
  100.      *      java.lang.String, java.lang.String)
  101.      */
  102.     public void endElement(String uri, String localName, String qName)
  103.             throws SAXException {
  104.         if ("pace".equals(qName)) {
  105.             PdfPCell cell = new PdfPCell();
  106.             cell.addElement(fs.process(buf.toString()));
  107.             cell.setPadding(3);
  108.             cell.setUseAscender(true);
  109.             cell.setUseDescender(true);
  110.             if (rtl) {
  111.                 cell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
  112.             }
  113.             table.addCell(language);
  114.             table.addCell(cell);
  115.             table.addCell(countries);
  116.         }
  117.     }
  118.  
  119.     /**
  120.      * @see org.xml.sax.ContentHandler#characters(char[], int, int)
  121.      */
  122.     public void characters(char[] ch, int start, int length)
  123.             throws SAXException {
  124.         buf.append(ch, start, length);
  125.     }
  126.  
  127.     /**
  128.      * Returns the table.
  129.      *
  130.      * @return the PdfPTable (get it after using the parser)
  131.      */
  132.     public PdfPTable getTable() {
  133.         return table;
  134.     }
  135.  
  136.     /**
  137.      * This example reads a text file written in UTF-8. Each line is a pipe
  138.      * delimited array containing the name of a language, the word 'peace'
  139.      * written in that language
  140.      *
  141.      * @param args
  142.      *            no arguments needed
  143.      */
  144.     public static void main(String[] args) {
  145.         // step 1
  146.         Document doc = new Document(PageSize.A4.rotate());
  147.         try {
  148.             // step 2
  149.             PdfWriter.getInstance(doc, new FileOutputStream(RESULT));
  150.             // step 3
  151.             doc.open();
  152.             // step 4
  153.             Peace p = new Peace();
  154.             SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
  155.             parser.parse(new InputSource(new FileInputStream(RESOURCE)), p);
  156.             doc.add(p.getTable());
  157.         } catch (Exception e) {
  158.             e.printStackTrace();
  159.         }
  160.         // step 5
  161.         doc.close();
  162.     }
  163. }
FontTypes.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using Ionic.Zip;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using iTextSharp.text;
  13. using iTextSharp.text.pdf;
  14. using iTextSharp.text.pdf.draw;
  15.  
  16. namespace kuujinbo.iTextInAction2Ed.Chapter11 {
  17.   public class FontTypes : IWriter {
  18. // ===========================================================================
  19.     /** Some text. */
  20.     public const string TEXT
  21.         = "quick brown fox jumps over the lazy dog\nQUICK BROWN FOX JUMPS OVER THE LAZY DOG";
  22.    
  23. /** Paths to and encodings of fonts we're going to use in this example;
  24.  * you may have to comment out some of the fonts to run example
  25. */
  26.     public readonly string[][] FONTS = {
  27.       new string[] {BaseFont.HELVETICA, BaseFont.WINANSI},
  28.       new string[] {Path.Combine(Utility.ResourceFonts, "cmr10.afm"), BaseFont.WINANSI},
  29.       new string[] {Path.Combine(Utility.ResourceFonts, "cmr10.pfm"), BaseFont.WINANSI},
  30.       //new string[] {"c:/windows/fonts/ARBLI__.TTF", BaseFont.WINANSI},
  31.       new string[] {"c:/windows/fonts/arial.ttf", BaseFont.WINANSI},
  32.       new string[] {"c:/windows/fonts/arial.ttf", BaseFont.IDENTITY_H},
  33.       new string[] {Path.Combine(Utility.ResourceFonts, "Puritan2.otf"), BaseFont.WINANSI},
  34.       new string[] {"c:/windows/fonts/msgothic.ttc,0", BaseFont.IDENTITY_H},
  35.       new string[] {"KozMinPro-Regular", "UniJIS-UCS2-H"}
  36.     };
  37. // ---------------------------------------------------------------------------
  38.     public void Write(Stream stream) {
  39.       // step 1
  40.       using (Document document = new Document()) {
  41.         // step 2
  42.         PdfWriter.GetInstance(document, stream);
  43.         // step 3
  44.         document.Open();
  45.         // step 4
  46.         BaseFont bf;
  47.         Font font;
  48.         for (int i = 0; i < FONTS.Length; i++) {
  49.           bf = BaseFont.CreateFont(FONTS[i][0], FONTS[i][1], BaseFont.EMBEDDED);
  50.           document.Add(new Paragraph(String.Format("Font file: {0} with encoding {1}",
  51.             FONTS[i][0], FONTS[i][1]
  52.           )));
  53.           document.Add(new Paragraph(
  54.             String.Format("iText class: {0}", bf.GetType().ToString()
  55.           )));
  56.           font = new Font(bf, 12);
  57.           document.Add(new Paragraph(TEXT, font));
  58.           document.Add(new LineSeparator(0.5f, 100, null, 0, -5));
  59.         }
  60.       }
  61.     }
  62. // ===========================================================================
  63.   }
  64. }
TTCExample.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using Ionic.Zip;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using iTextSharp.text;
  13. using iTextSharp.text.pdf;
  14.  
  15. namespace kuujinbo.iTextInAction2Ed.Chapter11 {
  16.   public class TTCExample : IWriter {
  17. // ===========================================================================
  18.   /** The path to the font. */
  19.     public const string FONT = "c:/windows/fonts/msgothic.ttc";
  20. // ---------------------------------------------------------------------------
  21.     public void Write(Stream stream) {
  22.       // step 1
  23.       using (Document document = new Document()) {
  24.         // step 2
  25.         PdfWriter.GetInstance(document, stream);
  26.         // step 3
  27.         document.Open();
  28.         // step 4
  29.         BaseFont bf;
  30.         Font font;
  31.         String[] names = BaseFont.EnumerateTTCNames(FONT);
  32.         for (int i = 0; i < names.Length; i++) {
  33.           bf = BaseFont.CreateFont(
  34.             String.Format("{0}, {1}", FONT, i),
  35.             BaseFont.IDENTITY_H, BaseFont.EMBEDDED
  36.           );
  37.           font = new Font(bf, 12);
  38.           document.Add(new Paragraph("font " + i + ": " + names[i], font));
  39.           document.Add(new Paragraph("Rash\u00f4mon", font));
  40.           document.Add(new Paragraph("Directed by Akira Kurosawa", font));
  41.           document.Add(new Paragraph("\u7f85\u751f\u9580", font));
  42.           document.Add(Chunk.NEWLINE);
  43.         }
  44.       }
  45.     }
  46. // ===========================================================================
  47.   }
  48. }
EncodingNames.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using Ionic.Zip;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using iTextSharp.text;
  13. using iTextSharp.text.pdf;
  14.  
  15. namespace kuujinbo.iTextInAction2Ed.Chapter11 {
  16.   public class EncodingNames : IWriter {
  17. // ===========================================================================
  18. /** The path to the font.
  19.  *  you may have to comment out some of the fonts to run example
  20. */
  21.     public readonly string[] FONT = {
  22.       //"c:/windows/fonts/ARBLI__.TTF",
  23.       Path.Combine(Utility.ResourceFonts, "Puritan2.otf"),
  24.       "c:/windows/fonts/arialbd.ttf"
  25.     };
  26. // ---------------------------------------------------------------------------
  27.     public void Write(Stream stream) {
  28.       // step 1
  29.       using (Document document = new Document()) {
  30.         // step 2
  31.         PdfWriter.GetInstance(document, stream);
  32.         // step 3
  33.         document.Open();
  34.         // step 4
  35.         for (int i = 0; i < FONT.Length; ++i) {
  36.           BaseFont bf = BaseFont.CreateFont(FONT[i],  BaseFont.WINANSI, BaseFont.EMBEDDED);
  37.           document.Add(new Paragraph("PostScript name: " + bf.PostscriptFontName));
  38.           document.Add(new Paragraph("Available code pages:"));
  39.           string[] encoding = bf.CodePagesSupported;
  40.           for (int j = 0; j < encoding.Length; j++) {
  41.             document.Add(new Paragraph("encoding[" + j + "] = " + encoding[j]));
  42.           }
  43.           document.Add(Chunk.NEWLINE);        
  44.         }
  45.       }
  46.     }
  47. // ===========================================================================
  48.   }
  49. }
EncodingExample.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using Ionic.Zip;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using iTextSharp.text;
  13. using iTextSharp.text.pdf;
  14.  
  15. namespace kuujinbo.iTextInAction2Ed.Chapter11 {
  16.   public class EncodingExample : IWriter {
  17. // ===========================================================================
  18.     /** The path to the font. */
  19.     public const string FONT = "c:/windows/fonts/arialbd.ttf";
  20.     /** Movie information. */
  21.     public readonly string[][] MOVIES = {
  22.       new string[] {
  23.         "Cp1252",
  24.         "A Very long Engagement (France)",
  25.         "directed by Jean-Pierre Jeunet",
  26.         "Un long dimanche de fian\u00e7ailles"
  27.       },
  28.       new string[] {
  29.         "Cp1250",
  30.         "No Man's Land (Bosnia-Herzegovina)",
  31.         "Directed by Danis Tanovic",
  32.         "Nikogar\u0161nja zemlja"
  33.       },
  34.       new string[] {
  35.         "Cp1251",
  36.         "You I Love (Russia)",
  37.         "directed by Olga Stolpovskaja and Dmitry Troitsky",
  38.         "\u042f \u043b\u044e\u0431\u043b\u044e \u0442\u0435\u0431\u044f"
  39.       },
  40.       new string[] {
  41.         "Cp1253",
  42.         "Brides (Greece)",
  43.         "directed by Pantelis Voulgaris",
  44.         "\u039d\u03cd\u03c6\u03b5\u03c2"
  45.       }
  46.     };
  47. // ---------------------------------------------------------------------------
  48.     public virtual void Write(Stream stream) {
  49.       // step 1
  50.       using (Document document = new Document()) {
  51.         // step 2
  52.         PdfWriter.GetInstance(document, stream);
  53.         // step 3
  54.         document.Open();
  55.         // step 4
  56.         BaseFont bf;
  57.         for (int i = 0; i < 4; i++) {
  58.           bf = BaseFont.CreateFont(FONT, MOVIES[i][0], BaseFont.EMBEDDED);
  59.           document.Add(new Paragraph(
  60.             "Font: " + bf.PostscriptFontName
  61.             + " with encoding: " + bf.Encoding
  62.           ));
  63.           document.Add(new Paragraph(MOVIES[i][1]));
  64.           document.Add(new Paragraph(MOVIES[i][2]));
  65.           document.Add(new Paragraph(MOVIES[i][3], new Font(bf, 12)));
  66.           document.Add(Chunk.NEWLINE);
  67.         }
  68.       }
  69.     }
  70. // ===========================================================================
  71.   }
  72. }
UnicodeExample.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using Ionic.Zip;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using iTextSharp.text;
  13. using iTextSharp.text.pdf;
  14.  
  15. namespace kuujinbo.iTextInAction2Ed.Chapter11 {
  16.   public class UnicodeExample : EncodingExample {
  17. // ===========================================================================
  18.     public override void Write(Stream stream) {
  19.       // step 1
  20.       using (Document document = new Document()) {
  21.         // step 2
  22.         PdfWriter.GetInstance(document, stream);
  23.         // step 3
  24.         document.Open();
  25.         // step 4
  26.         BaseFont bf;
  27.         for (int i = 0; i < 4; i++) {
  28.           bf = BaseFont.CreateFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  29.           document.Add(new Paragraph(
  30.             "Font: " + bf.PostscriptFontName
  31.             + " with encoding: " + bf.Encoding
  32.           ));
  33.           document.Add(new Paragraph(MOVIES[i][1]));
  34.           document.Add(new Paragraph(MOVIES[i][2]));
  35.           document.Add(new Paragraph(MOVIES[i][3], new Font(bf, 12)));
  36.           document.Add(Chunk.NEWLINE);
  37.         }
  38.       }
  39.     }
  40. // ===========================================================================
  41.   }
  42. }
FontFileAndSizes.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using Ionic.Zip;
  10. using System.Collections.Generic;
  11.  
  12. using System.Linq;
  13. using iTextSharp.text;
  14. using iTextSharp.text.pdf;
  15.  
  16. namespace kuujinbo.iTextInAction2Ed.Chapter11 {
  17.   public class FontFileAndSizes : IWriter {
  18. // ===========================================================================
  19.     /** The names of the resulting PDF files. */
  20.     public string[] RESULT {
  21.       get { return new string[] {
  22.           "font_not_embedded.pdf",
  23.           "font_embedded.pdf",
  24.           "font_embedded_less_glyphs.pdf",
  25.           "font_compressed.pdf",
  26.           "font_full.pdf"
  27.         };
  28.       }
  29.     }
  30.     /** The path to the font. */
  31.     public const string FONT = "c:/windows/fonts/arial.ttf";
  32.     /** Some text. */
  33.     public const string TEXT
  34.         = "quick brown fox jumps over the lazy dog";
  35.     /** Some text. */
  36.     public const string OOOO
  37.         = "ooooo ooooo ooo ooooo oooo ooo oooo ooo";
  38. // ---------------------------------------------------------------------------
  39.     public void Write(Stream stream) {
  40.       using (ZipFile zip = new ZipFile()) {
  41.         FontFileAndSizes ffs = new FontFileAndSizes();
  42.         BaseFont bf;
  43.         bf = BaseFont.CreateFont(FONT, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
  44.         zip.AddEntry(RESULT[0], ffs.CreatePdf(bf, TEXT));
  45.         bf = BaseFont.CreateFont(FONT, BaseFont.WINANSI, BaseFont.EMBEDDED);
  46.         zip.AddEntry(RESULT[1], ffs.CreatePdf(bf, TEXT));
  47.         zip.AddEntry(RESULT[2], ffs.CreatePdf(bf, OOOO));
  48.         bf = BaseFont.CreateFont(FONT, BaseFont.WINANSI, BaseFont.EMBEDDED);
  49.         bf.CompressionLevel = 9;
  50.         zip.AddEntry(RESULT[3], ffs.CreatePdf(bf, TEXT));
  51.         bf = BaseFont.CreateFont(FONT, BaseFont.WINANSI, BaseFont.EMBEDDED);
  52.         bf.Subset = false;
  53.         zip.AddEntry(RESULT[4], ffs.CreatePdf(bf, TEXT));
  54.         zip.Save(stream);            
  55.       }
  56.     }
  57. // ---------------------------------------------------------------------------    
  58.     /**
  59.      * Creates a PDF document.
  60.      */
  61.     public byte[] CreatePdf(BaseFont bf, String text) {
  62.       using (MemoryStream ms = new MemoryStream()) {
  63.         using (Document document = new Document()) {
  64.           PdfWriter.GetInstance(document, ms);
  65.           document.Open();
  66.           document.Add(new Paragraph(text, new Font(bf, 12)));
  67.         }
  68.         return ms.ToArray();
  69.       }
  70.     }    
  71. // ===========================================================================
  72.   }
  73. }
Type3Example.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using Ionic.Zip;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using iTextSharp.text;
  13. using iTextSharp.text.pdf;
  14.  
  15. namespace kuujinbo.iTextInAction2Ed.Chapter11 {
  16.   public class Type3Example : IWriter {
  17. // ===========================================================================
  18.     public void Write(Stream stream) {
  19.       // step 1
  20.       using (Document document = new Document()) {
  21.         // step 2
  22.         PdfWriter writer = PdfWriter.GetInstance(document, stream);
  23.         // step 3
  24.         document.Open();
  25.         // step 4
  26.         Type3Font t3 = new Type3Font(writer, true);
  27.         PdfContentByte d = t3.DefineGlyph('D', 600, 0, 0, 600, 700);
  28.         d.SetColorStroke(new BaseColor(0xFF, 0x00, 0x00));
  29.         d.SetColorFill(new GrayColor(0.7f));
  30.         d.SetLineWidth(100);
  31.         d.MoveTo(5, 5);
  32.         d.LineTo(300, 695);
  33.         d.LineTo(595, 5);
  34.         d.ClosePathFillStroke();
  35.         PdfContentByte s = t3.DefineGlyph('S', 600, 0, 0, 600, 700);
  36.         s.SetColorStroke(new BaseColor(0x00, 0x80, 0x80));
  37.         s.SetLineWidth(100);
  38.         s.MoveTo(595,5);
  39.         s.LineTo(5, 5);
  40.         s.LineTo(300, 350);
  41.         s.LineTo(5, 695);
  42.         s.LineTo(595, 695);
  43.         s.Stroke();
  44.         Font f = new Font(t3, 12);
  45.         Paragraph p = new Paragraph();
  46.         p.Add("This is a String with a Type3 font that contains a fancy Delta (");
  47.         p.Add(new Chunk("D", f));
  48.         p.Add(") and a custom Sigma (");
  49.         p.Add(new Chunk("S", f));
  50.         p.Add(").");
  51.         document.Add(p);        
  52.       }
  53.     }
  54. // ===========================================================================
  55.   }
  56. }
CJKExample.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using Ionic.Zip;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using iTextSharp.text;
  13. using iTextSharp.text.pdf;
  14.  
  15. namespace kuujinbo.iTextInAction2Ed.Chapter11 {
  16.   public class CJKExample : IWriter {
  17. // ===========================================================================
  18.     /** Movies, their director and original title */
  19.     public readonly string[][] MOVIES = {
  20.       new string[] {
  21.         "STSong-Light", "UniGB-UCS2-H",
  22.         "Movie title: House of The Flying Daggers (China)",
  23.         "directed by Zhang Yimou",
  24.         "\u5341\u950a\u57cb\u4f0f"
  25.       },
  26.       new string[] {
  27.         "KozMinPro-Regular", "UniJIS-UCS2-H",
  28.         "Movie title: Nobody Knows (Japan)",
  29.         "directed by Hirokazu Koreeda",
  30.         "\u8ab0\u3082\u77e5\u3089\u306a\u3044"
  31.       },
  32.       new string[] {
  33.         "HYGoThic-Medium", "UniKS-UCS2-H",
  34.         "Movie title: '3-Iron' aka 'Bin-jip' (South-Korea)",
  35.         "directed by Kim Ki-Duk",
  36.         "\ube48\uc9d1"
  37.       }
  38.     };
  39. // ---------------------------------------------------------------------------
  40.     public void Write(Stream stream) {
  41.       // step 1
  42.       using (Document document = new Document()) {
  43.         // step 2
  44.         PdfWriter.GetInstance(document, stream);
  45.         // step 3
  46.         document.Open();
  47.         // step 4
  48.         BaseFont bf;
  49.         Font font;
  50.         for (int i = 0; i < 3; i++) {
  51.           bf = BaseFont.CreateFont(
  52.             MOVIES[i][0], MOVIES[i][1], BaseFont.NOT_EMBEDDED
  53.           );
  54.           font = new Font(bf, 12);
  55.           document.Add(new Paragraph(bf.PostscriptFontName, font));
  56.           for (int j = 2; j < 5; j++) {
  57.             document.Add(new Paragraph(MOVIES[i][j], font));
  58.           }
  59.           document.Add(Chunk.NEWLINE);
  60.         }        
  61.       }
  62.     }
  63. // ===========================================================================
  64.   }
  65. }
VerticalTextExample1.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using Ionic.Zip;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using iTextSharp.text;
  13. using iTextSharp.text.pdf;
  14.  
  15. namespace kuujinbo.iTextInAction2Ed.Chapter11 {
  16.   public class VerticalTextExample1 : IWriter {
  17. // ===========================================================================
  18.     /** A Japanese String. */
  19.     public const string MOVIE = "\u4e03\u4eba\u306e\u4f8d";
  20.     /** The facts. */
  21.     public const string TEXT1
  22.     = "You embarrass me. You're overestimating me. "
  23.     + "Listen, I'm not a man with any special skill, "
  24.     + "but I've had plenty of experience in battles; losing battles, all of them.";
  25.     /** The conclusion. */
  26.     public const string TEXT2
  27.     = "In short, that's all I am. Drop such an idea for your own good.";
  28. // ---------------------------------------------------------------------------
  29.     public virtual void Write(Stream stream) {
  30.       // step 1
  31.       using (Document document = new Document(new Rectangle(420, 600))) {
  32.         // step 2
  33.         PdfWriter writer = PdfWriter.GetInstance(document, stream);
  34.         // step 3
  35.         document.Open();
  36.         // step 4
  37.         BaseFont bf = BaseFont.CreateFont(
  38.           "KozMinPro-Regular", "UniJIS-UCS2-V", BaseFont.NOT_EMBEDDED
  39.         );
  40.         Font font = new Font(bf, 20);
  41.         VerticalText vt = new VerticalText(writer.DirectContent);
  42.         vt.SetVerticalLayout(390, 570, 540, 12, 30);
  43.         vt.AddText(new Chunk(MOVIE, font));
  44.         vt.Go();
  45.         vt.AddText(new Phrase(TEXT1, font));
  46.         vt.Go();
  47.         vt.Alignment = Element.ALIGN_RIGHT;
  48.         vt.AddText(new Phrase(TEXT2, font));
  49.         vt.Go();
  50.       }
  51.     }
  52. // ===========================================================================
  53.   }
  54. }
VerticalTextExample2.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using Ionic.Zip;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using iTextSharp.text;
  13. using iTextSharp.text.pdf;
  14.  
  15. namespace kuujinbo.iTextInAction2Ed.Chapter11 {
  16.   public class VerticalTextExample2 : VerticalTextExample1 {
  17. // ===========================================================================
  18.     public override void Write(Stream stream) {
  19.       // step 1
  20.       using (Document document = new Document(new Rectangle(420, 600))) {
  21.         // step 2
  22.         PdfWriter writer = PdfWriter.GetInstance(document, stream);
  23.         // step 3
  24.         document.Open();
  25.         // step 4
  26.         BaseFont bf = BaseFont.CreateFont(
  27.           "KozMinPro-Regular", "Identity-V", BaseFont.NOT_EMBEDDED
  28.         );
  29.         Font font = new Font(bf, 20);
  30.         VerticalText vt = new VerticalText(writer.DirectContent);
  31.         vt.SetVerticalLayout(390, 570, 540, 12, 30);
  32.         font = new Font(bf, 20);
  33.         vt.AddText(new Phrase(ConvertCIDs(TEXT1), font));
  34.         vt.Go();
  35.         vt.Alignment = Element.ALIGN_RIGHT;
  36.         vt.AddText(new Phrase(ConvertCIDs(TEXT2), font));
  37.         vt.Go();
  38.       }
  39.     }
  40. // ---------------------------------------------------------------------------
  41. /**
  42.  * Converts the CIDs of the horizontal characters of a String
  43.  * into a String with vertical characters.
  44.  * @param text The String with the horizontal characters
  45.  * @return A String with vertical characters
  46.  */
  47.     public string ConvertCIDs(string text) {
  48.       char[] cid = text.ToCharArray();
  49.       for (int k = 0; k < cid.Length; ++k) {
  50.         char c = cid[k];
  51.         cid[k] = c == '\n' ? '\uff00' : (char) (c - ' ' + 8720);
  52.       }
  53.       return new String(cid);
  54.     }
  55. // ===========================================================================
  56.   }
  57. }
RightToLeftExample.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using Ionic.Zip;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using iTextSharp.text;
  13. using iTextSharp.text.pdf;
  14.  
  15. namespace kuujinbo.iTextInAction2Ed.Chapter11 {
  16.   public class RightToLeftExample : IWriter {
  17. // ===========================================================================
  18.     /** A movie title. */
  19.     public const string MOVIE =
  20.     "\u05d4\u05d0\u05e1\u05d5\u05e0\u05d5\u05ea \u05e9\u05dc \u05e0\u05d9\u05e0\u05d4";
  21. // ---------------------------------------------------------------------------
  22.     public void Write(Stream stream) {
  23.       // step 1
  24.       using (Document document = new Document(PageSize.A4)) {
  25.         // step 2
  26.         PdfWriter writer = PdfWriter.GetInstance(document, stream);
  27.         // step 3
  28.         document.Open();
  29.         // step 4
  30.         BaseFont bf = BaseFont.CreateFont(
  31.           "c:/windows/fonts/arial.ttf", BaseFont.IDENTITY_H, true
  32.         );
  33.         Font font = new Font(bf, 14);
  34.         document.Add(new Paragraph("Movie title: Nina's Tragedies"));
  35.         document.Add(new Paragraph("directed by Savi Gabizon"));
  36.         ColumnText column = new ColumnText(writer.DirectContent);
  37.         column.SetSimpleColumn(36, 770, 569, 36);
  38.         column.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
  39.         column.AddElement(new Paragraph(MOVIE, font));
  40.         column.Go();
  41.       }
  42.     }
  43. // ===========================================================================
  44.   }
  45. }
SayPeace.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using Ionic.Zip;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Xml;
  14. using iTextSharp.text;
  15. using iTextSharp.text.pdf;
  16.  
  17. namespace kuujinbo.iTextInAction2Ed.Chapter11 {
  18.   public class SayPeace : IWriter {
  19. // ===========================================================================
  20.     /** The resulting PDF file. */
  21.     public const String RESULT = "say_peace.pdf";
  22.     /** The XML file with the text. */
  23.     public static string RESOURCE = Path.Combine(
  24.       Utility.ResourceXml, "say_peace.xml"
  25.     );
  26.     /** The font that is used for the peace message. */
  27.     public Font f;
  28.     /** The document to which we are going to add our message. */
  29.     protected Document document;
  30.  
  31.     /** The StringBuilder that holds the characters. */
  32.     protected StringBuilder buf = new StringBuilder();
  33.    
  34.     /** The table that holds the text. */
  35.     protected PdfPTable table;
  36.    
  37.     /** The current cell. */
  38.     protected PdfPCell cell;
  39. // ---------------------------------------------------------------------------
  40.     public void Write(Stream stream) {
  41.       using (ZipFile zip = new ZipFile()) {
  42.         zip.AddFile(RESOURCE, "");
  43.        
  44.         using (MemoryStream ms = new MemoryStream()) {
  45.           // step 1
  46.           using (document = new Document()) {
  47.             // step 2
  48.             PdfWriter.GetInstance(document, ms);
  49.             // step 3
  50.             document.Open();
  51.             // step 4
  52.             f = new Font(BaseFont.CreateFont("c:/windows/fonts/arialuni.ttf",
  53.               BaseFont.IDENTITY_H, BaseFont.EMBEDDED
  54.             ));
  55.             using (XmlReader xr = XmlReader.Create(RESOURCE)) {
  56.               table = new PdfPTable(1);
  57.               xr.MoveToContent();
  58.               while (xr.Read()) {
  59.                 switch (xr.NodeType) {
  60.                   case XmlNodeType.Element:
  61.                     StartElement(xr);
  62.                     break;
  63.                   case XmlNodeType.Text:
  64.                     buf.Append(xr.Value.Trim());
  65.                     break;
  66.                   case XmlNodeType.EndElement:
  67.                     EndElement(xr.Name);
  68.                     break;
  69.                 }
  70.               }
  71.             }
  72.           }
  73.           zip.AddEntry(RESULT, ms.ToArray());
  74.         }
  75.         zip.Save(stream);            
  76.       }
  77.     }      
  78. // ---------------------------------------------------------------------------
  79.     public void StartElement(XmlReader xr) {
  80.       string name =  xr.Name;
  81.       if ("message".Equals(name)) {
  82.         buf = new StringBuilder();
  83.         cell = new PdfPCell();
  84.         cell.Border = PdfPCell.NO_BORDER;
  85.         string direction = xr.GetAttribute("direction") != null
  86.           ? xr.GetAttribute("direction")
  87.           : ""
  88.         ;
  89.         if ("RTL".Equals(direction)) {
  90.           cell.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
  91.         }
  92.       }
  93.       else if ("pace".Equals(name)) {
  94.         table = new PdfPTable(1);
  95.         table.WidthPercentage = 100;
  96.       }
  97.     }
  98. // ---------------------------------------------------------------------------
  99.     public void EndElement(string name) {
  100.       if ("big".Equals(name)) {
  101.         Chunk bold = new Chunk(Strip(buf), f);
  102.         bold.SetTextRenderMode(
  103.           PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE, 0.5f,
  104.           GrayColor.GRAYBLACK
  105.         );
  106.         Paragraph p = new Paragraph(bold);
  107.         p.Alignment = Element.ALIGN_LEFT;
  108.         cell.AddElement(p);
  109.         buf = new StringBuilder();
  110.       }
  111.       else if ("message".Equals(name)) {
  112.         Paragraph p = new Paragraph(Strip(buf), f);
  113.         p.Alignment = Element.ALIGN_LEFT;
  114.         cell.AddElement(p);
  115.         table.AddCell(cell);
  116.         buf = new StringBuilder();
  117.       }
  118.       else if ("pace".Equals(name)) {
  119.         document.Add(table);
  120.       }
  121.     }
  122. // ---------------------------------------------------------------------------    
  123.   /**
  124.    * Replaces all the newline characters by a space.
  125.    *
  126.    * @param buf the original StringBuffer
  127.    * @return a String without newlines
  128.    */
  129.     protected string Strip(StringBuilder buf) {
  130.       buf.Replace("\n", " ");
  131.       return buf.ToString();
  132.     }    
  133. // ===========================================================================
  134.   }
  135. }
Diacritics1.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using Ionic.Zip;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using iTextSharp.text;
  13. using iTextSharp.text.pdf;
  14.  
  15. namespace kuujinbo.iTextInAction2Ed.Chapter11 {
  16.   public class Diacritics1 : IWriter {
  17. // ===========================================================================
  18.     /** A movie title. */
  19.     public const string MOVIE =
  20.     "\u0e1f\u0e49\u0e32\u0e17\u0e30\u0e25\u0e32\u0e22\u0e42\u0e08\u0e23";
  21.     /** Movie poster */
  22.     public static string POSTER = Path.Combine(
  23.       Utility.ResourcePosters, "0269217.jpg"
  24.     );
  25.     /** Fonts */
  26.     public readonly string[] FONTS = {
  27.       "c:/windows/fonts/angsa.ttf",
  28.       "c:/windows/fonts/arialuni.ttf"
  29.     };
  30. // ---------------------------------------------------------------------------
  31.     public void Write(Stream stream) {
  32.       // step 1
  33.       using (Document document = new Document()) {
  34.         // step 2
  35.         PdfWriter.GetInstance(document, stream);
  36.         // step 3
  37.         document.Open();
  38.         // step 4
  39.         BaseFont bf;
  40.         Font font;
  41.         Image img = Image.GetInstance(POSTER);
  42.         img.ScalePercent(50);
  43.         img.BorderWidth = 18f;
  44.         img.Border = Image.BOX;
  45.         img.BorderColor = GrayColor.GRAYWHITE;
  46.         img.Alignment = Element.ALIGN_LEFT | Image.TEXTWRAP;
  47.         document.Add(img);
  48.         document.Add(new Paragraph(
  49.           "Movie title: Tears of the Black Tiger (Thailand)"
  50.         ));
  51.         document.Add(new Paragraph("directed by Wisit Sasanatieng"));
  52.         for (int i = 0; i < 2; i++) {
  53.           bf = BaseFont.CreateFont(
  54.             FONTS[i], BaseFont.IDENTITY_H, BaseFont.EMBEDDED
  55.           );
  56.           document.Add(new Paragraph("Font: " + bf.PostscriptFontName));
  57.           font = new Font(bf, 20);
  58.           document.Add(new Paragraph(MOVIE, font));
  59.         }
  60.       }
  61.     }
  62. // ===========================================================================
  63.   }
  64. }
Diacritics2.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using Ionic.Zip;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using iTextSharp.text;
  13. using iTextSharp.text.pdf;
  14.  
  15. namespace kuujinbo.iTextInAction2Ed.Chapter11 {
  16.   public class Diacritics2 : IWriter {
  17. // ===========================================================================
  18.     /** A movie title. */
  19.     public const string MOVIE = "Tomten \u00a8ar far till alla barnen";
  20.     /** Fonts */
  21.     public readonly string[] FONTS = {
  22.       "c:/windows/fonts/arial.ttf",
  23.       "c:/windows/fonts/cour.ttf"
  24.     };
  25. // ---------------------------------------------------------------------------
  26.     public void Write(Stream stream) {
  27.       // step 1
  28.       using (Document document = new Document()) {
  29.         // step 2
  30.         PdfWriter.GetInstance(document, stream);
  31.         // step 3
  32.         document.Open();
  33.         // step 4
  34.         document.Add(new Paragraph("Movie title: In Bed With Santa (Sweden)"));
  35.         document.Add(new Paragraph("directed by Kjell Sundvall"));
  36.         BaseFont bf = BaseFont.CreateFont(
  37.           FONTS[0], BaseFont.CP1252, BaseFont.EMBEDDED
  38.         );
  39.         Font font = new Font(bf, 12);
  40.         bf.SetCharAdvance('\u00a8', -100);
  41.         document.Add(new Paragraph(MOVIE, font));
  42.         bf = BaseFont.CreateFont(FONTS[1], BaseFont.CP1252, BaseFont.EMBEDDED);
  43.         bf.SetCharAdvance('\u00a8', 0);
  44.         font = new Font(bf, 12);
  45.         document.Add(new Paragraph(MOVIE, font));
  46.       }
  47.     }
  48. // ===========================================================================
  49.   }
  50. }
Monospace.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using Ionic.Zip;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using iTextSharp.text;
  13. using iTextSharp.text.pdf;
  14.  
  15. namespace kuujinbo.iTextInAction2Ed.Chapter11 {
  16.   public class Monospace : IWriter {
  17. // ===========================================================================
  18.     /** A movie title. */
  19.     public const string MOVIE = "Aanrijding in Moscou";
  20. // ---------------------------------------------------------------------------
  21.     public void Write(Stream stream) {
  22.       // step 1
  23.       using (Document document = new Document()) {
  24.         // step 2
  25.         PdfWriter.GetInstance(document, stream);
  26.         // step 3
  27.         document.Open();
  28.         // step 4
  29.         BaseFont bf1 = BaseFont.CreateFont(
  30.           "c:/windows/fonts/arial.ttf",
  31.           BaseFont.CP1252, BaseFont.EMBEDDED
  32.         );
  33.         Font font1 = new Font(bf1, 12);
  34.         document.Add(new Paragraph("Movie title: Moscou, Belgium", font1));
  35.         document.Add(new Paragraph(
  36.           "directed by Christophe Van Rompaey", font1
  37.         ));
  38.         document.Add(new Paragraph(MOVIE, font1));
  39.         BaseFont bf2 = BaseFont.CreateFont(
  40.           "c:/windows/fonts/cour.ttf",
  41.           BaseFont.CP1252, BaseFont.EMBEDDED
  42.         );
  43.         Font font2 = new Font(bf2, 12);
  44.         document.Add(new Paragraph(MOVIE, font2));
  45.         BaseFont bf3 = BaseFont.CreateFont(
  46.           "c:/windows/fonts/arialbd.ttf",
  47.           BaseFont.CP1252, BaseFont.EMBEDDED
  48.         );
  49.         Font font3 = new Font(bf3, 12);
  50.         int[] widths = bf3.Widths;
  51.         for (int k = 0; k < widths.Length; ++k) {
  52.           if (widths[k] != 0) widths[k] = 600;
  53.         }
  54.         bf3.ForceWidthsOutput = true;
  55.         document.Add(new Paragraph(MOVIE, font3));
  56.       }
  57.     }
  58. // ===========================================================================
  59.   }
  60. }
ExtraCharSpace.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using Ionic.Zip;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using iTextSharp.text;
  13. using iTextSharp.text.pdf;
  14.  
  15. namespace kuujinbo.iTextInAction2Ed.Chapter11 {
  16.   public class ExtraCharSpace : IWriter {
  17. // ===========================================================================
  18.     /** A movie title. */
  19.     public const string MOVIE = "Aanrijding in Moscou";      
  20. // ---------------------------------------------------------------------------
  21.     public void Write(Stream stream) {
  22.       // step 1
  23.       using (Document document = new Document()) {
  24.         // step 2
  25.         PdfWriter.GetInstance(document, stream);
  26.         // step 3
  27.         document.Open();
  28.         // step 4
  29.         BaseFont bf1 = BaseFont.CreateFont(
  30.           "c:/windows/fonts/arial.ttf",
  31.           BaseFont.CP1252, BaseFont.EMBEDDED
  32.         );
  33.         Font font1 = new Font(bf1, 12);
  34.         document.Add(new Paragraph("Movie title: Moscou, Belgium", font1));
  35.         document.Add(new Paragraph(
  36.           "directed by Christophe Van Rompaey", font1
  37.         ));
  38.         Chunk chunk = new Chunk(MOVIE, font1);
  39.         chunk.SetCharacterSpacing(10);
  40.         document.Add(new Paragraph(chunk));
  41.       }
  42.     }
  43. // ===========================================================================
  44.   }
  45. }
Ligatures1.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using Ionic.Zip;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using iTextSharp.text;
  13. using iTextSharp.text.pdf;
  14.  
  15. namespace kuujinbo.iTextInAction2Ed.Chapter11 {
  16.   public class Ligatures1 : IWriter {
  17. // ===========================================================================
  18.     public void Write(Stream stream) {
  19.       // step 1
  20.       using (Document document = new Document()) {
  21.         // step 2
  22.         PdfWriter.GetInstance(document, stream);
  23.         // step 3
  24.         document.Open();
  25.         // step 4
  26.         BaseFont bf = BaseFont.CreateFont(
  27.           "c:/windows/fonts/arial.ttf", BaseFont.CP1252, BaseFont.EMBEDDED
  28.         );
  29.         Font font = new Font(bf, 12);
  30.         document.Add(new Paragraph(
  31.           "Movie title: Love at First Hiccough (Denmark)", font
  32.         ));
  33.         document.Add(new Paragraph("directed by Tomas Villum Jensen", font));
  34.         document.Add(new Paragraph("K\u00e6rlighed ved f\u00f8rste hik", font));
  35.         document.Add(new Paragraph(
  36.           Ligaturize("Kaerlighed ved f/orste hik"), font
  37.         ));
  38.       }
  39.     }
  40. // ---------------------------------------------------------------------------
  41.   /**
  42.    * Method that makes the ligatures for the combinations 'a' and 'e'
  43.    * and for '/' and 'o'.
  44.    * @param s a String that may have the combinations ae or /o
  45.    * @return a String where the combinations are replaced by a unicode character
  46.    */
  47.     public String Ligaturize(string s) {
  48.       int pos;
  49.       //while ((pos = s.IndexOf("ae")) > -1) {
  50.       //  s = s.Substring(0, pos) + '\u00e6' + s.Substring(pos + 2);
  51.       //}
  52.       while ((pos = s.IndexOf("/o")) > -1) {
  53.         s = s.Substring(0, pos) + "\u00f8" + s.Substring(pos + 2);
  54.       }
  55.       return s;
  56.     }    
  57. // ===========================================================================
  58.   }
  59. }
Ligatures2.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using Ionic.Zip;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using iTextSharp.text;
  13. using iTextSharp.text.pdf;
  14.  
  15. namespace kuujinbo.iTextInAction2Ed.Chapter11 {
  16.   public class Ligatures2 : IWriter {
  17. // ===========================================================================
  18.     /** Correct movie title. */
  19.     public const string MOVIE =
  20.     "\u0644\u0648\u0631\u0627\u0646\u0633 \u0627\u0644\u0639\u0631\u0628";
  21.     /** Correct movie title. */
  22.     public const string MOVIE_WITH_SPACES =
  23.     "\u0644 \u0648 \u0631 \u0627 \u0646 \u0633   \u0627 \u0644 \u0639 \u0631 \u0628";
  24. // ---------------------------------------------------------------------------
  25.     public void Write(Stream stream) {
  26.       // step 1
  27.       using (Document document = new Document()) {
  28.         // step 2
  29.         PdfWriter writer = PdfWriter.GetInstance(document, stream);
  30.         // step 3
  31.         document.Open();
  32.         // step 4
  33.         BaseFont bf = BaseFont.CreateFont(
  34.           "c:/windows/fonts/arialuni.ttf",
  35.           BaseFont.IDENTITY_H, BaseFont.EMBEDDED
  36.         );
  37.         Font font = new Font(bf, 20);
  38.         document.Add(new Paragraph("Movie title: Lawrence of Arabia (UK)"));
  39.         document.Add(new Paragraph("directed by David Lean"));
  40.         document.Add(new Paragraph("Wrong: " + MOVIE, font));
  41.         ColumnText column = new ColumnText(writer.DirectContent);
  42.         column.SetSimpleColumn(36, 730, 569, 36);
  43.         column.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
  44.         column.AddElement(new Paragraph("Wrong: " + MOVIE_WITH_SPACES, font));
  45.         column.AddElement(new Paragraph(MOVIE, font));
  46.         column.Go();
  47.       }
  48.     }
  49. // ===========================================================================
  50.   }
  51. }
FontFactoryExample.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using Ionic.Zip;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using iTextSharp.text;
  13. using iTextSharp.text.pdf;
  14.  
  15. namespace kuujinbo.iTextInAction2Ed.Chapter11 {
  16.   public class FontFactoryExample : IWriter {
  17. // ===========================================================================
  18.     public void Write(Stream stream) {
  19.       // step 1
  20.       using (Document document = new Document()) {
  21.         // step 2
  22.         PdfWriter.GetInstance(document, stream);
  23.         // step 3
  24.         document.Open();
  25.         // step 4
  26.         Font font = FontFactory.GetFont("Times-Roman");
  27.         document.Add(new Paragraph("Times-Roman", font));
  28.         Font fontbold = FontFactory.GetFont("Times-Roman", 12, Font.BOLD);
  29.         document.Add(new Paragraph("Times-Roman, Bold", fontbold));
  30.         document.Add(Chunk.NEWLINE);
  31.         FontFactory.Register("c:/windows/fonts/garabd.ttf", "my_bold_font");
  32.         Font myBoldFont = FontFactory.GetFont("my_bold_font");
  33.         BaseFont bf = myBoldFont.BaseFont;
  34.         document.Add(new Paragraph(bf.PostscriptFontName, myBoldFont));
  35.         String[][] name = bf.FullFontName;
  36.         for (int i = 0; i < name.Length; i++) {
  37.           document.Add(new Paragraph(
  38.             name[i][3] + " (" + name[i][0]
  39.             + "; " + name[i][1] + "; " + name[i][2] + ")"
  40.           ));
  41.         }
  42.         Font myBoldFont2 = FontFactory.GetFont("Garamond vet");
  43.         document.Add(new Paragraph("Garamond Vet", myBoldFont2));
  44.         document.Add(Chunk.NEWLINE);
  45.         document.Add(new Paragraph("Registered fonts:"));
  46.         FontFactory.RegisterDirectory(Utility.ResourceFonts);
  47. /*              
  48.         string fontDirectory = Utility.ResourceFonts;
  49.         FontFactory.RegisterDirectory(
  50.           fontDirectory.Substring(0, fontDirectory.Length - 3
  51.         ));
  52.  */
  53.         foreach (String f in FontFactory.RegisteredFonts) {
  54.           document.Add(new Paragraph(
  55.             f, FontFactory.GetFont(f, "", BaseFont.EMBEDDED
  56.           )));
  57.         }
  58.         document.Add(Chunk.NEWLINE);
  59.         Font cmr10 = FontFactory.GetFont("cmr10");
  60.         cmr10.BaseFont.PostscriptFontName = "Computer Modern Regular";
  61.         Font computerModern = FontFactory.GetFont(
  62.           "Computer Modern Regular", "", BaseFont.EMBEDDED
  63.         );
  64.         document.Add(new Paragraph("Computer Modern", computerModern));
  65.         document.Add(Chunk.NEWLINE);
  66.         FontFactory.RegisterDirectories();
  67.         foreach (String f in FontFactory.RegisteredFamilies) {
  68.           document.Add(new Paragraph(f));
  69.         }
  70.         document.Add(Chunk.NEWLINE);
  71.         Font garamond = FontFactory.GetFont(
  72.           "garamond", BaseFont.WINANSI, BaseFont.EMBEDDED
  73.         );
  74.         document.Add(new Paragraph("Garamond", garamond));
  75.         Font garamondItalic = FontFactory.GetFont(
  76.           "Garamond", BaseFont.WINANSI, BaseFont.EMBEDDED, 12, Font.ITALIC
  77.         );
  78.         document.Add(new Paragraph("Garamond-Italic", garamondItalic));
  79.       }
  80.     }
  81. // ===========================================================================
  82.   }
  83. }
FontSelectionExample.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using Ionic.Zip;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using iTextSharp.text;
  13. using iTextSharp.text.pdf;
  14.  
  15. namespace kuujinbo.iTextInAction2Ed.Chapter11 {
  16.   public class FontSelectionExample : IWriter {
  17. // ===========================================================================
  18.     /** Some text */
  19.     public readonly string TEXT =
  20.     "These are the protagonists in 'Hero', a movie by Zhang Yimou:\n"
  21.     + "\u7121\u540d (Nameless), \u6b98\u528d (Broken Sword), "
  22.     + "\u98db\u96ea (Flying Snow), \u5982\u6708 (Moon), "
  23.     + "\u79e6\u738b (the King), and \u9577\u7a7a (Sky).";
  24. // ---------------------------------------------------------------------------
  25.     public void Write(Stream stream) {
  26.       // step 1
  27.       using (Document document = new Document(PageSize.A4)) {
  28.         // step 2
  29.         PdfWriter.GetInstance(document, stream);
  30.         // step 3
  31.         document.Open();
  32.         // step 4
  33.         FontSelector selector = new FontSelector();
  34.         selector.AddFont(FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12));
  35.         selector.AddFont(FontFactory.GetFont(
  36.           "MSung-Light", "UniCNS-UCS2-H", BaseFont.NOT_EMBEDDED
  37.         ));
  38.         Phrase ph = selector.Process(TEXT);
  39.         document.Add(new Paragraph(ph));
  40.       }
  41.     }
  42. // ===========================================================================
  43.   }
  44. }
Peace.cs
  1. /*
  2.  * This class is part of the book "iText in Action - 2nd Edition"
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)
  4.  * For more info, go to: http://itextpdf.com/examples/
  5.  * This example only works with the AGPL version of iText.
  6.  */
  7. using System;
  8. using System.IO;
  9. using Ionic.Zip;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Xml;
  14. using iTextSharp.text;
  15. using iTextSharp.text.pdf;
  16.  
  17. namespace kuujinbo.iTextInAction2Ed.Chapter11 {
  18.   public class Peace : IWriter {
  19. // ===========================================================================
  20.     /** The resulting PDF file. */
  21.     public const String RESULT = "peace.pdf";
  22.     /** The XML file with the text. */
  23.     public static string RESOURCE = Path.Combine(
  24.       Utility.ResourceXml, "peace.xml"
  25.     );
  26.     public readonly string FONT_PATH = Utility.ResourceFonts;
  27.     /** Paths to and encodings of fonts we're going to use in this example */
  28.     public string[][] FONTS {
  29.       get { return new string[][] {
  30.           new string[] {
  31.             "c:/windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H
  32.           },
  33.           new string[] {
  34.             string.Format(FONT_PATH, "abserif4_5.ttf"), BaseFont.IDENTITY_H
  35.           },
  36.           new string[] {
  37.             string.Format(FONT_PATH, "damase.ttf"), BaseFont.IDENTITY_H
  38.           },
  39.           new string[] {
  40.             string.Format(FONT_PATH, "fsex2p00_public.ttf"), BaseFont.IDENTITY_H
  41.           }
  42.         };
  43.       }
  44.     }
  45.     /** Holds he fonts that can be used for the peace message. */
  46.     public FontSelector fs;
  47.  
  48.     /** The columns that contains the message. */
  49.     protected PdfPTable table;
  50.  
  51.     /** The language. */
  52.     protected String language;
  53.  
  54.     /** The countries. */
  55.     protected String countries;
  56.  
  57.     /** Indicates when the text should be written from right to left. */
  58.     protected bool rtl;
  59.    
  60.     /** The StringBuilder that holds the characters. */
  61.     protected StringBuilder buf = new StringBuilder();
  62. // ---------------------------------------------------------------------------
  63.     public void Write(Stream stream) {
  64.       using (ZipFile zip = new ZipFile()) {
  65.         zip.AddFile(RESOURCE, "");      
  66.         using (MemoryStream ms = new MemoryStream()) {
  67.           using (Document document = new Document(PageSize.A4.Rotate())) {
  68.             // step 2
  69.             PdfWriter.GetInstance(document, ms);
  70.             // step 3
  71.             document.Open();
  72.             // step 4
  73.             fs = new FontSelector();
  74.             for (int i = 0; i < FONTS.Length; i++) {
  75.               fs.AddFont(FontFactory.GetFont(
  76.                 FONTS[i][0], FONTS[i][1], BaseFont.EMBEDDED
  77.               ));
  78.             }
  79.             table = new PdfPTable(3);
  80.             table.DefaultCell.Padding = 3;
  81.             table.DefaultCell.UseAscender = true;
  82.             table.DefaultCell.UseDescender = true;
  83.            
  84.             using (XmlReader xr = XmlReader.Create(RESOURCE)) {
  85.               xr.MoveToContent();
  86.               while (xr.Read()) {
  87.                 switch (xr.NodeType) {
  88.                   case XmlNodeType.Element:
  89.                     StartElement(xr);
  90.                     break;
  91.                   case XmlNodeType.Text:
  92.                     buf.Append(xr.Value.Trim());
  93.                     break;
  94.                   case XmlNodeType.EndElement:
  95.                     EndElement(xr.Name);
  96.                     break;
  97.                 }
  98.               }
  99.             }        
  100.             document.Add(table);
  101.           }
  102.           zip.AddEntry(RESULT, ms.ToArray());
  103.         }
  104.         zip.Save(stream);            
  105.       }
  106.     }
  107. // ---------------------------------------------------------------------------
  108.     public void StartElement(XmlReader xr) {
  109.       string name =  xr.Name;
  110.       if ("pace".Equals(name)) {
  111.         buf = new StringBuilder();
  112.         language = xr.GetAttribute("language");
  113.         countries = xr.GetAttribute("countries");
  114.         rtl = "RTL".Equals(xr.GetAttribute("direction")) ? true : false;
  115.       }
  116.     }
  117. // ---------------------------------------------------------------------------
  118.     public void EndElement(string name) {
  119.       if ("pace".Equals(name)) {
  120.         PdfPCell cell = new PdfPCell();
  121.         cell.AddElement(fs.Process(buf.ToString()));
  122.         cell.Padding = 3;
  123.         cell.UseAscender = true;
  124.         cell.UseDescender = true;
  125.         if (rtl) {
  126.           cell.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
  127.         }
  128.         table.AddCell(language);
  129.         table.AddCell(cell);
  130.         table.AddCell(countries);
  131.       }    
  132.     }    
  133. // ===========================================================================
  134.   }
  135. }
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now