Using fonts

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

7th November 2015
admin-marketing

Switch code for this example

FontTest.java
  1. /**
  2.  * Example written by Bruno Lowagie, showing that not all fonts contain
  3.  * all glyphs for all languages.
  4.  */
  5. package sandbox.fonts;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Font;
  10. import com.itextpdf.text.FontFactory;
  11. import com.itextpdf.text.Paragraph;
  12. import com.itextpdf.text.pdf.BaseFont;
  13. import com.itextpdf.text.pdf.PdfWriter;
  14.  
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import java.util.Set;
  19. import java.util.TreeSet;
  20.  
  21. public class FontTest {
  22.  
  23.     public static final String DEST = "results/fonts/overview.pdf";
  24.     public static final String FONTDIR = "resources/fonts";
  25.     public static final String TEXT = "Quick brown fox jumps over the lazy dog; 0123456789";
  26.     public static final String CP1250 = "Nikogar\u0161nja zemlja";
  27.     public static final String CP1251 = "\u042f \u043b\u044e\u0431\u043b\u044e \u0442\u0435\u0431\u044f";
  28.     public static final String CP1252 = "Un long dimanche de fian\u00e7ailles";
  29.     public static final String CP1253 = "\u039d\u03cd\u03c6\u03b5\u03c2";
  30.     public static final String CHINESE = "\u5341\u950a\u57cb\u4f0f";
  31.     public static final String JAPANESE = "\u8ab0\u3082\u77e5\u3089\u306a\u3044";
  32.     public static final String KOREAN = "\ube48\uc9d1";
  33.    
  34.     public static void main(String[] args) throws IOException, DocumentException {
  35.         File file = new File(DEST);
  36.         file.getParentFile().mkdirs();
  37.         new FontTest().createPdf(DEST);
  38.     }
  39.    
  40.     public void createPdf(String dest) throws IOException, DocumentException {
  41.         Document document = new Document();
  42.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  43.         document.open();
  44.         FontFactory.registerDirectory(FONTDIR);
  45.         Set fonts = new TreeSet(FontFactory.getRegisteredFonts());
  46.         for (String fontname : fonts) {
  47.             showFontInfo(document, fontname);
  48.         }
  49.         document.close();
  50.     }
  51.    
  52.     protected void showFontInfo(Document document, String fontname) throws DocumentException {
  53.         System.out.println(fontname);
  54.         Font font = null;
  55.         try {
  56.             font = FontFactory.getFont(fontname, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  57.         }
  58.         catch(Exception e) {
  59.             document.add(new Paragraph(String.format("The font %s doesn't have unicode support: %s", fontname, e.getMessage())));
  60.             return;
  61.         }
  62.         document.add(new Paragraph(
  63.                 String.format("Postscript name for %s: %s", fontname, font.getBaseFont().getPostscriptFontName())));
  64.         document.add(new Paragraph(TEXT, font));
  65.         document.add(new Paragraph(String.format("CP1250: %s", CP1250), font));
  66.         document.add(new Paragraph(String.format("CP1251: %s", CP1251), font));
  67.         document.add(new Paragraph(String.format("CP1252: %s", CP1252), font));
  68.         document.add(new Paragraph(String.format("CP1253: %s", CP1253), font));
  69.         document.add(new Paragraph(String.format("CHINESE: %s", CHINESE), font));
  70.         document.add(new Paragraph(String.format("JAPANESE: %s", JAPANESE), font));
  71.         document.add(new Paragraph(String.format("KOREAN: %s", KOREAN), font));
  72.     }
  73. }
LiberationSans.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/24220668/fontfactory-lowagie-java-getting-java-io-eofexception-when-trying-to-use-gre
  4.  */
  5. package sandbox.fonts;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Font;
  10. import com.itextpdf.text.FontFactory;
  11. import com.itextpdf.text.Paragraph;
  12. import com.itextpdf.text.pdf.PdfWriter;
  13.  
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import sandbox.WrapToTest;
  18.  
  19. @WrapToTest
  20. public class LiberationSans {
  21.  
  22.  
  23.     public static final String DEST = "results/fonts/LiberationSans.pdf";
  24.     public static final String FONT = "resources/fonts/LiberationSans-Regular.ttf";
  25.    
  26.     public static void main(String[] args) throws IOException, DocumentException {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         new LiberationSans().createPdf(DEST);
  30.     }
  31.    
  32.     public void createPdf(String dest) throws IOException, DocumentException {
  33.         Document document = new Document();
  34.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  35.         document.open();
  36.         FontFactory.register(FONT,"Greek-Regular");
  37.         Font f = FontFactory.getFont("Greek-Regular", "Cp1253", true);
  38.         Paragraph p = new Paragraph("\u039d\u03cd\u03c6\u03b5\u03c2", f);
  39.         document.add(p);
  40.         document.close();
  41.     }
  42. }
NotoExample.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/29237980/itext-pdf-not-displaying-chinese-characters-when-using-noto-fonts
  4.  */
  5. package sandbox.fonts;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Font;
  10. import com.itextpdf.text.FontFactory;
  11. import com.itextpdf.text.Paragraph;
  12. import com.itextpdf.text.pdf.BaseFont;
  13. import com.itextpdf.text.pdf.PdfWriter;
  14.  
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import sandbox.WrapToTest;
  19.  
  20. @WrapToTest
  21. public class NotoExample {
  22.        
  23.     public static final String DEST = "results/fonts/chinese.pdf";
  24.     public static final String FONT = "resources/fonts/NotoSansCJKsc-Regular.otf";
  25.     public static final String TEXT = "These are the protagonists in 'Hero', a movie by Zhang Yimou:\n"
  26.         + "\u7121\u540d (Nameless), \u6b98\u528d (Broken Sword), "
  27.         + "\u98db\u96ea (Flying Snow), \u5982\u6708 (Moon), "
  28.         + "\u79e6\u738b (the King), and \u9577\u7a7a (Sky).";
  29.     public static final String CHINESE = "\u5341\u950a\u57cb\u4f0f";
  30.     public static final String JAPANESE = "\u8ab0\u3082\u77e5\u3089\u306a\u3044";
  31.     public static final String KOREAN = "\ube48\uc9d1";
  32.    
  33.     public static void main(String[] args) throws IOException, DocumentException {
  34.         File file = new File(DEST);
  35.         file.getParentFile().mkdirs();
  36.         new NotoExample().createPdf(DEST);
  37.     }
  38.    
  39.     public void createPdf(String dest) throws IOException, DocumentException {
  40.         Document document = new Document();
  41.         PdfWriter.getInstance(document, new FileOutputStream(DEST));
  42.         document.open();
  43.         Font font = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  44.         Paragraph p = new Paragraph(TEXT, font);
  45.         document.add(p);
  46.         document.add(new Paragraph(CHINESE, font));
  47.         document.add(new Paragraph(JAPANESE, font));
  48.         document.add(new Paragraph(KOREAN, font));
  49.         document.close();
  50.     }
  51. }
SmallCapsExample.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://thread.gmane.org/gmane.comp.java.lib.itext.general/65892
  4.  *
  5.  * Some text displayed using a Small Caps font.
  6.  */
  7. package sandbox.fonts;
  8.  
  9. import com.itextpdf.text.Document;
  10. import com.itextpdf.text.DocumentException;
  11. import com.itextpdf.text.Font;
  12. import com.itextpdf.text.Paragraph;
  13. import com.itextpdf.text.pdf.BaseFont;
  14. import com.itextpdf.text.pdf.PdfWriter;
  15.  
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19.  
  20. import sandbox.WrapToTest;
  21.  
  22. @WrapToTest
  23. public class SmallCapsExample {
  24.  
  25.     public static final String DEST = "results/fonts/small_caps.pdf";
  26.     public static final String FONT = "resources/fonts/Delicious-SmallCaps.otf";
  27.    
  28.     public static void main(String[] args) throws IOException, DocumentException {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new SmallCapsExample().createPdf(DEST);
  32.     }
  33.    
  34.     public void createPdf(String dest) throws IOException, DocumentException {
  35.         Document document = new Document();
  36.         PdfWriter.getInstance(document, new FileOutputStream(DEST));
  37.         document.open();
  38.         BaseFont bf = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  39.         Font f = new Font(bf, 12);
  40.         Paragraph p = new Paragraph("This is some text displayed using a Small Caps font.", f);
  41.         document.add(p);
  42.         document.close();
  43.     }
  44. }
TengwarQuenya1.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/27842872/finding-trouble-to-change-installed-font-using-itext-library
  4.  */
  5. package sandbox.fonts;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Font;
  10. import com.itextpdf.text.FontFactory;
  11. import com.itextpdf.text.Paragraph;
  12. import com.itextpdf.text.pdf.BaseFont;
  13. import com.itextpdf.text.pdf.PdfWriter;
  14.  
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import sandbox.WrapToTest;
  19.  
  20. @WrapToTest
  21. public class TengwarQuenya1 {
  22.  
  23.     public static final String DEST = "results/fonts/tengwarquenya1.pdf";
  24.     public static final String FONT = "resources/fonts/QUENCAP1.TTF";
  25.    
  26.     public static void main(String[] args) throws IOException, DocumentException {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         new TengwarQuenya1().createPdf(DEST);
  30.     }
  31.    
  32.     public void createPdf(String dest) throws IOException, DocumentException {
  33.         Document document = new Document();
  34.         PdfWriter.getInstance(document, new FileOutputStream(DEST));
  35.         document.open();
  36.         Font f1 = FontFactory.getFont(FONT, BaseFont.WINANSI, BaseFont.EMBEDDED, 12);
  37.         document.add(new Paragraph("A Hello World PDF document.", f1));
  38.         document.close();
  39.     }
  40. }
FreeSansBold.java
  1. /*
  2.  * This example was written in answer to:
  3.  * http://stackoverflow.com/questions/34396579
  4.  */
  5. package sandbox.fonts;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Font;
  10. import com.itextpdf.text.Paragraph;
  11. import com.itextpdf.text.pdf.BaseFont;
  12. import com.itextpdf.text.pdf.PdfWriter;
  13. import java.io.File;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;import sandbox.WrapToTest;
  16. ;
  17.  
  18. /**
  19.  * @author iText
  20.  */
  21. @WrapToTest
  22. public class FreeSansBold {
  23.        
  24.     public static final String DEST = "results/fonts/free_sans_bold.pdf";
  25.     public static final String FONT = "resources/fonts/FreeSans.ttf";
  26.     public static final String FONTBOLD = "resources/fonts/FreeSansBold.ttf";
  27.    
  28.     public static void main(String[] args) throws IOException, DocumentException {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new FreeSansBold().createPdf(DEST);
  32.     }
  33.    
  34.     public void createPdf(String dest) throws IOException, DocumentException {
  35.         Document document = new Document();
  36.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  37.         document.open();
  38.         BaseFont bf = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  39.         Font font = new Font(bf, 12);
  40.         Paragraph p = new Paragraph("FreeSans regular: \u0110", font);
  41.         document.add(p);
  42.         BaseFont bfbold = BaseFont.createFont(FONTBOLD, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  43.         Font bold = new Font(bfbold, 12);
  44.         p = new Paragraph("FreeSans bold: \u0110", bold);
  45.         document.add(p);
  46.         document.close();
  47.     }
  48.    
  49. }
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