Language-specific examples

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

31st May 2016
iText PDF

Switch code for this example

ArabicExample.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:
  10.  * http://stackoverflow.com/questions/33726908/writing-arabic-in-pdf-using-itext
  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.layout.element.Text;
  22. import com.itextpdf.layout.property.BaseDirection;
  23. import com.itextpdf.licensekey.LicenseKey;
  24. import com.itextpdf.samples.GenericTest;
  25. import com.itextpdf.test.annotations.type.SampleTest;
  26.  
  27. import org.junit.experimental.categories.Category;
  28.  
  29. import java.io.File;
  30.  
  31. @Category(SampleTest.class)
  32. public class ArabicExample extends GenericTest {
  33.     public static final String ARABIC
  34.             = "\u0627\u0644\u0633\u0639\u0631 \u0627\u0644\u0627\u062c\u0645\u0627\u0644\u064a";
  35.     public static final String DEST
  36.             = "./target/test/resources/sandbox/fonts/arabic_example.pdf";
  37.     public static final String FONT
  38.             = "./src/test/resources/font/NotoNaskhArabic-Regular.ttf";
  39.  
  40.     public static void main(String[] args) throws Exception {
  41.         File file = new File(DEST);
  42.         file.getParentFile().mkdirs();
  43.         new ArabicExample().manipulatePdf(DEST);
  44.     }
  45.  
  46.     @Override
  47.     protected void manipulatePdf(String dest) throws Exception {
  48.         //Load the license file to use advanced typography features
  49.         LicenseKey.loadLicenseFile(System.getenv("ITEXT7_LICENSEKEY") + "/itextkey-typography.xml");
  50.  
  51.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  52.         Document doc = new Document(pdfDoc);
  53.  
  54.         PdfFont f = PdfFontFactory.createFont(FONT, PdfEncodings.IDENTITY_H);
  55.  
  56.         Paragraph p = new Paragraph("This is auto detection: ");
  57.         p.add(new Text(ARABIC).setFont(f));
  58.         p.add(new Text(": 50.00 USD"));
  59.         doc.add(p);
  60.  
  61.         p = new Paragraph("This is correct manual property: ").setBaseDirection(BaseDirection.LEFT_TO_RIGHT).setFontScript(Character.UnicodeScript.ARABIC);
  62.         p.add(new Text(ARABIC).setFont(f));
  63.         p.add(new Text(": 50.00"));
  64.         doc.add(p);
  65.  
  66.         doc.close();
  67.     }
  68. }
HindiExample.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:
  10.  * http://stackoverflow.com/questions/33733432/how-do-make-itext-to-accept-unicode-having-sequence-like-below/33733782
  11.  */
  12. package com.itextpdf.samples.sandbox.fonts;
  13.  
  14. import com.itextpdf.io.font.PdfEncodings;
  15. import com.itextpdf.kernel.color.DeviceRgb;
  16. import com.itextpdf.kernel.font.PdfFont;
  17. import com.itextpdf.kernel.font.PdfFontFactory;
  18. import com.itextpdf.kernel.pdf.PdfDocument;
  19. import com.itextpdf.kernel.pdf.PdfWriter;
  20. import com.itextpdf.layout.Document;
  21. import com.itextpdf.layout.element.Cell;
  22. import com.itextpdf.layout.element.Paragraph;
  23. import com.itextpdf.layout.element.Table;
  24. import com.itextpdf.licensekey.LicenseKey;
  25. import com.itextpdf.samples.GenericTest;
  26. import com.itextpdf.test.annotations.type.SampleTest;
  27.  
  28. import org.junit.experimental.categories.Category;
  29.  
  30. import java.io.File;
  31.  
  32. @Category(SampleTest.class)
  33. public class HindiExample extends GenericTest {
  34.     public static final String DEST
  35.             = "./target/test/resources/sandbox/fonts/hindi_example.pdf";
  36.     public static final String FONT
  37.             = "./src/test/resources/font/FreeSans.ttf";
  38.  
  39.     public static void main(String[] args) throws Exception {
  40.         File file = new File(DEST);
  41.         file.getParentFile().mkdirs();
  42.         new HindiExample().manipulatePdf(DEST);
  43.     }
  44.  
  45.     @Override
  46.     protected void manipulatePdf(String dest) throws Exception {
  47.         LicenseKey.loadLicenseFile(System.getenv("ITEXT7_LICENSEKEY") + "/itextkey-typography.xml");
  48.  
  49.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  50.         Document doc = new Document(pdfDoc);
  51.  
  52.         PdfFont f = PdfFontFactory.createFont(FONT, PdfEncodings.IDENTITY_H);
  53.  
  54.         Paragraph p1 = new Paragraph("\u0915\u093e\u0930 \u092a\u093e\u0930\u094d\u0915\u093f\u0902\u0917").setFont(f);
  55.         doc.add(p1);
  56.  
  57.         Paragraph p2 = new Paragraph("\\u0915 \u0915 \\u093e \u093e \\0930 \u0930\n"
  58.                 + "\\u092a \u092a \\u093e \u093e \\u0930 \u0930 \\u094d \u094d"
  59.                 + "\\u0915 \u0915 \\u093f \\u093f \u093f \\u0902 \u0902"
  60.                 + "\\u0917 \u0917").setFont(f);
  61.         doc.add(p2);
  62.  
  63.         Table table = new Table(new float[]{10, 60, 30});
  64.         table.setWidthPercent(100);
  65.         Cell customerLblCell = new Cell().add("CUSTOMERS");
  66.         Cell balanceLblCell = new Cell().add(new Paragraph("\u0915\u093e\u0930\u092a\u093e\u0930\u094d\u0915\u093f\u0902\u0917")
  67.                 .setFont(f)
  68.                 .setFontColor(new DeviceRgb(50, 205, 50)));
  69.         table.addCell(customerLblCell);
  70.         table.addCell(balanceLblCell);
  71.         table.setMarginTop(10);
  72.         doc.add(table);
  73.  
  74.         doc.close();
  75.     }
  76. }
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