UnicodeWrite

Sample C# code for using Apryse SDK to create Unicode text and embed composite fonts in PDF files. Learn more about our UWP SDK.

1//
2// Copyright (c) 2001-2020 by PDFTron Systems Inc. All Rights Reserved.
3//
4
5using System;
6using System.IO;
7using System.Threading.Tasks;
8using Windows.Foundation;
9
10using pdftron.Filters;
11using pdftron.PDF;
12using pdftron.SDF;
13
14using PDFNetUniversalSamples.ViewModels;
15using Windows.UI.Xaml.Media;
16using Windows.UI.Core;
17using System.Text;
18
19namespace PDFNetSamples
20{
21 // This example illustrates how to create Unicode text and how to embed composite fonts.
22 //
23 // Note: This demo attempts to make use of 'arialuni.ttf' in the '/Samples/TestFiles'
24 // directory. Arial Unicode MS is about 24MB in size and used to come together with Windows and
25 // MS Office.
26 //
27 // In case you don't have access to Arial Unicode MS you can use another wide coverage
28 // font, like Google Noto, GNU UniFont, or cyberbit. Many of these are freely available,
29 // and there is a list maintained at https://en.wikipedia.org/wiki/Unicode_font
30 //
31 // If no specific font file can be loaded, the demo will fall back to system specific font
32 // substitution routines, and the result will depend on which fonts are available.
33 class UnicodeWriteTest : Sample
34 {
35 public UnicodeWriteTest() :
36 base ("UnicodeWrite", "This example illustrates how to create Unicode text and how to embed composite fonts.")
37 {
38 }
39
40 public override IAsyncAction RunAsync()
41 {
42 pdftron.PDFNet.Initialize();
43
44
45
46 return Task.Run(new System.Action(async () =>
47 {
48 try
49 {
50 PDFDoc doc = new PDFDoc();
51
52 ElementBuilder eb = new ElementBuilder(); // ElementBuilder is used to build new Element objects
53 ElementWriter writer = new ElementWriter(); // ElementWriter is used to write Elements to the page
54
55 Page page = doc.PageCreate(new pdftron.PDF.Rect(0, 0, 612, 794)); // Start a new page
56
57 writer.Begin(page); // begin writing to this page
58
59 Font fnt = null;
60 try
61 {
62 fnt = Font.Create(doc, "Arial", "");
63 }
64 catch (Exception e)
65 {
66 WriteLine("Exception:" + e.Message);
67 }
68
69 if (fnt == null)
70 {
71 try
72 {
73 fnt = Font.CreateCIDTrueTypeFont(doc, "ARIALUNI.TTF", true, true);
74 }
75 catch (Exception e)
76 {
77 WriteLine("Exception:" + e.Message);
78 }
79 } else if (fnt == null)
80 {
81 try
82 {
83 fnt = Font.CreateCIDTrueTypeFont(doc, "C:/Windows/Fonts/ARIALUNI.TTF", true, true);
84 }
85 catch (Exception e)
86 {
87 WriteLine("Exception:" + e.Message);
88 }
89 } else if (fnt == null)
90 {
91 WriteLine("Note: using system font substitution for unshaped unicode text");
92 fnt = Font.Create(doc, "Helvetica", "");
93 }
94 else
95 {
96 WriteLine("Note: using Arial Unicode for unshaped unicode text");
97 }
98
99 Element element = eb.CreateTextBegin(fnt, 1);
100 element.SetTextMatrix(10, 0, 0, 10, 50, 600);
101 element.GetGState().SetLeading(2); // Set the spacing between lines
102 writer.WriteElement(element);
103
104 // Hello World!!!
105 string hello = "Hello World!";
106 writer.WriteElement(eb.CreateUnicodeTextRun(hello));
107 writer.WriteElement(eb.CreateTextNewLine());
108
109 // Latin
110 char[] latin = {
111 'a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', '\x45', '\x0046', '\x00C0',
112 '\x00C1', '\x00C2', '\x0143', '\x0144', '\x0145', '\x0152', '1', '2' // etc.
113 };
114 writer.WriteElement(eb.CreateUnicodeTextRun(new string(latin)));
115 writer.WriteElement(eb.CreateTextNewLine());
116
117 // Greek
118 char[] greek = {
119 (char)0x039E, (char)0x039F, (char)0x03A0, (char)0x03A1, (char)0x03A3,
120 (char)0x03A6, (char)0x03A8, (char)0x03A9 // etc.
121 };
122 writer.WriteElement(eb.CreateUnicodeTextRun(new string(greek)));
123 writer.WriteElement(eb.CreateTextNewLine());
124
125 // Cyrillic
126 char[] cyrillic = {
127 (char)0x0409, (char)0x040A, (char)0x040B, (char)0x040C, (char)0x040E, (char)0x040F, (char)0x0410, (char)0x0411,
128 (char)0x0412, (char)0x0413, (char)0x0414, (char)0x0415, (char)0x0416, (char)0x0417, (char)0x0418, (char)0x0419 // etc.
129 };
130 writer.WriteElement(eb.CreateUnicodeTextRun(new string(cyrillic)));
131 writer.WriteElement(eb.CreateTextNewLine());
132
133 // Hebrew
134 char[] hebrew = {
135 (char)0x05D0, (char)0x05D1, (char)0x05D3, (char)0x05D3, (char)0x05D4, (char)0x05D5, (char)0x05D6, (char)0x05D7, (char)0x05D8,
136 (char)0x05D9, (char)0x05DA, (char)0x05DB, (char)0x05DC, (char)0x05DD, (char)0x05DE, (char)0x05DF, (char)0x05E0, (char)0x05E1 // etc.
137 };
138 writer.WriteElement(eb.CreateUnicodeTextRun(new string(hebrew)));
139 writer.WriteElement(eb.CreateTextNewLine());
140
141 // Arabic
142 char[] arabic = {
143 (char)0x0624, (char)0x0625, (char)0x0626, (char)0x0627, (char)0x0628, (char)0x0629, (char)0x062A, (char)0x062B, (char)0x062C,
144 (char)0x062D, (char)0x062E, (char)0x062F, (char)0x0630, (char)0x0631, (char)0x0632, (char)0x0633, (char)0x0634, (char)0x0635 // etc.
145 };
146 writer.WriteElement(eb.CreateUnicodeTextRun(new string(arabic)));
147 writer.WriteElement(eb.CreateTextNewLine());
148
149 // Thai
150 char[] thai = {
151 (char)0x0E01, (char)0x0E02, (char)0x0E03, (char)0x0E04, (char)0x0E05, (char)0x0E06, (char)0x0E07, (char)0x0E08, (char)0x0E09,
152 (char)0x0E0A, (char)0x0E0B, (char)0x0E0C, (char)0x0E0D, (char)0x0E0E, (char)0x0E0F, (char)0x0E10, (char)0x0E11, (char)0x0E12 // etc.
153 };
154 writer.WriteElement(eb.CreateUnicodeTextRun(new string(thai)));
155 writer.WriteElement(eb.CreateTextNewLine());
156
157 // Hiragana - Japanese
158 char[] hiragana = {
159 (char)0x3041, (char)0x3042, (char)0x3043, (char)0x3044, (char)0x3045, (char)0x3046, (char)0x3047, (char)0x3048, (char)0x3049,
160 (char)0x304A, (char)0x304B, (char)0x304C, (char)0x304D, (char)0x304E, (char)0x304F, (char)0x3051, (char)0x3051, (char)0x3052 // etc.
161 };
162 writer.WriteElement(eb.CreateUnicodeTextRun(new string(hiragana)));
163 writer.WriteElement(eb.CreateTextNewLine());
164
165 // CJK Unified Ideographs
166 char[] cjk_uni = {
167 (char)0x5841, (char)0x5842, (char)0x5843, (char)0x5844, (char)0x5845, (char)0x5846, (char)0x5847, (char)0x5848, (char)0x5849,
168 (char)0x584A, (char)0x584B, (char)0x584C, (char)0x584D, (char)0x584E, (char)0x584F, (char)0x5850, (char)0x5851, (char)0x5852 // etc.
169 };
170 writer.WriteElement(eb.CreateUnicodeTextRun(new string(cjk_uni)));
171 writer.WriteElement(eb.CreateTextNewLine());
172
173 // Finish the block of text
174 writer.WriteElement(eb.CreateTextEnd());
175
176 WriteLine("Now using text shaping logic to place text");
177
178 // Create a font in indexed encoding mode
179 // normally this would mean that we are required to provide glyph indices
180 // directly to CreateUnicodeTextRun, but instead, we will use the GetShapedText
181 // method to take care of this detail for us.
182 Font indexedFont = Font.CreateCIDTrueTypeFont(doc, Path.Combine(InputPath, "NotoSans_with_hindi.ttf"), true, true, FontEncoding.e_Indices);
183 element = eb.CreateTextBegin(indexedFont, 10.0);
184 writer.WriteElement(element);
185
186 double linePos = 350.0;
187 double lineSpace = 20.0;
188
189 // Transform unicode text into an abstract collection of glyph indices and positioning info
190 ShapedText shapedText = indexedFont.GetShapedText("Shaped Hindi Text:");
191
192 // transform the shaped text info into a PDF element and write it to the page
193 element = eb.CreateShapedTextRun(shapedText);
194 element.SetTextMatrix(1.5, 0, 0, 1.5, 50, linePos);
195 linePos -= lineSpace;
196 writer.WriteElement(element);
197
198 // read in unicode text lines from a file
199
200 string[] hindiTextLines = File.ReadAllLines(Path.Combine(InputPath, "hindi_sample_utf16le.txt"), new UnicodeEncoding());
201
202 WriteLine("Read in " + hindiTextLines.Length + " lines of Unicode text from file");
203 foreach (string textLine in hindiTextLines)
204 {
205 shapedText = indexedFont.GetShapedText(textLine);
206 element = eb.CreateShapedTextRun(shapedText);
207 element.SetTextMatrix(1.5, 0, 0, 1.5, 50, linePos);
208 linePos -= lineSpace;
209 writer.WriteElement(element);
210 WriteLine("Wrote shaped line to page");
211 }
212
213 // Finish the shaped block of text
214 writer.WriteElement(eb.CreateTextEnd());
215
216 writer.End(); // save changes to the current page
217 doc.PagePushBack(page);
218
219 await doc.SaveAsync(Path.Combine(OutputPath, "unicodewrite.pdf"),
220 SDFDocSaveOptions.e_linearized | SDFDocSaveOptions.e_hex_strings);
221
222 await AddFileToOutputList(Path.Combine(OutputPath, "unicodewrite.pdf"));
223
224 WriteLine("Done. Result saved in unicodewrite.pdf...");
225 }
226 catch (Exception e)
227 {
228 WriteLine(GetExceptionMessage(e));
229 }
230
231 })).AsAsyncAction();
232 }
233 }
234}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales
Create Unicode Text, Embed CID Fonts in PDF in C# | Apryse documentation