Showing special characters

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

31st May 2016
iText PDF

Switch code for this example

MathSymbols.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * This example was written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/31268867/
  11.  */
  12. package com.itextpdf.samples.sandbox.fonts;
  13.  
  14. import com.itextpdf.io.font.PdfEncodings;
  15. import com.itextpdf.kernel.font.PdfFont;
  16. import com.itextpdf.kernel.font.PdfFontFactory;
  17. import com.itextpdf.kernel.pdf.PdfDocument;
  18. import com.itextpdf.kernel.pdf.PdfWriter;
  19. import com.itextpdf.layout.Document;
  20. import com.itextpdf.layout.element.Paragraph;
  21. import com.itextpdf.samples.GenericTest;
  22. import com.itextpdf.test.annotations.type.SampleTest;
  23.  
  24. import org.junit.experimental.categories.Category;
  25.  
  26. import java.io.File;
  27.  
  28. @Category(SampleTest.class)
  29. public class MathSymbols extends GenericTest {
  30.     public static final String DEST = "./target/test/resources/sandbox/fonts/math_symbols.pdf";
  31.     public static final String FONT = "./src/test/resources/font/FreeSans.ttf";
  32.  
  33.     public static void main(String[] args) throws Exception {
  34.         File file = new File(DEST);
  35.         file.getParentFile().mkdirs();
  36.         new MathSymbols().manipulatePdf(DEST);
  37.     }
  38.  
  39.     @Override
  40.     protected void manipulatePdf(String dest) throws Exception {
  41.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
  42.         Document doc = new Document(pdfDoc);
  43.  
  44.         PdfFont f1 = PdfFontFactory.createFont(FONT, PdfEncodings.CP1250, true);
  45.         Paragraph p1 = new Paragraph("Testing of letters \u010c,\u0106,\u0160,\u017d,\u0110").setFont(f1);
  46.         doc.add(p1);
  47.  
  48.         PdfFont f2 = PdfFontFactory.createFont(FONT, PdfEncodings.IDENTITY_H);
  49.         Paragraph p2 = new Paragraph("Testing of letters \u010c,\u0106,\u0160,\u017d,\u0110").setFont(f2);
  50.         doc.add(p2);
  51.  
  52.         doc.close();
  53.     }
  54. }
RupeeSymbol.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/26360814/rupee-symbol-is-not-showing-in-android
  11.  */
  12. package com.itextpdf.samples.sandbox.fonts;
  13.  
  14. import com.itextpdf.io.font.PdfEncodings;
  15. import com.itextpdf.kernel.font.PdfFont;
  16. import com.itextpdf.kernel.font.PdfFontFactory;
  17. import com.itextpdf.kernel.pdf.PdfDocument;
  18. import com.itextpdf.kernel.pdf.PdfWriter;
  19. import com.itextpdf.layout.Document;
  20. import com.itextpdf.layout.element.Paragraph;
  21. import com.itextpdf.samples.GenericTest;
  22. import com.itextpdf.test.annotations.type.SampleTest;
  23. import org.junit.experimental.categories.Category;
  24.  
  25. import java.io.File;
  26.  
  27. @Category(SampleTest.class)
  28. public class RupeeSymbol extends GenericTest {
  29.     public static final String DEST = "./target/test/resources/sandbox/fonts/rupee_symbol.pdf";
  30.  
  31.     public static final String FONT1 = "./src/test/resources/font/PlayfairDisplay-Regular.ttf";
  32.     public static final String FONT2 = "./src/test/resources/font/PT_Sans-Web-Regular.ttf";
  33.     public static final String FONT3 = "./src/test/resources/font/FreeSans.ttf";
  34.     public static final String RUPEE = "The Rupee character \u20B9 and the Rupee symbol \u20A8";
  35.  
  36.     public static void main(String[] args) throws Exception {
  37.         File file = new File(DEST);
  38.         file.getParentFile().mkdirs();
  39.         new RupeeSymbol().manipulatePdf(DEST);
  40.     }
  41.  
  42.     @Override
  43.     protected void manipulatePdf(String dest) throws Exception {
  44.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
  45.         Document doc = new Document(pdfDoc);
  46.         PdfFont font1 = PdfFontFactory.createFont(FONT1, PdfEncodings.IDENTITY_H);
  47.         PdfFont font2 = PdfFontFactory.createFont(FONT2, PdfEncodings.IDENTITY_H);
  48.         PdfFont font3 = PdfFontFactory.createFont(FONT3, PdfEncodings.IDENTITY_H);
  49.         PdfFont font4 = PdfFontFactory.createFont(FONT3, PdfEncodings.WINANSI, true);
  50.  
  51.         doc.add(new Paragraph(RUPEE).setFont(font1));
  52.         doc.add(new Paragraph(RUPEE).setFont(font2));
  53.         doc.add(new Paragraph(RUPEE).setFont(font3));
  54.         doc.add(new Paragraph(RUPEE).setFont(font4));
  55.         doc.close();
  56.     }
  57. }
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