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}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by PDFTron Systems Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6#include <SDF/Obj.h>
7#include <PDF/PDFNet.h>
8#include <PDF/PDFDoc.h>
9#include <Common/UString.h>
10
11#include <SDF/SDFDoc.h>
12#include <iostream>
13#include <sstream>
14#include "../../LicenseKey/CPP/LicenseKey.h"
15
16#include <Layout/FlowDocument.h>
17#include <Layout/ContentTree.h>
18
19
20using namespace std;
21using namespace pdftron;
22using namespace SDF;
23using namespace PDF;
24using namespace Layout;
25
26namespace {
27
28 // Iterate over all text runs of the document and make every second run
29 // bold with smaller font size.
30 void ModifyContentTree(ContentNode node)
31 {
32 bool bold = false;
33
34 for (ContentNodeIterator itr = node.GetContentNodeIterator();
35 itr.HasNext();
36 itr.Next())
37 {
38 ContentElement el = itr.Current();
39
40 if (ElementRef<ContentNode> content_node = el.AsContentNode())
41 {
42 ModifyContentTree(*content_node);
43 continue;
44 }
45
46 if (ElementRef<TextRun> text_run = el.AsTextRun())
47 {
48 if (bold)
49 {
50 text_run->GetTextStyledElement().SetBold(true);
51 text_run->GetTextStyledElement().SetFontSize(
52 text_run->GetTextStyledElement().GetFontSize() * 0.8);
53 }
54 bold = !bold;
55 continue;
56 }
57 }
58 }
59
60 std::string IntToString(int val)
61 {
62 std::stringstream ss;
63 ss << val;
64 return ss.str();
65 }
66}
67
68int main(int argc, char *argv[])
69{
70 std::string const para_text(
71 "Lorem ipsum dolor "
72 "sit amet, consectetur adipisicing elit, sed "
73 "do eiusmod tempor incididunt ut labore "
74 "et dolore magna aliqua. Ut enim ad "
75 "minim veniam, quis nostrud exercitation "
76 "ullamco laboris nisi ut aliquip ex ea "
77 "commodo consequat. Duis aute irure "
78 "dolor in reprehenderit in voluptate velit "
79 "esse cillum dolore eu fugiat nulla pariatur. "
80 "Excepteur sint occaecat cupidatat "
81 "non proident, sunt in culpa qui officia "
82 "deserunt mollit anim id est laborum.");
83
84 int ret = 0;
85 PDFNet::Initialize(LicenseKey);
86
87 // Relative path to the folder containing test files.
88 string output_path = "../../TestFiles/Output/";
89
90 try
91 {
92 FlowDocument flowdoc;
93
94 Paragraph para = flowdoc.AddParagraph();
95 TextStyledElement st_para = para.GetTextStyledElement();
96
97 st_para.SetFontSize(24);
98 st_para.SetTextColor(255, 0, 0);
99 para.AddText(UString("Start \tRed \tText\n"));
100
101 para.AddTabStop(150);
102 para.AddTabStop(250);
103 st_para.SetTextColor(0, 0, 255);
104 para.AddText(UString("Start \tBlue \tText\n"));
105
106 TextRun last_run = para.AddText(UString("Start Green Text\n"));
107
108 ContentNodeIterator itr = para.GetContentNodeIterator();
109 for (int i = 0; itr.HasNext(); itr.Next(), i++)
110 {
111 ContentElement el = itr.Current();
112
113 if (ElementRef<TextRun> run = el.AsTextRun())
114 {
115 run->GetTextStyledElement().SetFontSize(12);
116
117 if (i == 0)
118 {
119 // Restore red color.
120 run->SetText(run->GetText() + "(restored \tred \tcolor)\n");
121 run->GetTextStyledElement().SetTextColor(255, 0, 0);
122 }
123 }
124 }
125
126 TextStyledElement st_last = last_run.GetTextStyledElement();
127
128 st_last.SetTextColor(0, 255, 0);
129 st_last.SetItalic(true);
130 st_last.SetFontSize(18);
131
132 para.GetTextStyledElement().SetBold(true);
133 para.SetBorder(0.2, 0, 127, 0);
134 st_last.SetBold(false);
135
136 {
137 flowdoc.AddParagraph(UString("Simple list creation process. All elements are added in their natural order\n\n"));
138
139 List list = flowdoc.AddList();
140 list.SetNumberFormat(List::e_upper_letter);
141 list.SetStartIndex(4);
142
143 ListItem item = list.AddItem(); // creates "D."
144 item.AddParagraph("item 0[0]");
145 Paragraph px = item.AddParagraph("item 0[1]");
146 TextStyledElement xx_para = px.GetTextStyledElement();
147 xx_para.SetTextColor(255, 99, 71);
148 px.AddText(UString(" Some More Text!"));
149
150
151 ListItem item2 = list.AddItem(); // creates "E."
152 List item2List = item2.AddList();
153 item2List.SetStartIndex(0);
154 item2List.SetNumberFormat(List::e_decimal, UString(""), true);
155 item2List.AddItem().AddParagraph("item 1[0].0");
156 Paragraph pp = item2List.AddItem().AddParagraph("item 1[0].1");
157 TextStyledElement sx_para = pp.GetTextStyledElement();
158 sx_para.SetTextColor(0, 0, 255);
159 pp.AddText(UString(" Some More Text"));
160 item2List.AddItem().AddParagraph("item 1[0].2");
161 List item2List1 = item2List.AddItem().AddList();
162 item2List1.SetStartIndex(7);
163 item2List1.SetNumberFormat(List::e_upper_roman, UString(")"), true);
164 item2List1.AddItem().AddParagraph("item 1[0].3.0");
165 item2List1.AddItem().AddParagraph("item 1[0].3.1");
166
167 ListItem extraItem = item2List1.AddItem();
168 extraItem.AddParagraph("item 1[0].3.2[0]");
169 extraItem.AddParagraph("item 1[0].3.2[1]");
170 List fourth = extraItem.AddList();
171 fourth.SetNumberFormat(List::e_decimal, UString(""), true);
172 fourth.AddItem().AddParagraph("Fourth Level");
173
174 fourth = item2List1.AddItem().AddList();
175 fourth.SetNumberFormat(List::e_lower_letter, UString(""), true);
176 fourth.AddItem().AddParagraph("Fourth Level (again)");
177
178
179 item2.AddParagraph("item 1[1]");
180 List item2List2 = item2.AddList();
181 item2List2.SetStartIndex(10);
182 item2List2.SetNumberFormat(List::e_lower_roman); // , UString(""), true);
183 item2List2.AddItem().AddParagraph("item 1[2].0");
184 item2List2.AddItem().AddParagraph("item 1[2].1");
185 item2List2.AddItem().AddParagraph("item 1[2].2");
186
187 ListItem item3 = list.AddItem(); // creates "F."
188 item3.AddParagraph("item 2");
189
190 ListItem item4 = list.AddItem(); // creates "G."
191 item4.AddParagraph("item 3");
192
193 ListItem item5 = list.AddItem(); // creates "H."
194 item5.AddParagraph("item 4");
195 }
196
197 for (ContentNodeIterator itr = flowdoc.GetBody().GetContentNodeIterator();
198 itr.HasNext();
199 itr.Next())
200 {
201 ContentElement el = itr.Current();
202
203 if (ElementRef<List> list = el.AsList())
204 {
205 if (list->GetIndentationLevel() == 1)
206 {
207 Paragraph p = list->AddItem().AddParagraph("Item added during iteration");
208 TextStyledElement ps = p.GetTextStyledElement();
209 ps.SetTextColor(0, 127, 0);
210 }
211 }
212
213 if (ElementRef<ListItem> list_item = el.AsListItem())
214 {
215 if (list_item->GetIndentationLevel() == 2)
216 {
217 Paragraph p = list_item->AddParagraph("* Paragraph added during iteration");
218 TextStyledElement ps = p.GetTextStyledElement();
219 ps.SetTextColor(0, 0, 255);
220 }
221 }
222 }
223
224 flowdoc.AddParagraph(UString("\f")); // page break
225
226 {
227 flowdoc.AddParagraph(UString("Nonlinear list creation flow. Items are added randomly. List body separated by a paragraph, does not belong to the list\n\n"));
228
229 List list = flowdoc.AddList();
230 list.SetNumberFormat(List::e_upper_letter);
231 list.SetStartIndex(4);
232
233 ListItem item = list.AddItem(); // creates "D."
234 item.AddParagraph("item 0[0]");
235 Paragraph px = item.AddParagraph("item 0[1]");
236 TextStyledElement xx_para = px.GetTextStyledElement();
237 xx_para.SetTextColor(255, 99, 71);
238 px.AddText(UString(" Some More Text!"));
239 item.AddParagraph("item 0[2]");
240 px = item.AddParagraph("item 0[3]");
241 item.AddParagraph("item 0[4]");
242 xx_para = px.GetTextStyledElement();
243 xx_para.SetTextColor(255, 99, 71);
244
245
246 ListItem item2 = list.AddItem(); // creates "E."
247 List item2List = item2.AddList();
248 item2List.SetStartIndex(0);
249 item2List.SetNumberFormat(List::e_decimal, UString(""), true);
250 item2List.AddItem().AddParagraph("item 1[0].0");
251 Paragraph pp = item2List.AddItem().AddParagraph("item 1[0].1");
252 TextStyledElement sx_para = pp.GetTextStyledElement();
253 sx_para.SetTextColor(0, 0, 255);
254 pp.AddText(UString(" Some More Text"));
255
256
257 ListItem item3 = list.AddItem(); // creates "F."
258 item3.AddParagraph("item 2");
259
260 item2List.AddItem().AddParagraph("item 1[0].2");
261
262 item2.AddParagraph("item 1[1]");
263 List item2List2 = item2.AddList();
264 item2List2.SetStartIndex(10);
265 item2List2.SetNumberFormat(List::e_lower_roman); // , UString(""), true);
266 item2List2.AddItem().AddParagraph("item 1[2].0");
267 item2List2.AddItem().AddParagraph("item 1[2].1");
268 item2List2.AddItem().AddParagraph("item 1[2].2");
269
270 ListItem item4 = list.AddItem(); // creates "G."
271 item4.AddParagraph("item 3");
272
273 List item2List1 = item2List.AddItem().AddList();
274 item2List1.SetStartIndex(7);
275 item2List1.SetNumberFormat(List::e_upper_roman, UString(")"), true);
276 item2List1.AddItem().AddParagraph("item 1[0].3.0");
277
278 flowdoc.AddParagraph(UString("---------------------------------- splitting paragraph ----------------------------------"));
279
280 item2List1.ContinueList();
281
282 item2List1.AddItem().AddParagraph("item 1[0].3.1 (continued)");
283 ListItem extraItem = item2List1.AddItem();
284 extraItem.AddParagraph("item 1[0].3.2[0]");
285 extraItem.AddParagraph("item 1[0].3.2[1]");
286 List fourth = extraItem.AddList();
287 fourth.SetNumberFormat(List::e_decimal, UString(""), true);
288 fourth.AddItem().AddParagraph("FOURTH LEVEL");
289
290 ListItem item5 = list.AddItem(); // creates "H."
291 item5.AddParagraph("item 4 (continued)");
292 }
293
294
295 flowdoc.AddParagraph(UString(" "));
296
297 flowdoc.SetDefaultMargins(72.0, 72.0, 144.0, 228.0);
298 flowdoc.SetDefaultPageSize(650, 750);
299 flowdoc.AddParagraph(UString(para_text));
300
301 UInt8 const clr1[] = {50, 50, 199};
302 UInt8 const clr2[] = {30, 199, 30};
303
304 for (int i = 0; i < 50; i++) {
305 Paragraph para = flowdoc.AddParagraph();
306 TextStyledElement st = para.GetTextStyledElement();
307
308 int point_size = (17*i*i*i)%13+5;
309 if (i % 2 == 0)
310 {
311 st.SetItalic(true);
312 st.SetTextColor(clr1[0], clr1[1], clr1[2]);
313 st.SetBackgroundColor(200, 200, 200);
314 para.SetSpaceBefore(20);
315 para.SetStartIndent(20);
316 para.SetJustificationMode(Paragraph::e_text_justify_left);
317 }
318 else
319 {
320 st.SetTextColor(clr2[0], clr2[1], clr2[2]);
321 para.SetSpaceBefore(50);
322 para.SetEndIndent(20);
323 para.SetJustificationMode(Paragraph::e_text_justify_right);
324 }
325
326 para.AddText(UString(para_text));
327 para.AddText(UString(" " + para_text));
328 st.SetFontSize(point_size);
329 }
330
331 // Table creation
332 Table new_table = flowdoc.AddTable();
333 new_table.SetDefaultColumnWidth(100);
334 new_table.SetDefaultRowHeight(15);
335
336 for (int i = 0; i < 4; i++)
337 {
338 TableRow row = new_table.AddTableRow();
339 row.SetRowHeight(new_table.GetDefaultRowHeight() + i*5);
340 for (int j = 0; j < 5; j++)
341 {
342 TableCell cell = row.AddTableCell();
343 cell.SetBorder(0.5, 255, 0, 0);
344
345 if (i == 3)
346 {
347 if (j % 2)
348 {
349 cell.SetVerticalAlignment(TableCell::e_alignment_center);
350 }
351 else
352 {
353 cell.SetVerticalAlignment(TableCell::e_alignment_bottom);
354 }
355 }
356
357 if ((i == 3) && (j == 4))
358 {
359 Paragraph para_title = cell.AddParagraph(UString("Table title"));
360 para_title.SetJustificationMode(Paragraph::e_text_justify_center);
361
362 Table nested_table = cell.AddTable();
363 nested_table.SetDefaultColumnWidth(33);
364 nested_table.SetDefaultRowHeight(5);
365 nested_table.SetBorder(0.5, 0, 0, 0);
366
367 for (int nested_row_index = 0; nested_row_index < 3; nested_row_index++)
368 {
369 TableRow nested_row = nested_table.AddTableRow();
370 for (int nested_column_index = 0; nested_column_index < 3; nested_column_index++)
371 {
372 std::string str = IntToString(nested_row_index) + "/" + IntToString(nested_column_index);
373 TableCell nested_cell = nested_row.AddTableCell();
374 nested_cell.SetBackgroundColor(200, 200, 255);
375 nested_cell.SetBorder(0.1, 0, 255, 0);
376
377 Paragraph para = nested_cell.AddParagraph(UString(str));
378 para.SetJustificationMode(Paragraph::e_text_justify_right);
379 }
380 }
381 }
382 else
383 if ((i == 2) && (j == 2))
384 {
385 Paragraph para = cell.AddParagraph(UString("Cell " + IntToString(j) + " x " + IntToString(i) + "\n"));
386 para.AddText(UString("to be bold text 1\n"));
387 para.AddText(UString("still normal text\n"));
388 para.AddText(UString("to be bold text 2"));
389 cell.AddParagraph(UString("Second Paragraph"));
390 }
391 else
392 {
393 cell.AddParagraph(UString("Cell " + IntToString(j) + " x " + IntToString(i)));
394 }
395 }
396 }
397
398 // Walk the content tree and modify some text runs.
399 ModifyContentTree(flowdoc.GetBody());
400
401 // Merge cells
402 TableCell merged_cell = new_table.GetTableCell(2, 0).MergeCellsRight(1);
403 merged_cell.SetHorizontalAlignment(TableCell::e_alignment_middle);
404
405 new_table.GetTableCell(0, 0).MergeCellsDown(1).SetVerticalAlignment(TableCell::e_alignment_center);
406
407 // Walk over the table and change the first cell in each row.
408 int row_idx = 0;
409 UInt8 const clr_row1[] = {175, 240, 240};
410 UInt8 const clr_row2[] = {250, 250, 175};
411
412 for (ContentNodeIterator table_itr = new_table.GetContentNodeIterator();
413 table_itr.HasNext();
414 table_itr.Next())
415 {
416 if (ElementRef<TableRow> row = table_itr.Current().AsTableRow())
417 {
418 for (ContentNodeIterator row_itr = row->GetContentNodeIterator();
419 row_itr.HasNext();
420 row_itr.Next())
421 {
422 if (ElementRef<TableCell> cell = row_itr.Current().AsTableCell())
423 {
424 if (row_idx % 2)
425 {
426 cell->SetBackgroundColor(clr_row1[0], clr_row1[1], clr_row1[2]);
427 }
428 else
429 {
430 cell->SetBackgroundColor(clr_row2[0], clr_row2[1], clr_row2[2]);
431 }
432
433 for (ContentNodeIterator cell_itr = cell->GetContentNodeIterator();
434 cell_itr.HasNext();
435 cell_itr.Next())
436 {
437 if (ElementRef<Paragraph> para = cell_itr.Current().AsParagraph())
438 {
439 TextStyledElement st = para->GetTextStyledElement();
440 st.SetTextColor(255, 0, 0);
441 st.SetFontSize(12);
442 }
443 else
444 {
445 if (ElementRef<Table> nested_table = cell_itr.Current().AsTable())
446 {
447 TableCell nested_cell = nested_table->GetTableCell(1, 1);
448 nested_cell.SetBackgroundColor(255, 127, 127);
449 }
450 }
451 }
452 }
453 }
454 }
455
456 ++row_idx;
457 }
458
459 PDFDoc my_pdf = flowdoc.PaginateToPDF();
460 my_pdf.Save(output_path + "created_doc.pdf", SDFDoc::e_linearized, 0);
461 }
462 catch(Common::Exception& e)
463 {
464 cout << e << endl;
465 ret = 1;
466 }
467 catch(...)
468 {
469 cout << "Unknown Exception" << endl;
470 ret = 1;
471 }
472
473 PDFNet::Terminate();
474 return ret;
475}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6import com.pdftron.common.Matrix2D;
7import com.pdftron.common.PDFNetException;
8import com.pdftron.pdf.*;
9import com.pdftron.sdf.SDFDoc;
10import com.pdftron.filters.*;
11import com.pdftron.layout.*;
12
13public class DocumentCreationTest {
14
15 // Iterate over all text runs of the document and make every second run
16 // bold with smaller font size.
17 public static void ModifyContentTree(ContentNode node) throws PDFNetException
18 {
19 boolean bold = false;
20
21 ContentNodeIterator itr = node.getContentNodeIterator();
22 while (itr.hasNext())
23 {
24 ContentElement el = itr.next();
25
26 TextRun text_run = el.asTextRun();
27 if (text_run != null)
28 {
29 if (bold)
30 {
31 text_run.getTextStyledElement().setBold(true);
32 text_run.getTextStyledElement().setFontSize(text_run.getTextStyledElement().getFontSize() * 0.8);
33 }
34 bold = !bold;
35 continue;
36 }
37
38 ContentNode content_node = el.asContentNode();
39 if (content_node != null)
40 {
41 ModifyContentTree(content_node);
42 continue;
43 }
44 }
45 }
46
47 public static void main(String[] args) {
48 PDFNet.initialize(PDFTronLicense.Key());
49
50 // Relative path to the folder containing test files.
51 String output_path = "../../TestFiles/Output/";
52 String output_name = "created_doc.pdf";
53 String para_text =
54 "Lorem ipsum dolor "
55 + "sit amet, consectetur adipisicing elit, sed "
56 + "do eiusmod tempor incididunt ut labore "
57 + "et dolore magna aliqua. Ut enim ad "
58 + "minim veniam, quis nostrud exercitation "
59 + "ullamco laboris nisi ut aliquip ex ea "
60 + "commodo consequat. Duis aute irure "
61 + "dolor in reprehenderit in voluptate velit "
62 + "esse cillum dolore eu fugiat nulla pariatur. "
63 + "Excepteur sint occaecat cupidatat "
64 + "non proident, sunt in culpa qui officia "
65 + "deserunt mollit anim id est laborum.";
66
67 try {
68 FlowDocument flowdoc = new FlowDocument();
69 Paragraph para = flowdoc.addParagraph();
70 TextStyledElement st_para = para.getTextStyledElement();
71
72 st_para.setFontSize(24);
73 st_para.setTextColor(255, 0, 0);
74 para.addText("Start \tRed \tText\n");
75
76 para.addTabStop(150);
77 para.addTabStop(250);
78 st_para.setTextColor(0, 0, 255);
79 para.addText("Start \tBlue \tText\n");
80
81 TextRun last_run = para.addText("Start Green Text\n");
82
83 boolean start = true;
84 ContentNodeIterator itr = para.getContentNodeIterator();
85 while (itr.hasNext())
86 {
87 ContentElement el = itr.next();
88 TextRun run = el.asTextRun();
89 if (run != null)
90 {
91 run.getTextStyledElement().setFontSize(12);
92 if (start)
93 {
94 // Restore red color.
95 start = false;
96 run.setText(run.getText() + "(restored \tred \tcolor)\n");
97 run.getTextStyledElement().setTextColor(255, 0, 0);
98 }
99 }
100 }
101
102 TextStyledElement st_last = last_run.getTextStyledElement();
103 st_last.setTextColor(0, 255, 0);
104 st_last.setItalic(true);
105 st_last.setFontSize(18);
106
107 para.getTextStyledElement().setBold(true);
108 para.setBorder(0.2, 0, 127, 0);
109 st_last.setBold(false);
110
111 {
112 flowdoc.addParagraph("Simple list creation process. All elements are added in their natural order\n\n");
113
114 List list = flowdoc.addList();
115 list.setNumberFormat(List.ListItemNumberFormat.e_upper_letter);
116 list.setStartIndex(4);
117
118 ListItem item = list.addItem(); // creates "D."
119 item.addParagraph("item 0[0]");
120 Paragraph px = item.addParagraph("item 0[1]");
121 TextStyledElement xx_para = px.getTextStyledElement();
122 xx_para.setTextColor(255, 99, 71);
123 px.addText(" Some More Text!");
124
125
126 ListItem item2 = list.addItem(); // creates "E."
127 List item2List = item2.addList();
128 item2List.setStartIndex(0);
129 item2List.setNumberFormat(List.ListItemNumberFormat.e_decimal, "", true);
130 item2List.addItem().addParagraph("item 1[0].0");
131 Paragraph pp = item2List.addItem().addParagraph("item 1[0].1");
132 TextStyledElement sx_para = pp.getTextStyledElement();
133 sx_para.setTextColor(0, 0, 255);
134 pp.addText(" Some More Text");
135 item2List.addItem().addParagraph("item 1[0].2");
136 List item2List1 = item2List.addItem().addList();
137 item2List1.setStartIndex(7);
138 item2List1.setNumberFormat(List.ListItemNumberFormat.e_upper_roman, ")", true);
139 item2List1.addItem().addParagraph("item 1[0].3.0");
140 item2List1.addItem().addParagraph("item 1[0].3.1");
141
142 ListItem extraItem = item2List1.addItem();
143 extraItem.addParagraph("item 1[0].3.2[0]");
144 extraItem.addParagraph("item 1[0].3.2[1]");
145 List fourth = extraItem.addList();
146 fourth.setNumberFormat(List.ListItemNumberFormat.e_decimal, "", true);
147 fourth.addItem().addParagraph("Fourth Level");
148
149 fourth = item2List1.addItem().addList();
150 fourth.setNumberFormat(List.ListItemNumberFormat.e_lower_letter, "", true);
151 fourth.addItem().addParagraph("Fourth Level (again)");
152
153
154 item2.addParagraph("item 1[1]");
155 List item2List2 = item2.addList();
156 item2List2.setStartIndex(10);
157 item2List2.setNumberFormat(List.ListItemNumberFormat.e_lower_roman); // , "", true);
158 item2List2.addItem().addParagraph("item 1[2].0");
159 item2List2.addItem().addParagraph("item 1[2].1");
160 item2List2.addItem().addParagraph("item 1[2].2");
161
162 ListItem item3 = list.addItem(); // creates "F."
163 item3.addParagraph("item 2");
164
165 ListItem item4 = list.addItem(); // creates "G."
166 item4.addParagraph("item 3");
167
168 ListItem item5 = list.addItem(); // creates "H."
169 item5.addParagraph("item 4");
170 }
171
172 for (itr = flowdoc.getBody().getContentNodeIterator(); itr.hasNext();)
173 {
174 ContentElement el = itr.next();
175
176 List list = el.asList();
177 if (list != null)
178 {
179 if (list.getIndentationLevel() == 1)
180 {
181 Paragraph p = list.addItem().addParagraph("Item added during iteration");
182 TextStyledElement ps = p.getTextStyledElement();
183 ps.setTextColor(0, 127, 0);
184 }
185 }
186
187 ListItem list_item = el.asListItem();
188 if (list_item != null)
189 {
190 if (list_item.getIndentationLevel() == 2)
191 {
192 Paragraph p = list_item.addParagraph("* Paragraph added during iteration");
193 TextStyledElement ps = p.getTextStyledElement();
194 ps.setTextColor(0, 0, 255);
195 }
196 }
197 }
198
199 flowdoc.addParagraph("\f"); // page break
200
201 {
202 flowdoc.addParagraph("Nonlinear list creation flow. Items are added randomly. List body separated by a paragraph, does not belong to the list\n\n");
203
204 List list = flowdoc.addList();
205 list.setNumberFormat(List.ListItemNumberFormat.e_upper_letter);
206 list.setStartIndex(4);
207
208 ListItem item = list.addItem(); // creates "D."
209 item.addParagraph("item 0[0]");
210 Paragraph px = item.addParagraph("item 0[1]");
211 TextStyledElement xx_para = px.getTextStyledElement();
212 xx_para.setTextColor(255, 99, 71);
213 px.addText(" Some More Text!");
214 item.addParagraph("item 0[2]");
215 px = item.addParagraph("item 0[3]");
216 item.addParagraph("item 0[4]");
217 xx_para = px.getTextStyledElement();
218 xx_para.setTextColor(255, 99, 71);
219
220
221 ListItem item2 = list.addItem(); // creates "E."
222 List item2List = item2.addList();
223 item2List.setStartIndex(0);
224 item2List.setNumberFormat(List.ListItemNumberFormat.e_decimal, "", true);
225 item2List.addItem().addParagraph("item 1[0].0");
226 Paragraph pp = item2List.addItem().addParagraph("item 1[0].1");
227 TextStyledElement sx_para = pp.getTextStyledElement();
228 sx_para.setTextColor(0, 0, 255);
229 pp.addText(" Some More Text");
230
231
232 ListItem item3 = list.addItem(); // creates "F."
233 item3.addParagraph("item 2");
234
235 item2List.addItem().addParagraph("item 1[0].2");
236
237 item2.addParagraph("item 1[1]");
238 List item2List2 = item2.addList();
239 item2List2.setStartIndex(10);
240 item2List2.setNumberFormat(List.ListItemNumberFormat.e_lower_roman); // , "", true);
241 item2List2.addItem().addParagraph("item 1[2].0");
242 item2List2.addItem().addParagraph("item 1[2].1");
243 item2List2.addItem().addParagraph("item 1[2].2");
244
245 ListItem item4 = list.addItem(); // creates "G."
246 item4.addParagraph("item 3");
247
248 List item2List1 = item2List.addItem().addList();
249 item2List1.setStartIndex(7);
250 item2List1.setNumberFormat(List.ListItemNumberFormat.e_upper_roman, ")", true);
251 item2List1.addItem().addParagraph("item 1[0].3.0");
252
253 flowdoc.addParagraph("---------------------------------- splitting paragraph ----------------------------------");
254
255 item2List1.continueList();
256
257 item2List1.addItem().addParagraph("item 1[0].3.1 (continued)");
258 ListItem extraItem = item2List1.addItem();
259 extraItem.addParagraph("item 1[0].3.2[0]");
260 extraItem.addParagraph("item 1[0].3.2[1]");
261 List fourth = extraItem.addList();
262 fourth.setNumberFormat(List.ListItemNumberFormat.e_decimal, "", true);
263 fourth.addItem().addParagraph("FOURTH LEVEL");
264
265 ListItem item5 = list.addItem(); // creates "H."
266 item5.addParagraph("item 4 (continued)");
267 }
268
269
270 flowdoc.addParagraph(" ");
271
272 flowdoc.setDefaultMargins(72.0, 72.0, 144.0, 228.0);
273 flowdoc.setDefaultPageSize(650, 750);
274 flowdoc.addParagraph(para_text);
275
276 int[] clr1 = new int[] {50, 50, 199};
277 int[] clr2 = new int[] {30, 199, 30};
278
279 for (int i = 0; i < 50; i++)
280 {
281 para = flowdoc.addParagraph();
282 TextStyledElement st = para.getTextStyledElement();
283
284 int point_size = (17*i*i*i)%13+5;
285 if (i % 2 == 0)
286 {
287 st.setItalic(true);
288 st.setTextColor(clr1[0], clr1[1], clr1[2]);
289 st.setBackgroundColor(200, 200, 200);
290 para.setSpaceBefore(20);
291 para.setStartIndent(20);
292 para.setJustificationMode(Paragraph.TextJustification.e_text_justify_left);
293 }
294 else
295 {
296 st.setTextColor(clr2[0], clr2[1], clr2[2]);
297 para.setSpaceBefore(50);
298 para.setEndIndent(20);
299 para.setJustificationMode(Paragraph.TextJustification.e_text_justify_right);
300 }
301
302 para.addText(para_text);
303 para.addText(" " + para_text);
304 st.setFontSize(point_size);
305 }
306
307 // Table creation
308 Table new_table = flowdoc.addTable();
309 new_table.setDefaultColumnWidth(100);
310 new_table.setDefaultRowHeight(15);
311
312 for (int i = 0; i < 4; i++)
313 {
314 TableRow row = new_table.addTableRow();
315 row.setRowHeight(new_table.getDefaultRowHeight() + i*5);
316 for (int j = 0; j < 5; j++)
317 {
318 TableCell cell = row.addTableCell();
319 cell.setBorder(0.5, 255, 0, 0);
320
321 if (i == 3)
322 {
323 if (j % 2 != 0)
324 {
325 cell.setVerticalAlignment(TableCell.CellAlignmentVerticalVals.e_alignment_center);
326 }
327 else
328 {
329 cell.setVerticalAlignment(TableCell.CellAlignmentVerticalVals.e_alignment_bottom);
330 }
331 }
332
333 if ((i == 3) && (j == 4))
334 {
335 Paragraph para_title = cell.addParagraph("Table title");
336 para_title.setJustificationMode(Paragraph.TextJustification.e_text_justify_center);
337
338 Table nested_table = cell.addTable();
339 nested_table.setDefaultColumnWidth(33);
340 nested_table.setDefaultRowHeight(5);
341 nested_table.setBorder(0.5, 0, 0, 0);
342
343 for (int nested_row_index = 0; nested_row_index < 3; nested_row_index++)
344 {
345 TableRow nested_row = nested_table.addTableRow();
346 for (int nested_column_index = 0; nested_column_index < 3; nested_column_index++)
347 {
348 String str = Integer.toString(nested_row_index) + "/" + Integer.toString(nested_column_index);
349 TableCell nested_cell = nested_row.addTableCell();
350 nested_cell.setBackgroundColor(200, 200, 255);
351 nested_cell.setBorder(0.1, 0, 255, 0);
352
353 Paragraph new_para = nested_cell.addParagraph(str);
354 new_para.setJustificationMode(Paragraph.TextJustification.e_text_justify_right);
355 }
356 }
357 }
358 else
359 if ((i == 2) && (j == 2))
360 {
361 Paragraph new_para = cell.addParagraph("Cell " + Integer.toString(j) + " x " + Integer.toString(i) + "\n");
362 new_para.addText("to be bold text 1\n");
363 new_para.addText("still normal text\n");
364 new_para.addText("to be bold text 2");
365 cell.addParagraph("Second Paragraph");
366 }
367 else
368 {
369 cell.addParagraph("Cell " + Integer.toString(j) + " x " + Integer.toString(i));
370 }
371 }
372 }
373
374 // Walk the content tree and modify some text runs.
375 ModifyContentTree(flowdoc.getBody());
376
377 // Merge cells
378 TableCell merged_cell = new_table.getTableCell(2, 0).mergeCellsRight(1);
379 merged_cell.setHorizontalAlignment(TableCell.CellAlignmentHorizontalVals.e_alignment_middle);
380
381 new_table.getTableCell(0, 0).mergeCellsDown(1).setVerticalAlignment(TableCell.CellAlignmentVerticalVals.e_alignment_center);
382
383 // Walk over the table and change the first cell in each row.
384 int row_idx = 0;
385 int[] clr_row1 = new int[] {175, 240, 240};
386 int[] clr_row2 = new int[] {250, 250, 175};
387
388 for (ContentNodeIterator table_itr = new_table.getContentNodeIterator();
389 table_itr.hasNext();)
390 {
391 TableRow row = table_itr.next().asTableRow();
392 if (row != null)
393 {
394 for (ContentNodeIterator row_itr = row.getContentNodeIterator();
395 row_itr.hasNext();)
396 {
397 TableCell cell = row_itr.next().asTableCell();
398 if (cell != null)
399 {
400 if (row_idx % 2 != 0)
401 {
402 cell.setBackgroundColor(clr_row1[0], clr_row1[1], clr_row1[2]);
403 }
404 else
405 {
406 cell.setBackgroundColor(clr_row2[0], clr_row2[1], clr_row2[2]);
407 }
408
409 for (ContentNodeIterator cell_itr = cell.getContentNodeIterator();
410 cell_itr.hasNext();)
411 {
412 ContentElement element = cell_itr.next();
413 para = element.asParagraph();
414 if (para != null)
415 {
416 TextStyledElement st = para.getTextStyledElement();
417 st.setTextColor(255, 0, 0);
418 st.setFontSize(12);
419 }
420 else
421 {
422 Table nested_table = element.asTable();
423 if (nested_table != null)
424 {
425 TableCell nested_cell = nested_table.getTableCell(1, 1);
426 nested_cell.setBackgroundColor(255, 127, 127);
427 }
428 }
429 }
430 }
431 }
432 }
433
434 ++row_idx;
435 }
436
437 PDFDoc my_pdf = flowdoc.paginateToPDF();
438 my_pdf.save(output_path+output_name, SDFDoc.SaveMode.LINEARIZED, null);
439 }
440 catch (Exception e)
441 {
442 e.printStackTrace();
443 }
444
445 PDFNet.terminate();
446 }
447}
1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6import site
7site.addsitedir("../../../PDFNetC/Lib")
8import sys
9from PDFNetPython import *
10
11import platform
12
13sys.path.append("../../LicenseKey/PYTHON")
14from LicenseKey import *
15
16# Relative path to the folder containing the test files.
17output_path = "../../TestFiles/Output/"
18output_name = "created_doc.pdf"
19
20def modify_content_tree(node):
21 bold = False
22
23 itr = node.GetContentNodeIterator()
24 while itr.HasNext():
25 el = itr.Current()
26 itr.Next()
27
28 text_run = el.AsTextRun()
29 if text_run is not None:
30 if bold:
31 text_run.GetTextStyledElement().SetBold(True)
32 text_run.GetTextStyledElement().SetFontSize(
33 text_run.GetTextStyledElement().GetFontSize() * 0.8)
34 bold = not bold
35 continue
36
37 content_node = el.AsContentNode()
38 if content_node is not None:
39 modify_content_tree(content_node)
40 continue
41
42def main():
43 # The first step in every application using PDFNet is to initialize the
44 # library. The library is usually initialized only once, but calling
45 # Initialize() multiple times is also fine.
46 PDFNet.Initialize(LicenseKey)
47
48 para_text = ("Lorem ipsum dolor "
49 "sit amet, consectetur adipisicing elit, sed "
50 "do eiusmod tempor incididunt ut labore "
51 "et dolore magna aliqua. Ut enim ad "
52 "minim veniam, quis nostrud exercitation "
53 "ullamco laboris nisi ut aliquip ex ea "
54 "commodo consequat. Duis aute irure "
55 "dolor in reprehenderit in voluptate velit "
56 "esse cillum dolore eu fugiat nulla pariatur. "
57 "Excepteur sint occaecat cupidatat "
58 "non proident, sunt in culpa qui officia "
59 "deserunt mollit anim id est laborum.")
60
61 result = True
62
63 try:
64 flowdoc = FlowDocument()
65 para = flowdoc.AddParagraph()
66 st_para = para.GetTextStyledElement()
67
68 st_para.SetFontSize(24)
69 st_para.SetTextColor(255, 0, 0)
70 para.AddText("Start \tRed \tText\n")
71
72 para.AddTabStop(150)
73 para.AddTabStop(250)
74 st_para.SetTextColor(0, 0, 255)
75 para.AddText("Start \tBlue \tText\n")
76
77 last_run = para.AddText("Start Green Text\n")
78
79 start = True
80 itr = para.GetContentNodeIterator()
81 while itr.HasNext():
82 el = itr.Current()
83 run = el.AsTextRun()
84 if run is not None:
85 run.GetTextStyledElement().SetFontSize(12)
86 if start:
87 # Restore red color.
88 start = False
89 run.SetText(run.GetText() + "(restored \tred \tcolor)\n")
90 run.GetTextStyledElement().SetTextColor(255, 0, 0)
91 itr.Next()
92
93 st_last = last_run.GetTextStyledElement()
94 st_last.SetTextColor(0, 255, 0)
95 st_last.SetItalic(True)
96 st_last.SetFontSize(18)
97
98 para.GetTextStyledElement().SetBold(True)
99 para.SetBorder(0.2, 0, 127, 0)
100 st_last.SetBold(False)
101
102 flowdoc.AddParagraph("Simple list creation process. All elements are added in their natural order\n\n")
103
104 list = flowdoc.AddList()
105 list.SetNumberFormat(List.e_upper_letter)
106 list.SetStartIndex(4)
107
108 item = list.AddItem() # creates "D."
109 item.AddParagraph("item 0[0]")
110 px = item.AddParagraph("item 0[1]")
111 xx_para = px.GetTextStyledElement()
112 xx_para.SetTextColor(255, 99, 71)
113 px.AddText(" Some More Text!")
114
115 item2 = list.AddItem() # creates "E."
116 item2List = item2.AddList()
117 item2List.SetStartIndex(0)
118 item2List.SetNumberFormat(List.e_decimal, "", True)
119 item2List.AddItem().AddParagraph("item 1[0].0")
120 pp = item2List.AddItem().AddParagraph("item 1[0].1")
121 sx_para = pp.GetTextStyledElement()
122 sx_para.SetTextColor(0, 0, 255)
123 pp.AddText(" Some More Text")
124 item2List.AddItem().AddParagraph("item 1[0].2")
125 item2List1 = item2List.AddItem().AddList()
126 item2List1.SetStartIndex(7)
127 item2List1.SetNumberFormat(List.e_upper_roman, ")", True)
128 item2List1.AddItem().AddParagraph("item 1[0].3.0")
129 item2List1.AddItem().AddParagraph("item 1[0].3.1")
130
131 extraItem = item2List1.AddItem()
132 extraItem.AddParagraph("item 1[0].3.2[0]")
133 extraItem.AddParagraph("item 1[0].3.2[1]")
134 fourth = extraItem.AddList()
135 fourth.SetNumberFormat(List.e_decimal, "", True)
136 fourth.AddItem().AddParagraph("Fourth Level")
137
138 fourth = item2List1.AddItem().AddList()
139 fourth.SetNumberFormat(List.e_lower_letter, "", True)
140 fourth.AddItem().AddParagraph("Fourth Level (again)")
141
142 item2.AddParagraph("item 1[1]")
143 item2List2 = item2.AddList()
144 item2List2.SetStartIndex(10)
145 item2List2.SetNumberFormat(List.e_lower_roman)
146 item2List2.AddItem().AddParagraph("item 1[2].0")
147 item2List2.AddItem().AddParagraph("item 1[2].1")
148 item2List2.AddItem().AddParagraph("item 1[2].2")
149
150 item3 = list.AddItem() # creates "F."
151 item3.AddParagraph("item 2")
152
153 item4 = list.AddItem() # creates "G."
154 item4.AddParagraph("item 3")
155
156 item5 = list.AddItem() # creates "H."
157 item5.AddParagraph("item 4")
158
159 itr = flowdoc.GetBody().GetContentNodeIterator()
160 while itr.HasNext():
161 el = itr.Current()
162
163 list = el.AsList()
164 if list is not None:
165 if list.GetIndentationLevel() == 1:
166 p = list.AddItem().AddParagraph("Item added during iteration")
167 ps = p.GetTextStyledElement()
168 ps.SetTextColor(0, 127, 0)
169
170 list_item = el.AsListItem()
171 if list_item is not None:
172 if list_item.GetIndentationLevel() == 2:
173 p = list_item.AddParagraph("* Paragraph added during iteration")
174 ps = p.GetTextStyledElement()
175 ps.SetTextColor(0, 0, 255)
176 itr.Next()
177
178 flowdoc.AddParagraph("\f") # page break
179
180 flowdoc.AddParagraph("Nonlinear list creation flow. Items are added randomly."
181 + " List body separated by a paragraph, does not belong to the list\n\n")
182
183 list = flowdoc.AddList()
184 list.SetNumberFormat(List.e_upper_letter)
185 list.SetStartIndex(4)
186
187 item = list.AddItem() # creates "D."
188 item.AddParagraph("item 0[0]")
189 px = item.AddParagraph("item 0[1]")
190 xx_para = px.GetTextStyledElement()
191 xx_para.SetTextColor(255, 99, 71)
192 px.AddText(" Some More Text!")
193 item.AddParagraph("item 0[2]")
194 px = item.AddParagraph("item 0[3]")
195 item.AddParagraph("item 0[4]")
196 xx_para = px.GetTextStyledElement()
197 xx_para.SetTextColor(255, 99, 71)
198
199 item2 = list.AddItem() # creates "E."
200 item2List = item2.AddList()
201 item2List.SetStartIndex(0)
202 item2List.SetNumberFormat(List.e_decimal, "", True)
203 item2List.AddItem().AddParagraph("item 1[0].0")
204 pp = item2List.AddItem().AddParagraph("item 1[0].1")
205 sx_para = pp.GetTextStyledElement()
206 sx_para.SetTextColor(0, 0, 255)
207 pp.AddText(" Some More Text")
208
209 item3 = list.AddItem() # creates "F."
210 item3.AddParagraph("item 2")
211
212 item2List.AddItem().AddParagraph("item 1[0].2")
213
214 item2.AddParagraph("item 1[1]")
215 item2List2 = item2.AddList()
216 item2List2.SetStartIndex(10)
217 item2List2.SetNumberFormat(List.e_lower_roman)
218 item2List2.AddItem().AddParagraph("item 1[2].0")
219 item2List2.AddItem().AddParagraph("item 1[2].1")
220 item2List2.AddItem().AddParagraph("item 1[2].2")
221
222 item4 = list.AddItem() # creates "G."
223 item4.AddParagraph("item 3")
224
225 item2List1 = item2List.AddItem().AddList()
226 item2List1.SetStartIndex(7)
227 item2List1.SetNumberFormat(List.e_upper_roman, ")", True)
228 item2List1.AddItem().AddParagraph("item 1[0].3.0")
229
230 flowdoc.AddParagraph("---------------------------------- splitting paragraph ----------------------------------")
231
232 item2List1.ContinueList()
233
234 item2List1.AddItem().AddParagraph("item 1[0].3.1 (continued)")
235 extraItem = item2List1.AddItem()
236 extraItem.AddParagraph("item 1[0].3.2[0]")
237 extraItem.AddParagraph("item 1[0].3.2[1]")
238 fourth = extraItem.AddList()
239 fourth.SetNumberFormat(List.e_decimal, "", True)
240 fourth.AddItem().AddParagraph("FOURTH LEVEL")
241
242 item5 = list.AddItem() # creates "H."
243 item5.AddParagraph("item 4 (continued)")
244
245 flowdoc.AddParagraph(" ")
246
247 flowdoc.SetDefaultMargins(72.0, 72.0, 144.0, 228.0)
248 flowdoc.SetDefaultPageSize(650, 750)
249 flowdoc.AddParagraph(para_text)
250
251 clr1 = [50, 50, 199]
252 clr2 = [30, 199, 30]
253
254 for i in range(50):
255 para = flowdoc.AddParagraph()
256 style = para.GetTextStyledElement()
257 point_size = (17*i*i*i)%13+5
258 if i % 2 == 0:
259 style.SetItalic(True)
260 style.SetTextColor(clr1[0], clr1[1], clr1[2])
261 style.SetBackgroundColor(200, 200, 200);
262 para.SetSpaceBefore(20)
263 para.SetStartIndent(20)
264 para.SetJustificationMode(Paragraph.e_text_justify_left)
265 else:
266 style.SetTextColor(clr2[0], clr2[1], clr2[2])
267 para.SetSpaceBefore(50)
268 para.SetEndIndent(20)
269 para.SetJustificationMode(Paragraph.e_text_justify_right)
270
271 para.AddText(para_text)
272 para.AddText(" " + para_text)
273 style.SetFontSize(point_size)
274
275 # Table creation
276 new_table = flowdoc.AddTable()
277 new_table.SetDefaultColumnWidth(100)
278 new_table.SetDefaultRowHeight(15)
279
280 for i in range(4):
281 row = new_table.AddTableRow()
282 row.SetRowHeight(new_table.GetDefaultRowHeight() + i*5)
283 for j in range(5):
284 cell = row.AddTableCell()
285 cell.SetBorder(0.5, 255, 0, 0)
286
287 if i == 3:
288 if j % 2 != 0:
289 cell.SetVerticalAlignment(TableCell.e_alignment_center)
290 else:
291 cell.SetVerticalAlignment(TableCell.e_alignment_bottom)
292
293 if i == 3 and j == 4:
294 para_title = cell.AddParagraph("Table title")
295 para_title.SetJustificationMode(Paragraph.e_text_justify_center)
296
297 nested_table = cell.AddTable()
298 nested_table.SetDefaultColumnWidth(33)
299 nested_table.SetDefaultRowHeight(5)
300 nested_table.SetBorder(0.5, 0, 0, 0)
301
302 for nested_row_index in range(3):
303 nested_row = nested_table.AddTableRow()
304 for nested_column_index in range(3):
305 para_str = str(nested_row_index) + "/" + str(nested_column_index)
306 nested_cell = nested_row.AddTableCell()
307 nested_cell.SetBackgroundColor(200, 200, 255)
308 nested_cell.SetBorder(0.1, 0, 255, 0)
309
310 new_para = nested_cell.AddParagraph(para_str)
311 new_para.SetJustificationMode(Paragraph.e_text_justify_right)
312 elif i == 2 and j == 2:
313 new_para = cell.AddParagraph("Cell " + str(j) + " x " + str(i) + "\n")
314 new_para.AddText("to be bold text 1\n")
315 new_para.AddText("still normal text\n")
316 new_para.AddText("to be bold text 2")
317 cell.AddParagraph("Second Paragraph")
318 else:
319 cell.AddParagraph("Cell " + str(j) + " x " + str(i))
320
321 # Walk the content tree and modify some text runs.
322 modify_content_tree(flowdoc.GetBody())
323
324 # Merge cells
325 merged_cell = new_table.GetTableCell(2, 0).MergeCellsRight(1)
326 merged_cell.SetHorizontalAlignment(TableCell.e_alignment_middle)
327
328 new_table.GetTableCell(0, 0).MergeCellsDown(1).SetVerticalAlignment(TableCell.e_alignment_center)
329
330 # Walk over the table and change the first cell in each row.
331 row_idx = 0
332 clr_row1 = [175, 240, 240]
333 clr_row2 = [250, 250, 175]
334
335 table_itr = new_table.GetContentNodeIterator()
336 while table_itr.HasNext():
337 row = table_itr.Current().AsTableRow()
338 if row is not None:
339 row_itr = row.GetContentNodeIterator()
340 while row_itr.HasNext():
341 cell = row_itr.Current().AsTableCell()
342 if cell is not None:
343 if row_idx % 2 != 0:
344 cell.SetBackgroundColor(clr_row1[0], clr_row1[1], clr_row1[2])
345 else:
346 cell.SetBackgroundColor(clr_row2[0], clr_row2[1], clr_row2[2])
347
348 cell_itr = cell.GetContentNodeIterator()
349 while cell_itr.HasNext():
350 element = cell_itr.Current();
351 para = element.AsParagraph()
352 if para is not None:
353 st = para.GetTextStyledElement()
354 st.SetTextColor(255, 0, 0)
355 st.SetFontSize(12)
356 else:
357 nested_table = element.AsTable()
358 if nested_table is not None:
359 nested_cell = nested_table.GetTableCell(1, 1)
360 nested_cell.SetBackgroundColor(255, 127, 127)
361 cell_itr.Next()
362 row_itr.Next()
363
364 row_idx += 1
365 table_itr.Next()
366
367 my_pdf = flowdoc.PaginateToPDF()
368 my_pdf.Save(output_path + output_name, SDFDoc.e_linearized)
369
370 except Exception as e:
371 print(str(e))
372 result = False
373
374 #-----------------------------------------------------------------------------------
375
376 if not result:
377 print("Tests FAILED!!!\n==========")
378 PDFNet.Terminate()
379 return
380 PDFNet.Terminate()
381 print("Tests successful.\n==========")
382
383if __name__ == '__main__':
384 main()
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6const { PDFNet } = require('@pdftron/pdfnet-node');
7const PDFTronLicense = require('../LicenseKey/LicenseKey');
8
9((exports) => {
10 'use strict';
11
12 exports.runDocumentCreationTest = async () => {
13 // Iterate over all text runs of the document and make every second run
14 // bold with smaller font size.
15 const modifyContentTree = async (node) => {
16 let bold = false;
17
18 const itr = await node.getContentNodeIterator();
19 while (await itr.hasNext()) {
20 const el = await itr.current();
21 itr.next();
22
23 const text_run = await el.asTextRun();
24 if (text_run !== null) {
25 if (bold) {
26 const styledElement = await text_run.getTextStyledElement();
27 styledElement.setBold(true);
28 styledElement.setFontSize(await styledElement.getFontSize() * 0.8);
29 }
30 bold = !bold;
31 continue;
32 }
33
34 const content_node = await el.asContentNode();
35 if (content_node !== null) {
36 await modifyContentTree(content_node);
37 continue;
38 }
39 }
40 };
41
42 const main = async () => {
43
44 const outputPath = '../TestFiles/Output/';
45 try {
46 const para_text =
47 "Lorem ipsum dolor " +
48 "sit amet, consectetur adipisicing elit, sed " +
49 "do eiusmod tempor incididunt ut labore " +
50 "et dolore magna aliqua. Ut enim ad " +
51 "minim veniam, quis nostrud exercitation " +
52 "ullamco laboris nisi ut aliquip ex ea " +
53 "commodo consequat. Duis aute irure " +
54 "dolor in reprehenderit in voluptate velit " +
55 "esse cillum dolore eu fugiat nulla pariatur. " +
56 "Excepteur sint occaecat cupidatat " +
57 "non proident, sunt in culpa qui officia " +
58 "deserunt mollit anim id est laborum.";
59
60 const flowdoc = await PDFNet.FlowDocument.create();
61 const para = await flowdoc.addParagraph();
62 const st_para = await para.getTextStyledElement();
63
64 st_para.setFontSize(24);
65 st_para.setTextColor(255, 0, 0);
66 para.addText('Start \tRed \tText\n');
67
68 para.addTabStop(150);
69 para.addTabStop(250);
70 st_para.setTextColor(0, 0, 255);
71 para.addText('Start \tBlue \tText\n');
72
73 let last_run = await para.addText('Start Green Text\n');
74
75 let start = true;
76 const para_itr = await para.getContentNodeIterator();
77 while (await para_itr.hasNext()) {
78 const el = await para_itr.current();
79 const run = await el.asTextRun();
80 if (run !== null) {
81 (await run.getTextStyledElement()).setFontSize(12);
82 if (start) {
83 // Restore red color.
84 start = false;
85 run.setText(await run.getText() + '(restored \tred \tcolor)\n');
86 (await run.getTextStyledElement()).setTextColor(255, 0, 0);
87 }
88 }
89 await para_itr.next();
90 }
91
92 const st_last = await last_run.getTextStyledElement();
93 st_last.setTextColor(0, 255, 0);
94 st_last.setItalic(true);
95 st_last.setFontSize(18);
96
97 (await para.getTextStyledElement()).setBold(true);
98 para.setBorder(0.2, 0, 127, 0);
99 st_last.setBold(false);
100
101 await flowdoc.addParagraphWithText('Simple list creation process. All elements are added in their natural order\n\n');
102
103 const list = await flowdoc.addList();
104 list.setNumberFormat(PDFNet.List.NumberFormat.e_upper_letter, ".", false);
105 list.setStartIndex(4);
106
107 const item = await list.addItem(); // creates "D."
108 item.addParagraphWithText('item 0[0]');
109 const px = await item.addParagraphWithText('item 0[1]');
110 const xx_para = await px.getTextStyledElement();
111 xx_para.setTextColor(255, 99, 71);
112 px.addText(' Some More Text!');
113
114 const item2 = await list.addItem(); // creates "E."
115 const item2List = await item2.addList();
116 item2List.setStartIndex(0);
117 item2List.setNumberFormat(PDFNet.List.NumberFormat.e_decimal, '', true);
118 (await item2List.addItem()).addParagraphWithText('item 1[0].0');
119 const pp = await (await item2List.addItem()).addParagraphWithText('item 1[0].1');
120 const sx_para = await pp.getTextStyledElement();
121 sx_para.setTextColor(0, 0, 255);
122 pp.addText(' Some More Text');
123 (await item2List.addItem()).addParagraphWithText('item 1[0].2');
124 const item2List1 = await (await item2List.addItem()).addList();
125 item2List1.setStartIndex(7);
126 item2List1.setNumberFormat(PDFNet.List.NumberFormat.e_upper_roman, ')', true);
127 (await item2List1.addItem()).addParagraphWithText('item 1[0].3.0');
128 (await item2List1.addItem()).addParagraphWithText('item 1[0].3.1');
129
130 const extraItem = await item2List1.addItem();
131 extraItem.addParagraphWithText('item 1[0].3.2[0]');
132 extraItem.addParagraphWithText('item 1[0].3.2[1]');
133 let fourth = await extraItem.addList();
134 fourth.setNumberFormat(PDFNet.List.NumberFormat.e_decimal, '', true);
135 (await fourth.addItem()).addParagraphWithText('Fourth Level');
136
137 fourth = await (await item2List1.addItem()).addList();
138 fourth.setNumberFormat(PDFNet.List.NumberFormat.e_lower_letter, '', true);
139 (await fourth.addItem()).addParagraphWithText('Fourth Level (again)');
140
141 item2.addParagraphWithText('item 1[1]');
142 const item2List2 = await item2.addList();
143 item2List2.setStartIndex(10);
144 item2List2.setNumberFormat(PDFNet.List.NumberFormat.e_lower_roman, ".", false);
145 (await item2List2.addItem()).addParagraphWithText('item 1[2].0');
146 (await item2List2.addItem()).addParagraphWithText('item 1[2].1');
147 (await item2List2.addItem()).addParagraphWithText('item 1[2].2');
148
149 const item3 = await list.addItem(); // creates "F."
150 item3.addParagraphWithText('item 2');
151
152 const item4 = await list.addItem(); // creates "G."
153 item4.addParagraphWithText('item 3');
154
155 const item5 = await list.addItem(); // creates "H."
156 item5.addParagraphWithText('item 4');
157
158 const body_itr = await (await flowdoc.getBody()).getContentNodeIterator();
159 while (await body_itr.hasNext()) {
160 const el = await body_itr.current();
161
162 const list = await el.asList();
163 if (list !== null && (await list.getIndentationLevel()) === 1) {
164 const p = await (await list.addItem()).addParagraphWithText('Item added during iteration');
165 const ps = await p.getTextStyledElement();
166 ps.setTextColor(0, 127, 0);
167 }
168
169 const list_item = await el.asListItem();
170 if (list_item !== null && (await list_item.getIndentationLevel()) === 2) {
171 const p = await list_item.addParagraphWithText('* Paragraph added during iteration');
172 const ps = await p.getTextStyledElement();
173 ps.setTextColor(0, 0, 255);
174 }
175 await body_itr.next();
176 }
177
178 flowdoc.addParagraphWithText('\f'); // page break
179
180 flowdoc.addParagraphWithText(
181 'Nonlinear list creation flow. Items are added randomly.' +
182 ' List body separated by a paragraph, does not belong to the list\n\n'
183 );
184
185 const list2 = await flowdoc.addList();
186 list2.setNumberFormat(PDFNet.List.NumberFormat.e_upper_letter, ".", false);
187 list2.setStartIndex(4);
188
189 const item6 = await list2.addItem(); // creates "D."
190 item6.addParagraphWithText('item 0[0]');
191 const px2 = await item6.addParagraphWithText('item 0[1]');
192 const xx_para2 = await px2.getTextStyledElement();
193 xx_para2.setTextColor(255, 99, 71);
194 px2.addText(' Some More Text!');
195 item6.addParagraphWithText('item 0[2]');
196 const px3 = await item6.addParagraphWithText('item 0[3]');
197 item6.addParagraphWithText('item 0[4]');
198 const xx_para3 = await px3.getTextStyledElement();
199 xx_para3.setTextColor(255, 99, 71);
200
201 const item7 = await list2.addItem(); // creates "E."
202 const item7List = await item7.addList();
203 item7List.setStartIndex(0);
204 item7List.setNumberFormat(PDFNet.List.NumberFormat.e_decimal, '', true);
205 (await item7List.addItem()).addParagraphWithText('item 1[0].0');
206 const pp2 = await (await item7List.addItem()).addParagraphWithText('item 1[0].1');
207 const sx_para2 = await pp2.getTextStyledElement();
208 sx_para2.setTextColor(0, 0, 255);
209 pp2.addText(' Some More Text');
210
211 const item8 = await list2.addItem(); // creates "F."
212 item8.addParagraphWithText('item 2');
213
214 (await item7List.addItem()).addParagraphWithText('item 1[0].2');
215
216 item7.addParagraphWithText('item 1[1]');
217 const item7List2 = await item7.addList();
218 item7List2.setStartIndex(10);
219 item7List2.setNumberFormat(PDFNet.List.NumberFormat.e_lower_roman, ".", false);
220 (await item7List2.addItem()).addParagraphWithText('item 1[2].0');
221 (await item7List2.addItem()).addParagraphWithText('item 1[2].1');
222 (await item7List2.addItem()).addParagraphWithText('item 1[2].2');
223
224 const item9 = await list2.addItem(); // creates "G."
225 item9.addParagraphWithText('item 3');
226
227 const item7List1 = await (await item7List.addItem()).addList();
228 item7List1.setStartIndex(7);
229 item7List1.setNumberFormat(PDFNet.List.NumberFormat.e_upper_roman, ')', true);
230 (await item7List1.addItem()).addParagraphWithText('item 1[0].3.0');
231
232 flowdoc.addParagraphWithText(
233 '---------------------------------- splitting paragraph ----------------------------------'
234 );
235
236 item7List1.continueList();
237
238 (await item7List1.addItem()).addParagraphWithText('item 1[0].3.1 (continued)');
239 const extraItem2 = await item7List1.addItem();
240 extraItem2.addParagraphWithText('item 1[0].3.2[0]');
241 extraItem2.addParagraphWithText('item 1[0].3.2[1]');
242 let fourth2 = await extraItem2.addList();
243 fourth2.setNumberFormat(PDFNet.List.NumberFormat.e_decimal, '', true);
244 (await fourth2.addItem()).addParagraphWithText('FOURTH LEVEL');
245
246 const item10 = await list2.addItem(); // creates "H."
247 item10.addParagraphWithText('item 4 (continued)');
248
249 flowdoc.addParagraphWithText(' ');
250
251 flowdoc.setDefaultMargins(72.0, 72.0, 144.0, 228.0);
252 flowdoc.setDefaultPageSize(650, 750);
253 flowdoc.addParagraphWithText(para_text);
254
255 const clr1 = [50, 50, 199];
256 const clr2 = [30, 199, 30];
257
258 for (let i = 0; i < 50; i++) {
259 const para = await flowdoc.addParagraph();
260 const style = await para.getTextStyledElement();
261 const point_size = (17 * i * i * i) % 13 + 5;
262 if (i % 2 === 0) {
263 style.setItalic(true);
264 style.setTextColor(clr1[0], clr1[1], clr1[2]);
265 style.setBackgroundColor(200, 200, 200);
266 para.setSpaceBefore(20);
267 para.setStartIndent(20);
268 para.setJustificationMode(PDFNet.Paragraph.TextJustification.e_text_justify_left);
269 } else {
270 style.setTextColor(clr2[0], clr2[1], clr2[2]);
271 para.setSpaceBefore(50);
272 para.setEndIndent(20);
273 para.setJustificationMode(PDFNet.Paragraph.TextJustification.e_text_justify_right);
274 }
275
276 para.addText(para_text);
277 para.addText(' ' + para_text);
278 style.setFontSize(point_size);
279 }
280
281 // Table creation
282 const new_table = await flowdoc.addTable();
283 new_table.setDefaultColumnWidth(100);
284 new_table.setDefaultRowHeight(15);
285
286 for (let i = 0; i < 4; i++) {
287 const row = await new_table.addTableRow();
288 row.setRowHeight(await new_table.getDefaultRowHeight() + i * 5);
289 for (let j = 0; j < 5; j++) {
290 const cell = await row.addTableCell();
291 cell.setBorder(0.5, 255, 0, 0);
292
293 if (i === 3) {
294 if (j % 2 !== 0) {
295 cell.setVerticalAlignment(
296 PDFNet.TableCell.AlignmentVertical.e_alignment_center);
297 } else {
298 cell.setVerticalAlignment(
299 PDFNet.TableCell.AlignmentVertical.e_alignment_bottom);
300 }
301 }
302
303 if (i === 3 && j === 4) {
304 const para_title = await cell.addParagraphWithText('Table title');
305 para_title.setJustificationMode(
306 PDFNet.Paragraph.TextJustification.e_text_justify_center);
307
308 const nested_table = await cell.addTable();
309 nested_table.setDefaultColumnWidth(33);
310 nested_table.setDefaultRowHeight(5);
311 nested_table.setBorder(0.5, 0, 0, 0);
312
313 for (let nested_row_index = 0; nested_row_index < 3; nested_row_index++) {
314 const nested_row = await nested_table.addTableRow();
315 for (let nested_column_index = 0; nested_column_index < 3; nested_column_index++) {
316 const para_str = `${nested_row_index}/${nested_column_index}`;
317 const nested_cell = await nested_row.addTableCell();
318 nested_cell.setBackgroundColor(200, 200, 255);
319 nested_cell.setBorder(0.1, 0, 255, 0);
320
321 const new_para = await nested_cell.addParagraphWithText(para_str);
322 new_para.setJustificationMode(
323 PDFNet.Paragraph.TextJustification.e_text_justify_right);
324 }
325 }
326 } else if (i === 2 && j === 2) {
327 const new_para = await cell.addParagraphWithText(`Cell ${j} x ${i}\n`);
328 new_para.addText('to be bold text 1\n');
329 new_para.addText('still normal text\n');
330 new_para.addText('to be bold text 2');
331 cell.addParagraphWithText('Second Paragraph');
332 } else {
333 cell.addParagraphWithText(`Cell ${j} x ${i}`);
334 }
335 }
336 }
337
338 // Walk the content tree and modify some text runs.
339 await modifyContentTree(await flowdoc.getBody());
340
341 // Merge cells
342 const merged_cell = await (await new_table.getTableCell(2, 0)).mergeCellsRight(1);
343 merged_cell.setHorizontalAlignment(PDFNet.TableCell.AlignmentHorizontal.e_alignment_middle);
344
345 (await (await new_table.getTableCell(0, 0)).mergeCellsDown(1)).setVerticalAlignment(
346 PDFNet.TableCell.AlignmentVertical.e_alignment_center
347 );
348
349 // Walk over the table and change the first cell in each row.
350 let row_idx = 0;
351 const clr_row1 = [175, 240, 240];
352 const clr_row2 = [250, 250, 175];
353
354 const table_itr = await new_table.getContentNodeIterator();
355 while (await table_itr.hasNext()) {
356 const row = await (await table_itr.current()).asTableRow();
357 if (row !== null) {
358 const row_itr = await row.getContentNodeIterator();
359 while (await row_itr.hasNext()) {
360 const cell = await (await row_itr.current()).asTableCell();
361 if (cell !== null) {
362 if (row_idx % 2 !== 0) {
363 cell.setBackgroundColor(clr_row1[0], clr_row1[1], clr_row1[2]);
364 } else {
365 cell.setBackgroundColor(clr_row2[0], clr_row2[1], clr_row2[2]);
366 }
367
368 const cell_itr = await cell.getContentNodeIterator();
369 while (await cell_itr.hasNext()) {
370 const element = await cell_itr.current();
371 const para = await element.asParagraph();
372 if (para !== null) {
373 const st = await para.getTextStyledElement();
374 st.setTextColor(255, 0, 0);
375 st.setFontSize(12);
376 } else {
377 const nested_table = await element.asTable();
378 if (nested_table !== null) {
379 const nested_cell = await nested_table.getTableCell(1, 1);
380 nested_cell.setBackgroundColor(255, 127, 127);
381 }
382 }
383 await cell_itr.next();
384 }
385 }
386 await row_itr.next();
387 }
388 }
389
390 row_idx += 1;
391 await table_itr.next();
392 }
393
394 const my_pdf = await flowdoc.paginateToPDF();
395 my_pdf.save(outputPath + "created_doc.pdf", PDFNet.SDFDoc.SaveOptions.e_linearized);
396 console.log('Done.');
397 } catch (err) {
398 console.log(err);
399 }
400 };
401
402 PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function (error) {
403 console.log('Error: ' + JSON.stringify(error));
404 }).then(function () { return PDFNet.shutdown(); });
405 };
406 exports.runDocumentCreationTest();
407})(exports);
408// eslint-disable-next-line spaced-comment
409//# sourceURL=DocumentCreationTest.js
1<?php
2//---------------------------------------------------------------------------------------
3// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
4// Consult LICENSE.txt regarding license information.
5//---------------------------------------------------------------------------------------
6if(file_exists("../../../PDFNetC/Lib/PDFNetPHP.php"))
7include("../../../PDFNetC/Lib/PDFNetPHP.php");
8include("../../LicenseKey/PHP/LicenseKey.php");
9
10function modify_content_tree($node)
11{
12 $bold = false;
13 $itr = $node->GetContentNodeIterator();
14 while ($itr->HasNext())
15 {
16 $el = $itr->Current();
17 $itr->Next();
18
19 $text_run = $el->AsTextRun();
20 if ($text_run !== null)
21 {
22 if ($bold)
23 {
24 $text_run->GetTextStyledElement()->SetBold(true);
25 $text_run->GetTextStyledElement()->SetFontSize(
26 $text_run->GetTextStyledElement()->GetFontSize() * 0.8
27 );
28 }
29 $bold = !$bold;
30 continue;
31 }
32
33 $content_node = $el->AsContentNode();
34 if ($content_node !== null)
35 {
36 modify_content_tree($content_node);
37 continue;
38 }
39 }
40}
41
42function main()
43{
44 // Relative path to the folder containing the test files.
45 $inputPath = getcwd()."/../../TestFiles/";
46 $outputPath = $inputPath."Output/";
47 $outputName = "created_doc.pdf";
48
49 // The first step in every application using PDFNet is to initialize the
50 // library. The library is usually initialized only once, but calling
51 // Initialize() multiple times is also fine.
52 global $LicenseKey;
53 PDFNet::Initialize($LicenseKey);
54 // Wait for fonts to be loaded if they haven't already.
55 // This is done because PHP can run into errors when shutting down if font loading is still in progress.
56 PDFNet::GetSystemFontList();
57
58 //-----------------------------------------------------------------------------------
59
60 PDFNet::AddResourceSearchPath("../../../PDFNetC/Lib/");
61
62 $para_text = "Lorem ipsum dolor " .
63 "sit amet, consectetur adipisicing elit, sed " .
64 "do eiusmod tempor incididunt ut labore " .
65 "et dolore magna aliqua. Ut enim ad " .
66 "minim veniam, quis nostrud exercitation " .
67 "ullamco laboris nisi ut aliquip ex ea " .
68 "commodo consequat. Duis aute irure " .
69 "dolor in reprehenderit in voluptate velit " .
70 "esse cillum dolore eu fugiat nulla pariatur. " .
71 "Excepteur sint occaecat cupidatat " .
72 "non proident, sunt in culpa qui officia " .
73 "deserunt mollit anim id est laborum.";
74
75 $result = true;
76
77 try {
78 $flowdoc = new FlowDocument();
79 $para = $flowdoc->AddParagraph();
80 $style = $para->GetTextStyledElement();
81
82 $style->SetFontSize(24);
83 $style->SetTextColor(255, 0, 0);
84 $para->AddText("Start Red Text\n");
85
86 $para->AddText("Start \tRed \tText\n");
87
88 $para->AddTabStop(150);
89 $para->AddTabStop(250);
90 $style->SetTextColor(0, 0, 255);
91 $para->AddText("Start \tBlue \tText\n");
92
93 $last_run = $para->AddText("Start Green Text\n");
94
95 $start = true;
96 $itr = $para->GetContentNodeIterator();
97 while ($itr->HasNext()) {
98 $el = $itr->Current();
99 $run = $el->AsTextRun();
100 if ($run !== null) {
101 $run->GetTextStyledElement()->SetFontSize(12);
102 if ($start) {
103 // Restore red color.
104 $start = false;
105 $run->SetText($run->GetText() . "(restored red color)\n");
106 $run->GetTextStyledElement()->SetTextColor(255, 0, 0);
107 }
108 }
109 $itr->Next();
110 }
111
112 $st_last = $last_run->GetTextStyledElement();
113 $st_last->SetTextColor(0, 255, 0);
114 $st_last->SetItalic(true);
115 $st_last->SetFontSize(18);
116
117 $para->GetTextStyledElement()->SetBold(true);
118 $para->SetBorder(0.2, 0, 127, 0);
119 $st_last->SetBold(false);
120
121 $flowdoc->AddParagraph("Simple list creation process. All elements are added in their natural order\n\n");
122
123 $list = $flowdoc->AddList();
124 $list->SetNumberFormat(ListContainer::e_upper_letter);
125 $list->SetStartIndex(4);
126
127 $item = $list->AddItem(); // creates "D."
128 $item->AddParagraph("item 0[0]");
129 $px = $item->AddParagraph("item 0[1]");
130 $xx_para = $px->GetTextStyledElement();
131 $xx_para->SetTextColor(255, 99, 71);
132 $px->AddText(" Some More Text!");
133
134 $item2 = $list->AddItem(); // creates "E."
135 $item2List = $item2->AddList();
136 $item2List->SetStartIndex(0);
137 $item2List->SetNumberFormat(ListContainer::e_decimal, "", true);
138 $item2List->AddItem()->AddParagraph("item 1[0].0");
139 $pp = $item2List->AddItem()->AddParagraph("item 1[0].1");
140 $sx_para = $pp->GetTextStyledElement();
141 $sx_para->SetTextColor(0, 0, 255);
142 $pp->AddText(" Some More Text");
143 $item2List->AddItem()->AddParagraph("item 1[0].2");
144 $item2List1 = $item2List->AddItem()->AddList();
145 $item2List1->SetStartIndex(7);
146 $item2List1->SetNumberFormat(ListContainer::e_upper_roman, ")", true);
147 $item2List1->AddItem()->AddParagraph("item 1[0].3.0");
148 $item2List1->AddItem()->AddParagraph("item 1[0].3.1");
149 $extraItem = $item2List1->AddItem();
150 $extraItem->AddParagraph("item 1[0].3.2[0]");
151 $extraItem->AddParagraph("item 1[0].3.2[1]");
152 $fourth = $extraItem->AddList();
153 $fourth->SetNumberFormat(ListContainer::e_decimal, "", true);
154 $fourth->AddItem()->AddParagraph("Fourth Level");
155
156 $fourth = $item2List1->AddItem()->AddList();
157 $fourth->SetNumberFormat(ListContainer::e_lower_letter, "", true);
158 $fourth->AddItem()->AddParagraph("Fourth Level (again)");
159
160 $item2->AddParagraph("item 1[1]");
161 $item2List2 = $item2->AddList();
162 $item2List2->SetStartIndex(10);
163 $item2List2->SetNumberFormat(ListContainer::e_lower_roman);
164 $item2List2->AddItem()->AddParagraph("item 1[2].0");
165 $item2List2->AddItem()->AddParagraph("item 1[2].1");
166 $item2List2->AddItem()->AddParagraph("item 1[2].2");
167 $item3 = $list->AddItem(); // creates "F."
168 $item3->AddParagraph("item 2");
169
170 $item4 = $list->AddItem(); // creates "G."
171 $item4->AddParagraph("item 3");
172
173 $item5 = $list->AddItem(); // creates "H."
174 $item5->AddParagraph("item 4");
175
176 $itr = $flowdoc->GetBody()->GetContentNodeIterator();
177 while ($itr->HasNext()) {
178 $el = $itr->Current();
179 $list = $el->AsList();
180
181 if ($list !== null) {
182 if ($list->GetIndentationLevel() == 1) {
183 $p = $list->AddItem()->AddParagraph("Item added during iteration");
184 $ps = $p->GetTextStyledElement();
185 $ps->SetTextColor(0, 127, 0);
186 }
187 }
188
189 $list_item = $el->AsListItem();
190 if ($list_item !== null) {
191 if ($list_item->GetIndentationLevel() == 2) {
192 $p = $list_item->AddParagraph("* Paragraph added during iteration");
193 $ps = $p->GetTextStyledElement();
194 $ps->SetTextColor(0, 0, 255);
195 }
196 }
197 $itr->Next();
198 }
199
200 $flowdoc->AddParagraph("\f"); // page break
201
202 $flowdoc->AddParagraph("Nonlinear list creation flow. Items are added randomly."
203 . " List body separated by a paragraph, does not belong to the list\n\n");
204
205 $list = $flowdoc->AddList();
206 $list->SetNumberFormat(ListContainer::e_upper_letter);
207 $list->SetStartIndex(4);
208
209 $item = $list->AddItem(); // creates "D."
210 $item->AddParagraph("item 0[0]");
211 $px = $item->AddParagraph("item 0[1]");
212 $xx_para = $px->GetTextStyledElement();
213 $xx_para->SetTextColor(255, 99, 71);
214 $px->AddText(" Some More Text!");
215 $item->AddParagraph("item 0[2]");
216 $px = $item->AddParagraph("item 0[3]");
217 $item->AddParagraph("item 0[4]");
218 $xx_para = $px->GetTextStyledElement();
219 $xx_para->SetTextColor(255, 99, 71);
220
221 $item2 = $list->AddItem(); // creates "E."
222 $item2List = $item2->AddList();
223 $item2List->SetStartIndex(0);
224 $item2List->SetNumberFormat(ListContainer::e_decimal, "", true);
225 $item2List->AddItem()->AddParagraph("item 1[0].0");
226 $pp = $item2List->AddItem()->AddParagraph("item 1[0].1");
227 $sx_para = $pp->GetTextStyledElement();
228 $sx_para->SetTextColor(0, 0, 255);
229 $pp->AddText(" Some More Text");
230
231 $item3 = $list->AddItem(); // creates "F."
232 $item3->AddParagraph("item 2");
233
234 $item2List->AddItem()->AddParagraph("item 1[0].2");
235
236 $item2->AddParagraph("item 1[1]");
237 $item2List2 = $item2->AddList();
238 $item2List2->SetStartIndex(10);
239 $item2List2->SetNumberFormat(ListContainer::e_lower_roman);
240 $item2List2->AddItem()->AddParagraph("item 1[2].0");
241 $item2List2->AddItem()->AddParagraph("item 1[2].1");
242 $item2List2->AddItem()->AddParagraph("item 1[2].2");
243
244 $item4 = $list->AddItem(); // creates "G."
245 $item4->AddParagraph("item 3");
246
247 $item2List1 = $item2List->AddItem()->AddList();
248 $item2List1->SetStartIndex(7);
249 $item2List1->SetNumberFormat(ListContainer::e_upper_roman, ")", true);
250 $item2List1->AddItem()->AddParagraph("item 1[0].3.0");
251
252 $flowdoc->AddParagraph("---------------------------------- splitting paragraph ----------------------------------");
253
254 $item2List1->ContinueList();
255
256 $item2List1->AddItem()->AddParagraph("item 1[0].3.1 (continued)");
257 $extraItem = $item2List1->AddItem();
258 $extraItem->AddParagraph("item 1[0].3.2[0]");
259 $extraItem->AddParagraph("item 1[0].3.2[1]");
260 $fourth = $extraItem->AddList();
261 $fourth->SetNumberFormat(ListContainer::e_decimal, "", true);
262 $fourth->AddItem()->AddParagraph("FOURTH LEVEL");
263
264 $item5 = $list->AddItem(); // creates "H."
265 $item5->AddParagraph("item 4 (continued)");
266
267 $flowdoc->AddParagraph(" ");
268
269 $flowdoc->SetDefaultMargins(72.0, 72.0, 144.0, 228.0);
270 $flowdoc->SetDefaultPageSize(650, 750);
271 $flowdoc->AddParagraph($para_text);
272
273 $clr1 = [50, 50, 199];
274 $clr2 = [30, 199, 30];
275
276 for ($i = 0; $i < 50; $i++) {
277 $para = $flowdoc->AddParagraph();
278 $style = $para->GetTextStyledElement();
279 $point_size = ($i * $i * $i) % 13 + 5;
280 if ($i % 2 == 0) {
281 $style->SetItalic(true);
282 $style->SetTextColor($clr1[0], $clr1[1], $clr1[2]);
283 $style->SetBackgroundColor(200, 200, 200);
284 $para->SetSpaceBefore(20);
285 $para->SetStartIndent(20);
286 $para->SetJustificationMode(Paragraph::e_text_justify_left);
287 } else {
288 $style->SetTextColor($clr2[0], $clr2[1], $clr2[2]);
289 $para->SetSpaceBefore(50);
290 $para->SetEndIndent(20);
291 $para->SetJustificationMode(Paragraph::e_text_justify_right);
292 }
293
294 $para->AddText($para_text);
295 $para->AddText(" " . $para_text);
296 $style->SetFontSize($point_size);
297 }
298
299 // Table creation
300 $new_table = $flowdoc->AddTable();
301 $new_table->SetDefaultColumnWidth(100);
302 $new_table->SetDefaultRowHeight(15);
303
304 for ($i = 0; $i < 4; $i++) {
305 $row = $new_table->AddTableRow();
306 $row->SetRowHeight($new_table->GetDefaultRowHeight() + $i * 5);
307 for ($j = 0; $j < 5; $j++) {
308 $cell = $row->AddTableCell();
309 $cell->SetBorder(0.5, 255, 0, 0);
310
311 if ($i == 3) {
312 if ($j % 2 != 0) {
313 $cell->SetVerticalAlignment(TableCell::e_alignment_center);
314 } else {
315 $cell->SetVerticalAlignment(TableCell::e_alignment_bottom);
316 }
317 }
318
319 if ($i == 3 && $j == 4) {
320 $para_title = $cell->AddParagraph("Table title");
321 $para_title->SetJustificationMode(Paragraph::e_text_justify_center);
322
323 $nested_table = $cell->AddTable();
324 $nested_table->SetDefaultColumnWidth(33);
325 $nested_table->SetDefaultRowHeight(5);
326 $nested_table->SetBorder(0.5, 0, 0, 0);
327
328 for ($nested_row_index = 0; $nested_row_index < 3; $nested_row_index++) {
329 $nested_row = $nested_table->AddTableRow();
330 for ($nested_column_index = 0; $nested_column_index < 3; $nested_column_index++) {
331 $para_str = "{$nested_row_index}/{$nested_column_index}";
332 $nested_cell = $nested_row->AddTableCell();
333 $nested_cell->SetBackgroundColor(200, 200, 255);
334 $nested_cell->SetBorder(0.1, 0, 255, 0);
335
336 $new_para = $nested_cell->AddParagraph($para_str);
337 $new_para->SetJustificationMode(Paragraph::e_text_justify_right);
338 }
339 }
340 } elseif ($i == 2 && $j == 2) {
341 $new_para = $cell->AddParagraph("Cell " . $j . " x " . $i . "\n");
342 $new_para->AddText("to be bold text 1\n");
343 $new_para->AddText("still normal text\n");
344 $new_para->AddText("to be bold text 2");
345 $cell->AddParagraph("Second Paragraph");
346 } else {
347 $cell->AddParagraph("Cell " . $j . " x " . $i);
348 }
349 }
350 }
351
352 // Walk the content tree and modify some text runs.
353 modify_content_tree($flowdoc->GetBody());
354
355 // Merge cells
356 $merged_cell = $new_table->GetTableCell(2, 0)->MergeCellsRight(1);
357 $merged_cell->SetHorizontalAlignment(TableCell::e_alignment_middle);
358
359 $new_table->GetTableCell(0, 0)->MergeCellsDown(1)->SetVerticalAlignment(TableCell::e_alignment_center);
360
361 // Walk over the table and change the first cell in each row.
362 $row_idx = 0;
363 $clr_row1 = [175, 240, 240];
364 $clr_row2 = [250, 250, 175];
365
366 $table_itr = $new_table->GetContentNodeIterator();
367 while ($table_itr->HasNext()) {
368 $row = $table_itr->Current()->AsTableRow();
369 if ($row !== null) {
370 $row_itr = $row->GetContentNodeIterator();
371 while ($row_itr->HasNext()) {
372 $cell = $row_itr->Current()->AsTableCell();
373 if ($cell !== null) {
374 if ($row_idx % 2 != 0) {
375 $cell->SetBackgroundColor($clr_row1[0], $clr_row1[1], $clr_row1[2]);
376 } else {
377 $cell->SetBackgroundColor($clr_row2[0], $clr_row2[1], $clr_row2[2]);
378 }
379
380 $cell_itr = $cell->GetContentNodeIterator();
381 while ($cell_itr->HasNext()) {
382 $element = $cell_itr->Current();
383 $para = $element->AsParagraph();
384 if ($para !== null) {
385 $st = $para->GetTextStyledElement();
386 $st->SetTextColor(255, 0, 0);
387 $st->SetFontSize(12);
388 } else {
389 $nested_table = $element->AsTable();
390 if ($nested_table !== null) {
391 $nested_cell = $nested_table->GetTableCell(1, 1);
392 $nested_cell->SetBackgroundColor(255, 127, 127);
393 }
394 }
395 $cell_itr->Next();
396 }
397 }
398 $row_itr->Next();
399 }
400 }
401 $row_idx++;
402 $table_itr->Next();
403 }
404
405 $my_pdf = $flowdoc->PaginateToPDF();
406 $my_pdf->Save($outputPath . $outputName, SDFDoc::e_linearized);
407 } catch (Exception $e) {
408 echo $e->getMessage() . "\n";
409 $result = false;
410 }
411
412 //-----------------------------------------------------------------------------------
413
414 PDFNet::Terminate();
415 echo(nl2br("Done.\n"));
416}
417
418main();
419?>
1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6require '../../../PDFNetC/Lib/PDFNetRuby'
7include PDFNetRuby
8require '../../LicenseKey/RUBY/LicenseKey'
9
10$stdout.sync = true
11
12# Relative path to the folder containing the test files.
13$inputPath = "../../TestFiles/"
14$outputPath = "../../TestFiles/Output/"
15
16def modify_content_tree(node)
17 bold = false
18
19 itr = node.GetContentNodeIterator()
20 while itr.HasNext()
21 el = itr.Current()
22 itr.Next()
23
24 text_run = el.AsTextRun()
25 if !text_run.nil?
26 if bold
27 text_run.GetTextStyledElement().SetBold(true)
28 text_run.GetTextStyledElement().SetFontSize(
29 text_run.GetTextStyledElement().GetFontSize() * 0.8)
30 end
31 bold = !bold
32 next
33 end
34
35 content_node = el.AsContentNode()
36 if !content_node.nil?
37 modify_content_tree(content_node)
38 next
39 end
40 end
41end
42
43
44def main()
45 # The first step in every application using PDFNet is to initialize the
46 # library. The library is usually initialized only once, but calling
47 # Initialize() multiple times is also fine.
48 PDFNet.Initialize(PDFTronLicense.Key)
49
50 PDFNet.AddResourceSearchPath("../../../PDFNetC/Lib/")
51
52 para_text = "Lorem ipsum dolor " \
53 "sit amet, consectetur adipisicing elit, sed " \
54 "do eiusmod tempor incididunt ut labore " \
55 "et dolore magna aliqua. Ut enim ad " \
56 "minim veniam, quis nostrud exercitation " \
57 "ullamco laboris nisi ut aliquip ex ea " \
58 "commodo consequat. Duis aute irure " \
59 "dolor in reprehenderit in voluptate velit " \
60 "esse cillum dolore eu fugiat nulla pariatur. " \
61 "Excepteur sint occaecat cupidatat " \
62 "non proident, sunt in culpa qui officia " \
63 "deserunt mollit anim id est laborum."
64
65 result = true
66
67 begin
68 flowdoc = FlowDocument.new()
69 para = flowdoc.AddParagraph()
70 st_para = para.GetTextStyledElement()
71
72 st_para.SetFontSize(24)
73 st_para.SetTextColor(255, 0, 0)
74 para.AddText("Start \tRed \tText\n")
75
76 para.AddTabStop(150)
77 para.AddTabStop(250)
78 st_para.SetTextColor(0, 0, 255)
79 para.AddText("Start \tBlue \tText\n")
80
81 last_run = para.AddText("Start Green Text\n")
82
83 start = true
84 itr = para.GetContentNodeIterator()
85 while itr.HasNext()
86 el = itr.Current()
87 run = el.AsTextRun()
88 if !run.nil?
89 run.GetTextStyledElement().SetFontSize(12)
90 if start
91 # Restore red color.
92 start = false
93 run.SetText(run.GetText() + "(restored \tred \tcolor)\n")
94 run.GetTextStyledElement().SetTextColor(255, 0, 0)
95 end
96 end
97 itr.Next()
98 end
99
100 st_last = last_run.GetTextStyledElement()
101 st_last.SetTextColor(0, 255, 0)
102 st_last.SetItalic(true)
103 st_last.SetFontSize(18)
104
105 para.GetTextStyledElement().SetBold(true)
106 para.SetBorder(0.2, 0, 127, 0)
107 st_last.SetBold(false)
108
109 flowdoc.AddParagraph("Simple list creation process. All elements are added in their natural order\n\n")
110
111 list = flowdoc.AddList()
112 list.SetNumberFormat(List::E_upper_letter)
113 list.SetStartIndex(4)
114
115 item = list.AddItem() # creates "D."
116 item.AddParagraph("item 0[0]")
117 px = item.AddParagraph("item 0[1]")
118 xx_para = px.GetTextStyledElement()
119 xx_para.SetTextColor(255, 99, 71)
120 px.AddText(" Some More Text!")
121
122 item2 = list.AddItem() # creates "E."
123 item2List = item2.AddList()
124 item2List.SetStartIndex(0)
125 item2List.SetNumberFormat(List::E_decimal, "", true)
126 item2List.AddItem().AddParagraph("item 1[0].0")
127 pp = item2List.AddItem().AddParagraph("item 1[0].1")
128 sx_para = pp.GetTextStyledElement()
129 sx_para.SetTextColor(0, 0, 255)
130 pp.AddText(" Some More Text")
131 item2List.AddItem().AddParagraph("item 1[0].2")
132 item2List1 = item2List.AddItem().AddList()
133 item2List1.SetStartIndex(7)
134 item2List1.SetNumberFormat(List::E_upper_roman, ")", true)
135 item2List1.AddItem().AddParagraph("item 1[0].3.0")
136 item2List1.AddItem().AddParagraph("item 1[0].3.1")
137
138 extraItem = item2List1.AddItem()
139 extraItem.AddParagraph("item 1[0].3.2[0]")
140 extraItem.AddParagraph("item 1[0].3.2[1]")
141 fourth = extraItem.AddList()
142 fourth.SetNumberFormat(List::E_decimal, "", true)
143 fourth.AddItem().AddParagraph("Fourth Level")
144
145 fourth = item2List1.AddItem().AddList()
146 fourth.SetNumberFormat(List::E_lower_letter, "", true)
147 fourth.AddItem().AddParagraph("Fourth Level (again)")
148
149 item2.AddParagraph("item 1[1]")
150 item2List2 = item2.AddList()
151 item2List2.SetStartIndex(10)
152 item2List2.SetNumberFormat(List::E_lower_roman)
153 item2List2.AddItem().AddParagraph("item 1[2].0")
154 item2List2.AddItem().AddParagraph("item 1[2].1")
155 item2List2.AddItem().AddParagraph("item 1[2].2")
156
157 item3 = list.AddItem() # creates "F."
158 item3.AddParagraph("item 2")
159
160 item4 = list.AddItem() # creates "G."
161 item4.AddParagraph("item 3")
162
163 item5 = list.AddItem() # creates "H."
164 item5.AddParagraph("item 4")
165
166 itr = flowdoc.GetBody().GetContentNodeIterator()
167 while itr.HasNext()
168 el = itr.Current()
169
170 list = el.AsList()
171 if !list.nil?
172 if list.GetIndentationLevel() == 1
173 p = list.AddItem().AddParagraph("Item added during iteration")
174 ps = p.GetTextStyledElement()
175 ps.SetTextColor(0, 127, 0)
176 end
177 end
178
179 list_item = el.AsListItem()
180 if !list_item.nil?
181 if list_item.GetIndentationLevel() == 2
182 p = list_item.AddParagraph("* Paragraph added during iteration")
183 ps = p.GetTextStyledElement()
184 ps.SetTextColor(0, 0, 255)
185 end
186 end
187
188 itr.Next()
189 end
190
191 flowdoc.AddParagraph("\f") # page break
192
193 flowdoc.AddParagraph("Nonlinear list creation flow. Items are added randomly." +
194 " List body separated by a paragraph, does not belong to the list\n\n")
195
196 list = flowdoc.AddList()
197 list.SetNumberFormat(List::E_upper_letter)
198 list.SetStartIndex(4)
199
200 item = list.AddItem() # creates "D."
201 item.AddParagraph("item 0[0]")
202 px = item.AddParagraph("item 0[1]")
203 xx_para = px.GetTextStyledElement()
204 xx_para.SetTextColor(255, 99, 71)
205 px.AddText(" Some More Text!")
206 item.AddParagraph("item 0[2]")
207 px = item.AddParagraph("item 0[3]")
208 item.AddParagraph("item 0[4]")
209 xx_para = px.GetTextStyledElement()
210 xx_para.SetTextColor(255, 99, 71)
211
212 item2 = list.AddItem() # creates "E."
213 item2List = item2.AddList()
214 item2List.SetStartIndex(0)
215 item2List.SetNumberFormat(List::E_decimal, "", true)
216 item2List.AddItem().AddParagraph("item 1[0].0")
217 pp = item2List.AddItem().AddParagraph("item 1[0].1")
218 sx_para = pp.GetTextStyledElement()
219 sx_para.SetTextColor(0, 0, 255)
220 pp.AddText(" Some More Text")
221
222 item3 = list.AddItem() # creates "F."
223 item3.AddParagraph("item 2")
224
225 item2List.AddItem().AddParagraph("item 1[0].2")
226
227 item2.AddParagraph("item 1[1]")
228 item2List2 = item2.AddList()
229 item2List2.SetStartIndex(10)
230 item2List2.SetNumberFormat(List::E_lower_roman)
231 item2List2.AddItem().AddParagraph("item 1[2].0")
232 item2List2.AddItem().AddParagraph("item 1[2].1")
233 item2List2.AddItem().AddParagraph("item 1[2].2")
234
235 item4 = list.AddItem() # creates "G."
236 item4.AddParagraph("item 3")
237
238 item2List1 = item2List.AddItem().AddList()
239 item2List1.SetStartIndex(7)
240 item2List1.SetNumberFormat(List::E_upper_roman, ")", true)
241 item2List1.AddItem().AddParagraph("item 1[0].3.0")
242
243 flowdoc.AddParagraph("---------------------------------- splitting paragraph ----------------------------------")
244
245 item2List1.ContinueList()
246
247 item2List1.AddItem().AddParagraph("item 1[0].3.1 (continued)")
248 extraItem = item2List1.AddItem()
249 extraItem.AddParagraph("item 1[0].3.2[0]")
250 extraItem.AddParagraph("item 1[0].3.2[1]")
251 fourth = extraItem.AddList()
252 fourth.SetNumberFormat(List::E_decimal, "", true)
253 fourth.AddItem().AddParagraph("FOURTH LEVEL")
254
255 item5 = list.AddItem() # creates "H."
256 item5.AddParagraph("item 4 (continued)")
257
258 flowdoc.AddParagraph(" ")
259
260 flowdoc.SetDefaultMargins(72.0, 72.0, 144.0, 228.0)
261 flowdoc.SetDefaultPageSize(650, 750)
262 flowdoc.AddParagraph(para_text)
263
264 clr1 = [50, 50, 199]
265 clr2 = [30, 199, 30]
266
267 50.times do |i|
268 para = flowdoc.AddParagraph()
269 style = para.GetTextStyledElement()
270 point_size = (17 * i * i * i) % 13 + 5
271 if i % 2 == 0
272 style.SetItalic(true)
273 style.SetTextColor(clr1[0], clr1[1], clr1[2])
274 style.SetBackgroundColor(200, 200, 200);
275 para.SetSpaceBefore(20)
276 para.SetStartIndent(20)
277 para.SetJustificationMode(Paragraph::E_text_justify_left)
278 else
279 style.SetTextColor(clr2[0], clr2[1], clr2[2])
280 para.SetSpaceBefore(50)
281 para.SetEndIndent(20)
282 para.SetJustificationMode(Paragraph::E_text_justify_right)
283 end
284
285 para.AddText(para_text)
286 para.AddText(" " + para_text)
287 style.SetFontSize(point_size)
288 end
289
290 # Table creation
291 new_table = flowdoc.AddTable()
292 new_table.SetDefaultColumnWidth(100)
293 new_table.SetDefaultRowHeight(15)
294
295 4.times do |i|
296 row = new_table.AddTableRow()
297 row.SetRowHeight(new_table.GetDefaultRowHeight() + i * 5)
298 5.times do |j|
299 cell = row.AddTableCell()
300 cell.SetBorder(0.5, 255, 0, 0)
301
302 if i == 3
303 if j % 2 != 0
304 cell.SetVerticalAlignment(TableCell::E_alignment_center)
305 else
306 cell.SetVerticalAlignment(TableCell::E_alignment_bottom)
307 end
308 end
309
310 if i == 3 && j == 4
311 para_title = cell.AddParagraph("Table title")
312 para_title.SetJustificationMode(Paragraph::E_text_justify_center)
313
314 nested_table = cell.AddTable()
315 nested_table.SetDefaultColumnWidth(33)
316 nested_table.SetDefaultRowHeight(5)
317 nested_table.SetBorder(0.5, 0, 0, 0)
318
319 3.times do |nested_row_index|
320 nested_row = nested_table.AddTableRow()
321 3.times do |nested_column_index|
322 para_str = "#{nested_row_index}/#{nested_column_index}"
323 nested_cell = nested_row.AddTableCell()
324 nested_cell.SetBackgroundColor(200, 200, 255)
325 nested_cell.SetBorder(0.1, 0, 255, 0)
326
327 new_para = nested_cell.AddParagraph(para_str)
328 new_para.SetJustificationMode(Paragraph::E_text_justify_right)
329 end
330 end
331 elsif i == 2 && j == 2
332 new_para = cell.AddParagraph("Cell #{j} x #{i}\n")
333 new_para.AddText("to be bold text 1\n")
334 new_para.AddText("still normal text\n")
335 new_para.AddText("to be bold text 2")
336 cell.AddParagraph("Second Paragraph")
337 else
338 cell.AddParagraph("Cell #{j} x #{i}")
339 end
340 end
341 end
342
343 # Walk the content tree and modify some text runs.
344 modify_content_tree(flowdoc.GetBody())
345
346 # Merge cells
347 merged_cell = new_table.GetTableCell(2, 0).MergeCellsRight(1)
348 merged_cell.SetHorizontalAlignment(TableCell::E_alignment_middle)
349
350 new_table.GetTableCell(0, 0).MergeCellsDown(1).SetVerticalAlignment(TableCell::E_alignment_center)
351
352 # Walk over the table and change the first cell in each row.
353 row_idx = 0
354 clr_row1 = [175, 240, 240]
355 clr_row2 = [250, 250, 175]
356
357 table_itr = new_table.GetContentNodeIterator()
358 while table_itr.HasNext()
359 row = table_itr.Current().AsTableRow()
360 unless row.nil?
361 row_itr = row.GetContentNodeIterator()
362 while row_itr.HasNext()
363 cell = row_itr.Current().AsTableCell()
364 unless cell.nil?
365 if row_idx % 2 != 0
366 cell.SetBackgroundColor(clr_row1[0], clr_row1[1], clr_row1[2])
367 else
368 cell.SetBackgroundColor(clr_row2[0], clr_row2[1], clr_row2[2])
369 end
370
371 cell_itr = cell.GetContentNodeIterator()
372 while cell_itr.HasNext()
373 element = cell_itr.Current();
374 para = element.AsParagraph()
375 unless para.nil?
376 st = para.GetTextStyledElement()
377 st.SetTextColor(255, 0, 0)
378 st.SetFontSize(12)
379 else
380 nested_table = element.AsTable()
381 unless nested_table.nil?
382 nested_cell = nested_table.GetTableCell(1, 1)
383 nested_cell.SetBackgroundColor(255, 127, 127)
384 end
385 end
386 cell_itr.Next()
387 end
388 end
389 row_itr.Next()
390 end
391 end
392
393 row_idx += 1
394 table_itr.Next()
395 end
396
397 my_pdf = flowdoc.PaginateToPDF()
398 my_pdf.Save($outputPath + "created_doc.pdf", SDFDoc::E_linearized)
399 rescue Exception => e
400 puts e.to_s
401 result = false
402 end
403
404 if !result
405 puts "Tests FAILED!!!\n=========="
406 PDFNet.Terminate()
407 return
408 end
409
410 #-----------------------------------------------------------------------------------
411
412 PDFNet.Terminate()
413 puts "Done."
414end
415
416main()
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales