-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathArabicWordSpacing.java
More file actions
85 lines (64 loc) · 3.14 KB
/
Copy pathArabicWordSpacing.java
File metadata and controls
85 lines (64 loc) · 3.14 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.itextpdf.samples.sandbox.typography.arabic;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.licensing.base.LicenseKey;
import com.itextpdf.samples.util.LicenseUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
/*
* ArabicWordSpacing.java
*
* Demonstrates rendering Arabic text with different word spacing values in
* a PDF. Creates paragraphs with default spacing, 10-point spacing, and
* 20-point spacing.
*/
public class ArabicWordSpacing {
public static final String DEST = "./target/sandbox/typography/ArabicWordSpacing.pdf";
public static final String FONTS_FOLDER = "./src/main/resources/font/";
public static void main(String[] args) throws Exception {
// Load the license file to use typography features
String licensePath = LicenseUtil.getPathToLicenseFileWithITextCoreAndPdfCalligraphProducts();
try (FileInputStream license = new FileInputStream(licensePath)) {
LicenseKey.loadLicenseFile(license);
}
File file = new File(DEST);
file.getParentFile().mkdirs();
new ArabicWordSpacing().createPDF(DEST);
}
public void createPDF(String dest) throws IOException {
// Create a pdf document along with a Document (default root layout element) instance
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(dest));
Document document = new Document(pdfDocument);
// في القيام بنشاط
String text = "\u0641\u064A\u0020\u0627\u0644\u0642\u064A\u0627\u0645\u0020\u0628\u0646\u0634\u0627\u0637";
PdfFont font = PdfFontFactory.createFont(FONTS_FOLDER + "NotoNaskhArabic-Regular.ttf",
PdfEncodings.IDENTITY_H);
// Overwrite some default document font-related properties. From now on they will be used for all the elements
// added to the document unless they are overwritten inside these elements
document
.setFont(font)
.setFontSize(10);
// Create paragraph, add string to it with the default word spacing and add all to the document
document.add(createParagraph(text));
// Add text with a word spacing of 10 points
document.add(createParagraph(text).setWordSpacing(10));
// Set word spacing on the document. It will be applied to the next paragraph
document.setWordSpacing(20);
document.add(createParagraph(text));
document.close();
}
// This method creates a paragraph with right text alignment
private static Paragraph createParagraph(String text) {
Paragraph paragraph = new Paragraph(text);
// In Arabic text goes from right to left, that's why we need to overwrite the default iText's alignment
paragraph.setTextAlignment(TextAlignment.RIGHT);
return paragraph;
}
}