Fonts

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

1st September 2016
iText PDF

Switch code for this example

HelloWorldInternational.java
  1. /**
  2.  * Example written for JavaOne 2016.
  3.  * Differences between iText 5 and iText 7 are discussed in the JavaOne talk
  4.  * "Oops, I broke my API" by Raf Hens and Bruno Lowagie.
  5.  * This is the iText 5 version of one of the examples.
  6.  */
  7. package javaone.edition16;
  8.  
  9. import com.itextpdf.text.Chunk;
  10. import java.io.File;
  11. import java.io.FileOutputStream;
  12. import java.io.IOException;
  13.  
  14. import com.itextpdf.text.Document;
  15. import com.itextpdf.text.DocumentException;
  16. import com.itextpdf.text.Font;
  17. import com.itextpdf.text.Font.FontFamily;
  18. import com.itextpdf.text.Paragraph;
  19. import com.itextpdf.text.pdf.BaseFont;
  20. import com.itextpdf.text.pdf.PdfWriter;
  21. import sandbox.WrapToTest;
  22.  
  23. @WrapToTest
  24. public class HelloWorldInternational {
  25.  
  26.     public static final String DEST
  27.         = "results/javaone/edition16/hello_international.pdf";
  28.  
  29.     public static void main(String[] args)
  30.         throws DocumentException, IOException {
  31.         File file = new File(DEST);
  32.         file.getParentFile().mkdirs();
  33.         new HelloWorldInternational().createPdf(DEST);
  34.     }
  35.  
  36.     public void createPdf(String dest)
  37.     throws DocumentException, IOException {
  38.         Document document = new Document();
  39.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  40.         document.open();
  41.         Font font = new Font(FontFamily.TIMES_ROMAN);
  42.         Font font14pt = new Font(FontFamily.TIMES_ROMAN, 14);
  43.         Font font10pt = new Font(FontFamily.TIMES_ROMAN, 10);
  44.         BaseFont bf_russian = BaseFont.createFont("resources/fonts/FreeSans.ttf", "CP1251", BaseFont.EMBEDDED);
  45.         Font russian = new Font(bf_russian, 12);
  46.         BaseFont bf_cjk = BaseFont.createFont("resources/fonts/NotoSansCJKsc-Regular.otf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  47.         Font cjk = new Font(bf_cjk, 12);
  48.         Paragraph p = new Paragraph("Hello World! ", font);
  49.         Chunk chunk = new Chunk("Hallo Wereld! ", font14pt);
  50.         p.add(chunk);
  51.         chunk = new Chunk("Bonjour le monde! ", font10pt);
  52.         chunk.setTextRise(4);
  53.         p.add(chunk);
  54.         chunk = new Chunk(
  55.             "\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u043b\u0442\u0435 \u043c\u0438\u0440! ", russian);
  56.         p.add(chunk);
  57.         p.add(new Chunk("\u4f60\u597d\u4e16\u754c! ", cjk));
  58.         p.add(new Chunk("\uc5ec\ubcf4\uc138\uc694 \uc138\uacc4!", cjk));
  59.         document.add(p);
  60.         document.close();
  61.     }
  62. }
HelloWorldInternational.java
  1. /**
  2.  * Example written for JavaOne 2016.
  3.  * Differences between iText 5 and iText 7 are discussed in the JavaOne talk
  4.  * "Oops, I broke my API" by Raf Hens and Bruno Lowagie.
  5.  * This is the iText 7 version of one of the examples.
  6.  */
  7. package com.itextpdf.sandbox.javaone16;
  8.  
  9. import com.itextpdf.io.font.FontConstants;
  10. import com.itextpdf.io.font.PdfEncodings;
  11. import com.itextpdf.kernel.font.PdfFont;
  12. import com.itextpdf.kernel.font.PdfFontFactory;
  13. import com.itextpdf.kernel.pdf.PdfDocument;
  14. import com.itextpdf.kernel.pdf.PdfWriter;
  15. import com.itextpdf.layout.Document;
  16. import com.itextpdf.layout.element.Paragraph;
  17. import com.itextpdf.layout.element.Text;
  18. import com.itextpdf.test.annotations.WrapToTest;
  19. import java.io.File;
  20. import java.io.IOException;
  21.  
  22. @WrapToTest
  23. public class HelloWorldInternational {
  24.    
  25.     public static final String DEST
  26.         = "results/javaone16/hello_international.pdf";
  27.  
  28.     public static void main(String[] args) throws IOException {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new HelloWorldInternational().createPdf(DEST);
  32.     }
  33.  
  34.     public void createPdf(String dest) throws IOException {
  35.         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
  36.         try (Document document = new Document(pdf)) {
  37.             PdfFont font = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);
  38.             PdfFont russian = PdfFontFactory.createFont(
  39.                 "src/main/resources/fonts/FreeSans.ttf", "CP1251", true);
  40.             PdfFont cjk = PdfFontFactory.createFont(
  41.                 "src/main/resources/fonts/NotoSansCJKsc-Regular.otf", PdfEncodings.IDENTITY_H, true);
  42.             document.setFont(font);
  43.             Paragraph p = new Paragraph("Hello World! ")
  44.                 .add(new Text("Hallo Wereld! ").setFontSize(14))
  45.                 .add(new Text("Bonjour le monde! ").setFontSize(10).setTextRise(4))
  46.                 .add(
  47.                     new Text("\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u043b\u0442\u0435 \u043c\u0438\u0440! ")
  48.                         .setFont(russian))
  49.                 .add(new Text("\u4f60\u597d\u4e16\u754c! ").setFont(cjk))
  50.                 .add(new Text("\uc5ec\ubcf4\uc138\uc694 \uc138\uacc4!").setFont(cjk));
  51.             document.add(p);
  52.         }
  53.     }
  54. }
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