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:
0 Comments