DocumentCreation

Sample C# code use the Document Layout Engine to easily create reflowable PDF documents programmatically using Apryse SDK.

1//
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3//
4
5using System;
6using pdftron;
7using pdftron.Common;
8using pdftron.Filters;
9using pdftron.SDF;
10using pdftron.PDF;
11using pdftron.Layout;
12
13namespace DocumentCreationTestCS
14{
15 /// <summary>
16 /// The sample code shows how to create document with the document creation API.
17 /// </summary>
18 class Class1
19 {
20 private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
21 static Class1() {}
22
23 // Iterate over all text runs of the document and make every second run
24 // bold with smaller font size.
25 static void ModifyContentTree(ContentNode node)
26 {
27 bool bold = false;
28
29 for (ContentNodeIterator itr = node.GetContentNodeIterator();
30 itr.HasNext();
31 itr.Next())
32 {
33 ContentElement el = itr.Current();
34
35 ContentNode content_node = el.AsContentNode();
36 if (content_node != null)
37 {
38 ModifyContentTree(content_node);
39 continue;
40 }
41
42 TextRun text_run = el.AsTextRun();
43 if (text_run != null)
44 {
45 if (bold)
46 {
47 text_run.GetTextStyledElement().SetBold(true);
48 text_run.GetTextStyledElement().SetFontSize(text_run.GetTextStyledElement().GetFontSize() * 0.8);
49 }
50 bold = !bold;
51 continue;
52 }
53 }
54 }
55
56 /// <summary>
57 /// The main entry point for the application.
58 /// </summary>
59 [STAThread]
60 static void Main(string[] args)
61 {
62 PDFNet.Initialize(PDFTronLicense.Key);
63
64 // Relative path to the folder containing test files.
65 string output_path = "../../../../TestFiles/Output/";
66
67 string para_text =
68 "Lorem ipsum dolor "
69 + "sit amet, consectetur adipisicing elit, sed "
70 + "do eiusmod tempor incididunt ut labore "
71 + "et dolore magna aliqua. Ut enim ad "
72 + "minim veniam, quis nostrud exercitation "
73 + "ullamco laboris nisi ut aliquip ex ea "
74 + "commodo consequat. Duis aute irure "
75 + "dolor in reprehenderit in voluptate velit "
76 + "esse cillum dolore eu fugiat nulla pariatur. "
77 + "Excepteur sint occaecat cupidatat "
78 + "non proident, sunt in culpa qui officia "
79 + "deserunt mollit anim id est laborum.";
80
81 try
82 {
83 using (FlowDocument flowdoc = new FlowDocument())
84 {
85 Paragraph para = flowdoc.AddParagraph();
86 TextStyledElement st_para = para.GetTextStyledElement();
87
88 st_para.SetFontSize(24);
89 st_para.SetTextColor(255, 0, 0);
90 para.AddText("Start \tRed \tText\n");
91
92 para.AddTabStop(150);
93 para.AddTabStop(250);
94 st_para.SetTextColor(0, 0, 255);
95 para.AddText("Start \tBlue \tText\n");
96
97 TextRun last_run = para.AddText("Start Green Text\n");
98
99 ContentNodeIterator itr = para.GetContentNodeIterator();
100 for (int i = 0; itr.HasNext(); itr.Next(), i++)
101 {
102 ContentElement el = itr.Current();
103
104 TextRun text_run = el.AsTextRun();
105 if (text_run != null)
106 {
107 text_run.GetTextStyledElement().SetFontSize(12);
108
109 if (i == 0)
110 {
111 // Restore red color.
112 text_run.SetText(text_run.GetText() + "(restored \tred \tcolor)\n");
113 text_run.GetTextStyledElement().SetTextColor(255, 0, 0);
114 }
115 }
116 }
117
118 TextStyledElement st_last = last_run.GetTextStyledElement();
119
120 st_last.SetTextColor(0, 255, 0);
121 st_last.SetItalic(true);
122 st_last.SetFontSize(18);
123
124 para.GetTextStyledElement().SetBold(true);
125 para.SetBorder(0.2, 0, 127, 0);
126 st_last.SetBold(false);
127
128 {
129 flowdoc.AddParagraph("Simple list creation process. All elements are added in their natural order\n\n");
130
131 List list = flowdoc.AddList();
132 list.SetNumberFormat(List.NumberFormat.e_upper_letter);
133 list.SetStartIndex(4);
134
135 ListItem item = list.AddItem(); // creates "D."
136 item.AddParagraph("item 0[0]");
137 Paragraph px = item.AddParagraph("item 0[1]");
138 TextStyledElement xx_para = px.GetTextStyledElement();
139 xx_para.SetTextColor(255, 99, 71);
140 px.AddText(" Some More Text!");
141
142
143 ListItem item2 = list.AddItem(); // creates "E."
144 List item2List = item2.AddList();
145 item2List.SetStartIndex(0);
146 item2List.SetNumberFormat(List.NumberFormat.e_decimal, "", true);
147 item2List.AddItem().AddParagraph("item 1[0].0");
148 Paragraph pp = item2List.AddItem().AddParagraph("item 1[0].1");
149 TextStyledElement sx_para = pp.GetTextStyledElement();
150 sx_para.SetTextColor(0, 0, 255);
151 pp.AddText(" Some More Text");
152 item2List.AddItem().AddParagraph("item 1[0].2");
153 List item2List1 = item2List.AddItem().AddList();
154 item2List1.SetStartIndex(7);
155 item2List1.SetNumberFormat(List.NumberFormat.e_upper_roman, ")", true);
156 item2List1.AddItem().AddParagraph("item 1[0].3.0");
157 item2List1.AddItem().AddParagraph("item 1[0].3.1");
158
159 ListItem extraItem = item2List1.AddItem();
160 extraItem.AddParagraph("item 1[0].3.2[0]");
161 extraItem.AddParagraph("item 1[0].3.2[1]");
162 List fourth = extraItem.AddList();
163 fourth.SetNumberFormat(List.NumberFormat.e_decimal, "", true);
164 fourth.AddItem().AddParagraph("Fourth Level");
165
166 fourth = item2List1.AddItem().AddList();
167 fourth.SetNumberFormat(List.NumberFormat.e_lower_letter, "", true);
168 fourth.AddItem().AddParagraph("Fourth Level (again)");
169
170
171 item2.AddParagraph("item 1[1]");
172 List item2List2 = item2.AddList();
173 item2List2.SetStartIndex(10);
174 item2List2.SetNumberFormat(List.NumberFormat.e_lower_roman); // , UString(""), true);
175 item2List2.AddItem().AddParagraph("item 1[2].0");
176 item2List2.AddItem().AddParagraph("item 1[2].1");
177 item2List2.AddItem().AddParagraph("item 1[2].2");
178
179 ListItem item3 = list.AddItem(); // creates "F."
180 item3.AddParagraph("item 2");
181
182 ListItem item4 = list.AddItem(); // creates "G."
183 item4.AddParagraph("item 3");
184
185 ListItem item5 = list.AddItem(); // creates "H."
186 item5.AddParagraph("item 4");
187 }
188
189 for (itr = flowdoc.GetBody().GetContentNodeIterator();
190 itr.HasNext();
191 itr.Next())
192 {
193 ContentElement el = itr.Current();
194
195 List list = el.AsList();
196 if (list != null)
197 {
198 if (list.GetIndentationLevel() == 1)
199 {
200 Paragraph p = list.AddItem().AddParagraph("Item added during iteration");
201 TextStyledElement ps = p.GetTextStyledElement();
202 ps.SetTextColor(0, 127, 0);
203 }
204 }
205
206 ListItem list_item = el.AsListItem();
207 if (list_item != null)
208 {
209 if (list_item.GetIndentationLevel() == 2)
210 {
211 Paragraph p = list_item.AddParagraph("* Paragraph added during iteration");
212 TextStyledElement ps = p.GetTextStyledElement();
213 ps.SetTextColor(0, 0, 255);
214 }
215 }
216 }
217
218 flowdoc.AddParagraph("\f"); // page break
219
220 {
221 flowdoc.AddParagraph("Nonlinear list creation flow. Items are added randomly."
222 + " List body separated by a paragraph, does not belong to the list\n\n");
223
224 List list = flowdoc.AddList();
225 list.SetNumberFormat(List.NumberFormat.e_upper_letter);
226 list.SetStartIndex(4);
227
228 ListItem item = list.AddItem(); // creates "D."
229 item.AddParagraph("item 0[0]");
230 Paragraph px = item.AddParagraph("item 0[1]");
231 TextStyledElement xx_para = px.GetTextStyledElement();
232 xx_para.SetTextColor(255, 99, 71);
233 px.AddText(" Some More Text!");
234 item.AddParagraph("item 0[2]");
235 px = item.AddParagraph("item 0[3]");
236 item.AddParagraph("item 0[4]");
237 xx_para = px.GetTextStyledElement();
238 xx_para.SetTextColor(255, 99, 71);
239
240
241 ListItem item2 = list.AddItem(); // creates "E."
242 List item2List = item2.AddList();
243 item2List.SetStartIndex(0);
244 item2List.SetNumberFormat(List.NumberFormat.e_decimal, "", true);
245 item2List.AddItem().AddParagraph("item 1[0].0");
246 Paragraph pp = item2List.AddItem().AddParagraph("item 1[0].1");
247 TextStyledElement sx_para = pp.GetTextStyledElement();
248 sx_para.SetTextColor(0, 0, 255);
249 pp.AddText(" Some More Text");
250
251
252 ListItem item3 = list.AddItem(); // creates "F."
253 item3.AddParagraph("item 2");
254
255 item2List.AddItem().AddParagraph("item 1[0].2");
256
257 item2.AddParagraph("item 1[1]");
258 List item2List2 = item2.AddList();
259 item2List2.SetStartIndex(10);
260 item2List2.SetNumberFormat(List.NumberFormat.e_lower_roman); // , UString(""), true);
261 item2List2.AddItem().AddParagraph("item 1[2].0");
262 item2List2.AddItem().AddParagraph("item 1[2].1");
263 item2List2.AddItem().AddParagraph("item 1[2].2");
264
265 ListItem item4 = list.AddItem(); // creates "G."
266 item4.AddParagraph("item 3");
267
268 List item2List1 = item2List.AddItem().AddList();
269 item2List1.SetStartIndex(7);
270 item2List1.SetNumberFormat(List.NumberFormat.e_upper_roman, ")", true);
271 item2List1.AddItem().AddParagraph("item 1[0].3.0");
272
273 flowdoc.AddParagraph("---------------------------------- splitting paragraph ----------------------------------");
274
275 item2List1.ContinueList();
276
277 item2List1.AddItem().AddParagraph("item 1[0].3.1 (continued)");
278 ListItem extraItem = item2List1.AddItem();
279 extraItem.AddParagraph("item 1[0].3.2[0]");
280 extraItem.AddParagraph("item 1[0].3.2[1]");
281 List fourth = extraItem.AddList();
282 fourth.SetNumberFormat(List.NumberFormat.e_decimal, "", true);
283 fourth.AddItem().AddParagraph("FOURTH LEVEL");
284
285 ListItem item5 = list.AddItem(); // creates "H."
286 item5.AddParagraph("item 4 (continued)");
287 }
288
289
290 flowdoc.AddParagraph(" ");
291
292 flowdoc.SetDefaultMargins(72.0, 72.0, 144.0, 228.0);
293 flowdoc.SetDefaultPageSize(650, 750);
294
295 flowdoc.AddParagraph(para_text);
296
297 byte[] clr1 = new byte[] { 50, 50, 199 };
298 byte[] clr2 = new byte[] { 30, 199, 30 };
299
300 for (int i = 0; i < 50; i++)
301 {
302 para = flowdoc.AddParagraph();
303 TextStyledElement st = para.GetTextStyledElement();
304
305 int point_size = (17 * i * i * i) % 13 + 5;
306 if (i % 2 == 0)
307 {
308 st.SetItalic(true);
309 st.SetTextColor(clr1[0], clr1[1], clr1[2]);
310 st.SetBackgroundColor(200, 200, 200);
311 para.SetSpaceBefore(20);
312 para.SetStartIndent(20);
313 para.SetJustificationMode(Paragraph.TextJustification.e_left);
314 }
315 else
316 {
317 st.SetTextColor(clr2[0], clr2[1], clr2[2]);
318 para.SetSpaceBefore(50);
319 para.SetEndIndent(20);
320 para.SetJustificationMode(Paragraph.TextJustification.e_right);
321 }
322
323 para.AddText(para_text);
324 para.AddText(" " + para_text);
325 st.SetFontSize(point_size);
326 }
327
328 // Table creation
329 Table new_table = flowdoc.AddTable();
330 new_table.SetDefaultColumnWidth(100);
331 new_table.SetDefaultRowHeight(15);
332
333 for (int i = 0; i < 4; i++)
334 {
335 TableRow new_row = new_table.AddTableRow();
336 new_row.SetRowHeight(new_table.GetDefaultRowHeight() + i * 5);
337 for (int j = 0; j < 5; j++)
338 {
339 TableCell cell = new_row.AddTableCell();
340 cell.SetBorder(0.5, 255, 0, 0);
341
342 if (i == 3)
343 {
344 if (j % 2 != 0)
345 {
346 cell.SetVerticalAlignment(TableCell.CellAlignmentVertical.e_center);
347 }
348 else
349 {
350 cell.SetVerticalAlignment(TableCell.CellAlignmentVertical.e_bottom);
351 }
352 }
353
354 if ((i == 3) && (j == 4))
355 {
356 Paragraph para_title = cell.AddParagraph("Table title");
357 para_title.SetJustificationMode(Paragraph.TextJustification.e_center);
358
359 Table nested_table = cell.AddTable();
360 nested_table.SetDefaultColumnWidth(33);
361 nested_table.SetDefaultRowHeight(5);
362 nested_table.SetBorder(0.5, 0, 0, 0);
363
364 for (int nested_row_index = 0; nested_row_index < 3; nested_row_index++)
365 {
366 TableRow nested_row = nested_table.AddTableRow();
367 for (int nested_column_index = 0; nested_column_index < 3; nested_column_index++)
368 {
369 string str = nested_row_index.ToString() + "/" + nested_column_index.ToString();
370 TableCell nested_cell = nested_row.AddTableCell();
371 nested_cell.SetBackgroundColor(200, 200, 255);
372 nested_cell.SetBorder(0.1, 0, 255, 0);
373
374 Paragraph new_para = nested_cell.AddParagraph(str);
375 new_para.SetJustificationMode(Paragraph.TextJustification.e_right);
376 }
377 }
378 }
379 else
380 if ((i == 2) && (j == 2))
381 {
382 para = cell.AddParagraph("Cell " + j.ToString() + " x " + i.ToString() + "\n");
383 para.AddText("to be bold text 1\n");
384 para.AddText("still normal text\n");
385 para.AddText("to be bold text 2");
386 cell.AddParagraph("Second Paragraph");
387 }
388 else
389 {
390 cell.AddParagraph("Cell " + j.ToString() + " x " + i.ToString());
391 }
392 }
393 }
394
395 // Walk the content tree and modify some text runs.
396 ModifyContentTree(flowdoc.GetBody());
397
398
399 // Merge cells
400 TableCell merged_cell = new_table.GetTableCell(2, 0).MergeCellsRight(1);
401 merged_cell.SetHorizontalAlignment(TableCell.CellAlignmentHorizontal.e_middle);
402
403 new_table.GetTableCell(0, 0).MergeCellsDown(1)
404 .SetVerticalAlignment(TableCell.CellAlignmentVertical.e_center);
405
406
407 // Walk over the table and change the first cell in each row.
408 int row = 0;
409 byte[] clr_row1 = new byte[] { 175, 240, 240 };
410 byte[] clr_row2 = new byte[] { 250, 250, 175 };
411
412 for (ContentNodeIterator table_itr = new_table.GetContentNodeIterator();
413 table_itr.HasNext();
414 table_itr.Next())
415 {
416 TableRow table_row = table_itr.Current().AsTableRow();
417 if (table_row != null)
418 {
419 for (ContentNodeIterator row_itr = table_row.GetContentNodeIterator();
420 row_itr.HasNext();
421 row_itr.Next())
422 {
423 TableCell table_cell = row_itr.Current().AsTableCell();
424 if (table_cell != null)
425 {
426 if (row % 2 != 0)
427 {
428 table_cell.SetBackgroundColor(clr_row1[0], clr_row1[1], clr_row1[2]);
429 }
430 else
431 {
432 table_cell.SetBackgroundColor(clr_row2[0], clr_row2[1], clr_row2[2]);
433 }
434
435 for (ContentNodeIterator cell_itr = table_cell.GetContentNodeIterator();
436 cell_itr.HasNext();
437 cell_itr.Next())
438 {
439 Paragraph cell_para = cell_itr.Current().AsParagraph();
440 if (cell_para != null)
441 {
442 TextStyledElement st = cell_para.GetTextStyledElement();
443 st.SetTextColor(255, 0, 0);
444 st.SetFontSize(12);
445 }
446 else
447 {
448 Table nested_table = cell_itr.Current().AsTable();
449 if (nested_table != null)
450 {
451 TableCell nested_cell = nested_table.GetTableCell(1, 1);
452 nested_cell.SetBackgroundColor(255, 127, 127);
453 }
454 }
455 }
456 }
457 }
458 }
459
460 ++row;
461 }
462
463 PDFDoc my_pdf = flowdoc.PaginateToPDF();
464 my_pdf.Save(output_path + "created_doc.pdf", SDFDoc.SaveOptions.e_linearized);
465 }
466 }
467 catch (PDFNetException e)
468 {
469 Console.WriteLine(e.Message);
470 }
471 PDFNet.Terminate();
472 }
473 }
474}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales