Showing special characters

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

1st November 2015
admin-marketing

Switch code for this example

RupeeSymbol.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/26360814/rupee-symbol-is-not-showing-in-android
  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. /**
  21.  *
  22.  * @author iText
  23.  */
  24. @WrapToTest
  25. public class RupeeSymbol {
  26.  
  27.     public static final String DEST = "results/fonts/rupee.pdf";
  28.     public static final String FONT1 = "resources/fonts/PlayfairDisplay-Regular.ttf";
  29.     public static final String FONT2 = "resources/fonts/PT_Sans-Web-Regular.ttf";
  30.     public static final String FONT3 = "resources/fonts/FreeSans.ttf";
  31.     public static final String RUPEE = "The Rupee character \u20B9 and the Rupee symbol \u20A8";
  32.    
  33.     public static void main(String[] args) throws IOException, DocumentException {
  34.         File file = new File(DEST);
  35.         file.getParentFile().mkdirs();
  36.         new RupeeSymbol().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 f1 = FontFactory.getFont(FONT1, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 12);
  44.         Font f2 = FontFactory.getFont(FONT2, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 12);
  45.         Font f3 = FontFactory.getFont(FONT3, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 12);
  46.         Font f4 = FontFactory.getFont(FONT3, BaseFont.WINANSI, BaseFont.EMBEDDED, 12);
  47.         document.add(new Paragraph(RUPEE, f1));
  48.         document.add(new Paragraph(RUPEE, f2));
  49.         document.add(new Paragraph(RUPEE, f3));
  50.         document.add(new Paragraph(RUPEE, f4));
  51.         document.close();
  52.     }
  53. }
MathSymbols.java
  1. /*
  2.  * This example was written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/31268867/
  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.  
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import sandbox.WrapToTest;
  18.  
  19. /**
  20.  *
  21.  * @author Bruno Lowagie (iText Software)
  22.  */
  23. @WrapToTest
  24. public class MathSymbols {
  25.     public static final String DEST = "results/fonts/math_symbols.pdf";
  26.     public static final String FONT = "resources/fonts/FreeSans.ttf";
  27.     public static final String TEXT = "this string contains special characters like this  \u2208, \u2229, \u2211, \u222b, \u2206";
  28.    
  29.     public static void main(String[] args) throws IOException, DocumentException {
  30.         File file = new File(DEST);
  31.         file.getParentFile().mkdirs();
  32.         new MathSymbols().createPdf(DEST);
  33.     }
  34.    
  35.     public void createPdf(String dest) throws IOException, DocumentException {
  36.         Document document = new Document();
  37.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  38.         document.open();
  39.         BaseFont bf = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  40.         Font f = new Font(bf, 12);
  41.         Paragraph p = new Paragraph(TEXT, f);
  42.         document.add(p);
  43.         document.close();
  44.     }
  45. }
SunCharacter.java
  1. /*
  2.  * This example was written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/31268867/
  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.  
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import sandbox.WrapToTest;
  18.  
  19. /**
  20.  *
  21.  * @author Bruno Lowagie (iText Software)
  22.  */
  23. @WrapToTest
  24. public class SunCharacter {
  25.     public static final String DEST = "results/fonts/sun_character.pdf";
  26.     public static final String FONT = "resources/fonts/Cardo-Regular.ttf";
  27.     public static final String TEXT = "The Cardo family of fonts supports this character: \u2609";
  28.    
  29.     public static void main(String[] args) throws IOException, DocumentException {
  30.         File file = new File(DEST);
  31.         file.getParentFile().mkdirs();
  32.         new SunCharacter().createPdf(DEST);
  33.     }
  34.    
  35.     public void createPdf(String dest) throws IOException, DocumentException {
  36.         Document document = new Document();
  37.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  38.         document.open();
  39.         BaseFont bf = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  40.         Font f = new Font(bf, 12);
  41.         Paragraph p = new Paragraph(TEXT, f);
  42.         document.add(p);
  43.         document.close();
  44.     }
  45. }
TickboxCharacter.java
  1. /*
  2.  * This example was written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/37991675
  4.  */
  5. package sandbox.fonts;
  6.  
  7. import com.itextpdf.text.Chunk;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Font;
  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. /**
  20.  *
  21.  * @author Bruno Lowagie (iText Software)
  22.  */
  23. @WrapToTest
  24. public class TickboxCharacter {
  25.     public static final String DEST = "results/fonts/tickbox_character.pdf";
  26.    
  27.     public static void main(String[] args) throws IOException, DocumentException {
  28.         File file = new File(DEST);
  29.         file.getParentFile().mkdirs();
  30.         new TickboxCharacter().createPdf(DEST);
  31.     }
  32.    
  33.     public void createPdf(String dest) throws IOException, DocumentException {
  34.         Document document = new Document();
  35.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  36.         document.open();
  37.         Paragraph p = new Paragraph("This is a tick box character: ");
  38.         Font zapfdingbats = new Font(Font.FontFamily.ZAPFDINGBATS, 14);
  39.         Chunk chunk = new Chunk("o", zapfdingbats);
  40.         p.add(chunk);
  41.         document.add(p);
  42.         document.close();
  43.     }
  44. }
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