/
ArabicWordSpacing.cs
75 lines (61 loc) · 2.79 KB
/
ArabicWordSpacing.cs
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
using System;
using System.IO;
using iText.Commons.Utils;
using iText.IO.Font;
using iText.Kernel.Font;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using iText.Licensing.Base;
namespace iText.Samples.Sandbox.Typography.Arabic
{
public class ArabicWordSpacing
{
public const String DEST = "results/sandbox/typography/ArabicWordSpacing.pdf";
public const String FONTS_FOLDER = "../../../resources/font/";
public static void Main(String[] args)
{
// Load the license file to use typography features
using (Stream license = FileUtil.GetInputStreamForFile(
Environment.GetEnvironmentVariable("ITEXT7_LICENSEKEY") + "/itextkey-typography.json"))
{
LicenseKey.LoadLicenseFile(license);
}
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new ArabicWordSpacing().CreatePDF(DEST);
}
public virtual void CreatePDF(String dest)
{
// 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;
}
}
}