Language-specific examples

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

20th November 2015
admin-marketing

Switch code for this example

ArabicExample.java
  1. /**
  2.  * Example written by Bruno Lowagie.
  3.  */
  4. package sandbox.fonts;
  5.  
  6. import com.itextpdf.text.Chunk;
  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.Phrase;
  12. import com.itextpdf.text.pdf.BaseFont;
  13. import com.itextpdf.text.pdf.ColumnText;
  14. import com.itextpdf.text.pdf.PdfWriter;
  15.  
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import sandbox.WrapToTest;
  20.  
  21. @WrapToTest
  22. public class ArabicExample {
  23.        
  24.     public static final String DEST = "results/fonts/arabic_rtl.pdf";
  25.     public static final String FONT = "resources/fonts/NotoNaskhArabic-Regular.ttf";
  26.    
  27.     public static final String ARABIC = "\u0627\u0644\u0633\u0639\u0631 \u0627\u0644\u0627\u062c\u0645\u0627\u0644\u064a";
  28.    
  29.     public static void main(String[] args) throws IOException, DocumentException {
  30.         File file = new File(DEST);
  31.         file.getParentFile().mkdirs();
  32.         new ArabicExample().createPdf(DEST);
  33.     }
  34.    
  35.     public void createPdf(String dest) throws IOException, DocumentException {
  36.         Document document = new Document();
  37.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  38.         document.open();
  39.         Font f = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  40.         Phrase p = new Phrase("This is incorrect: ");
  41.         p.add(new Chunk(ARABIC, f));
  42.         p.add(new Chunk(": 50.00 USD"));
  43.         document.add(p);
  44.  
  45.         p = new Phrase("This is correct: ");
  46.         p.add(new Chunk(ARABIC, f));
  47.         p.add(new Phrase(": 50.00"));
  48.        
  49.         ColumnText canvas = new ColumnText(writer.getDirectContent());
  50.         canvas.setSimpleColumn(36, 750, 559, 780);
  51.         canvas.setRunDirection(PdfWriter.RUN_DIRECTION_LTR);
  52.         canvas.addElement(p);
  53.         canvas.go();
  54.  
  55.         document.close();
  56.     }
  57. }
HindiExample.java
  1. /**
  2.  * Example written by Bruno Lowagie.
  3.  */
  4. package sandbox.fonts;
  5.  
  6. import com.itextpdf.text.BaseColor;
  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.Phrase;
  13. import com.itextpdf.text.pdf.BaseFont;
  14. import com.itextpdf.text.pdf.PdfPCell;
  15. import com.itextpdf.text.pdf.PdfPTable;
  16. import com.itextpdf.text.pdf.PdfWriter;
  17.  
  18. import java.io.File;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21. import sandbox.WrapToTest;
  22.  
  23. @WrapToTest
  24. public class HindiExample {
  25.        
  26.     public static final String DEST = "results/fonts/hindi.pdf";
  27.     public static final String FONT = "resources/fonts/FreeSans.ttf";
  28.    
  29.     public static void main(String[] args) throws IOException, DocumentException {
  30.         File file = new File(DEST);
  31.         file.getParentFile().mkdirs();
  32.         new HindiExample().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.         Font f = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  40.         Paragraph p1 = new Paragraph("\u0915\u093e\u0930 \u092a\u093e\u0930\u094d\u0915\u093f\u0902\u0917", f);
  41.         document.add(p1);
  42.         Paragraph p2 = new Paragraph("\\u0915 \u0915 \\u093e \u093e \\0930 \u0930\n"
  43.                 + "\\u092a \u092a \\u093e \u093e \\u0930 \u0930 \\u094d \u094d"
  44.                 + "\\u0915 \u0915 \\u093f \\u093f \u093f \\u0902 \u0902"
  45.                 + "\\u0917 \u0917", f);
  46.         document.add(p2);
  47.        
  48.         BaseFont unicode = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  49.         Font font=new Font(unicode,12,Font.NORMAL,new BaseColor(50,205,50));
  50.         PdfPTable table = new PdfPTable(new float[] { 10, 60, 30 });
  51.         table.setWidthPercentage(100);
  52.         PdfPCell customerLblCell = new PdfPCell(new Phrase("CUSTOMERS"));
  53.         PdfPCell balanceLblCell = new PdfPCell(new Phrase("\u0915\u093e\u0930\u092a\u093e\u0930\u094d\u0915\u093f\u0902\u0917", font));
  54.         table.addCell(customerLblCell);
  55.         table.addCell(balanceLblCell);
  56.         table.completeRow();
  57.         table.setSpacingBefore(10);
  58.         document.add(table);
  59.         document.close();
  60.     }
  61. }
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