Languages Supported while creating pdf using iText 7.

 The iText layout module will automatically look for pdfCalligraph in its dependencies if text if a language or writing system that requires it is encountered by the Renderer Framework. For example, when iText encounters text that contain Indic texts, or a script that's written from right to left, iText checks if pdfCalligraph is available and will then use its functionality to provide the correct glyph shapes to write to the PDF file. However, as the typography logic is complex and can be resource-heavy even for documents that don’t require this functionality, iText won't attempt any advanced shaping operations if the pdfCalligraph module has not been loaded as a binary dependency.

  • pdfCalligraph features
  • Automatic detection of writing systems
  • Wider language support
  • Right-to-left support
  • Ligatures
  • Kerning
  • Glyph substitution
  • Available for Java and .NET (C#)
For this example, we'll demonstrate using pdfCalligraph to correctly render text in different languages. First, let’s start with a simple English sentence, which we've translated into three different languages using Google Translate.

This is an example sentence.
Let's see how that looks in Arabic:

.هذه هي الجملة المستخدمة في المثال
Now let’s see how it looks in Hindi:

यह एक उदाहरण वाक्य है।
And finally in Tamil:

இது ஒரு எடுத்துக்காட்டு வாக்கியம்.
Those of you familiar with any of the languages in question may notice the translations are not perfect, but they will be sufficient for our purposes here.

Now we’ll save each piece of text as separate XML files, english.xml, arabic.xml, hindi.xml and tamil.xml. Note that because Arabic is written right-to-left, in order for pdfCalligraph to display the Arabic text starting from the right side of the PDF you’ll need to specify this in the XML. However, the languages will be detected and handled by pdfCalligraph automatically.

C# code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
string[] sources = new string[] { "english.xml", "arabic.xml", "hindi.xml", "tamil.xml" };
            PdfWriter writer = new PdfWriter(DEST);
            PdfDocument pdfDocument = new PdfDocument(writer);
            Document document = new Document(pdfDocument);
            FontSet set = new FontSet();
            set.AddFont("NotoNaskhArabic-Regular.ttf");
            set.AddFont("NotoSansTamil-Regular.ttf");
            set.AddFont("FreeSans.ttf");
            document.SetFontProvider(new FontProvider(set));
            document.SetProperty(Property.FONT, new String[] { "MyFontFamilyName" });
            foreach (string source in sources)
            {
                XmlDocument doc = new XmlDocument();
                var stream = new FileStream(source, FileMode.Open);
                doc.Load(stream);
                XmlNode element = doc.GetElementsByTagName("text").Item(0);
                Paragraph paragraph = new Paragraph();
                XmlNode textDirectionElement = element.Attributes.GetNamedItem("direction");
                Boolean rtl = textDirectionElement != null && textDirectionElement.InnerText.Equals("rtl");
                if (rtl)
                {
                    paragraph.SetTextAlignment(TextAlignment.RIGHT);
                }
                paragraph.Add(element.InnerText);
                document.Add(paragraph);
            }
            document.Close();

Java Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
final String[] sources = {"english.xml", "arabic.xml", "hindi.xml", "tamil.xml"};
        final PdfWriter writer = new PdfWriter(DEST);
        final PdfDocument pdfDocument = new PdfDocument(writer);
        final Document document = new Document(pdfDocument);
        final FontSet set = new FontSet();
        set.addFont("fonts/NotoNaskhArabic-Regular.ttf");
        set.addFont("fonts/NotoSansTamil-Regular.ttf");
        set.addFont("fonts/FreeSans.ttf");
        document.setFontProvider(new FontProvider(set));
        document.setProperty(Property.FONT, new String[]{"MyFontFamilyName"});
        for (final String source : sources) {
            final File xmlFile = new File(source);
            final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            final DocumentBuilder builder = factory.newDocumentBuilder();
            final org.w3c.dom.Document doc = builder.parse(xmlFile);
            final Node element = doc.getElementsByTagName("text").item(0);
            final Paragraph paragraph = new Paragraph();
            final Node textDirectionElement = element.getAttributes().getNamedItem("direction");
            boolean rtl = textDirectionElement != null && textDirectionElement.getTextContent()
                    .equalsIgnoreCase("rtl");
            if (rtl) {
                paragraph.setTextAlignment(TextAlignment.RIGHT);
            }
            paragraph.add(element.getTextContent());
            document.add(paragraph);
        }
        document.close();
        pdfDocument.close();
        writer.close();

This will create a PDF containing the following text in the four specified languages, as illustrated below:

An image displaying four different languages in a single PDF
Our example PDF showing the four different languages.


Source:

Post a Comment

0 Comments