@pdftron/pdfnet-node-samples 10.1.1-beta → 10.2.0-beta

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pdftron/pdfnet-node-samples",
3
- "version": "10.1.1-beta",
3
+ "version": "10.2.0-beta",
4
4
  "description": "Sample code for the @pdftron/pdfnet-node package.",
5
5
  "scripts": {
6
6
  "test": "run-script-os",
@@ -12,7 +12,7 @@
12
12
  "license": "Commercial",
13
13
  "homepage": "https://www.pdftron.com",
14
14
  "dependencies": {
15
- "@pdftron/pdfnet-node": "~10.1.1-beta",
15
+ "@pdftron/pdfnet-node": "~10.2.0-beta",
16
16
  "run-script-os": "^1.1.6"
17
17
  }
18
18
  }
@@ -0,0 +1,153 @@
1
+ //---------------------------------------------------------------------------------------
2
+ // Copyright (c) 2001-2021 by Apryse Software Inc. All Rights Reserved.
3
+ // Consult legal.txt regarding legal and license information.
4
+ //---------------------------------------------------------------------------------------
5
+
6
+ //---------------------------------------------------------------------------------------
7
+ // The following sample illustrates how to convert to PDF with virtual printer on Windows.
8
+ // It supports several input formats like docx, xlsx, rtf, txt, html, pub, emf, etc. For more details, visit
9
+ // https://docs.apryse.com/documentation/windows/guides/features/conversion/convert-other/
10
+ //
11
+ // To check if ToPDF (or ToXPS) require that PDFNet printer is installed use Convert::RequiresPrinter(filename).
12
+ // The installing application must be run as administrator. The manifest for this sample
13
+ // specifies appropriate the UAC elevation.
14
+ //
15
+ // Note: the PDFNet printer is a virtual XPS printer supported on Vista SP1 and Windows 7.
16
+ // For Windows XP SP2 or higher, or Vista SP0 you need to install the XPS Essentials Pack (or
17
+ // equivalent redistributables). You can download the XPS Essentials Pack from:
18
+ // http://www.microsoft.com/downloads/details.aspx?FamilyId=B8DCFFDD-E3A5-44CC-8021-7649FD37FFEE&displaylang=en
19
+ // Windows XP Sp2 will also need the Microsoft Core XML Services (MSXML) 6.0:
20
+ // http://www.microsoft.com/downloads/details.aspx?familyid=993C0BCF-3BCF-4009-BE21-27E85E1857B1&displaylang=en
21
+ //
22
+ // Note: Convert.fromEmf and Convert.toEmf will only work on Windows and require GDI+.
23
+ //
24
+ // Please contact us if you have any questions.
25
+ //---------------------------------------------------------------------------------------
26
+
27
+ const { PDFNet } = require('@pdftron/pdfnet-node');
28
+ const PDFTronLicense = require('../LicenseKey/LicenseKey');
29
+
30
+ ((exports) => {
31
+ 'use strict';
32
+
33
+ let Testfile = function (inputFile, outputFile) {
34
+ this.inputFile = inputFile;
35
+ this.outputFile = outputFile;
36
+ }
37
+
38
+ const testfiles = [
39
+ new Testfile('simple-word_2007.docx', 'docx2pdf.pdf'),
40
+ new Testfile('simple-powerpoint_2007.pptx', 'pptx2pdf.pdf'),
41
+ new Testfile('simple-excel_2007.xlsx', 'xlsx2pdf.pdf'),
42
+ new Testfile('simple-publisher.pub', 'pub2pdf.pdf'),
43
+ new Testfile('simple-text.txt', 'txt2pdf.pdf'),
44
+ new Testfile('simple-rtf.rtf', 'rtf2pdf.pdf'),
45
+ new Testfile('simple-emf.emf', 'emf2pdf.pdf'),
46
+ new Testfile('simple-webpage.mht','mht2pdf.pdf'),
47
+ new Testfile('simple-webpage.html', 'html2pdf.pdf'),
48
+ ]
49
+
50
+
51
+
52
+ const inputPath = '../TestFiles/';
53
+ const outputPath = '../TestFiles/Output/';
54
+
55
+ exports.runConvertPrintTest = () => {
56
+
57
+ const main = async () => {
58
+ if (process.platform === 'win32') {
59
+ try {
60
+ await convertToPdfFromFile();
61
+ console.log('ConvertFile succeeded');
62
+ } catch (err) {
63
+ console.log('ConvertFile failed');
64
+ console.log(err);
65
+ }
66
+
67
+ try {
68
+ await convertSpecificFormats();
69
+ console.log('ConvertSpecificFormats succeeded');
70
+ } catch (err) {
71
+ console.log('ConvertSpecificFormats failed');
72
+ console.log(err);
73
+ }
74
+ if (await PDFNet.Convert.printerIsInstalled()) {
75
+ try {
76
+ console.log('Uninstalling printer (requires Windows platform and administrator)');
77
+ await PDFNet.Convert.printerUninstall();
78
+ console.log('Uninstalled printer ' + await PDFNet.Convert.printerGetPrinterName());
79
+ } catch (err) {
80
+ console.log('Unable to uninstall printer');
81
+ }
82
+ }
83
+ console.log('Done.');
84
+ }
85
+ else {
86
+ console.log('ConvertPrintTest only available on Windows');
87
+ }
88
+ };
89
+
90
+ const convertToPdfFromFile = async () => {
91
+ if (await PDFNet.Convert.printerIsInstalled('PDFTron PDFNet')) {
92
+ await PDFNet.Convert.printerSetPrinterName('PDFTron PDFNet');
93
+ } else if (!(await PDFNet.Convert.printerIsInstalled())) {
94
+ try {
95
+ // This will fail if not run as administrator. Harmless if PDFNet
96
+ // printer already installed
97
+ console.log('Installing printer (requires Windows platform and administrator)');
98
+ await PDFNet.Convert.printerInstall();
99
+ console.log('Installed printer ' + await PDFNet.Convert.printerGetPrinterName());
100
+ } catch (err) {
101
+ console.log('Unable to install printer');
102
+ }
103
+ }
104
+ for (const testfile of testfiles) {
105
+
106
+ try {
107
+ const pdfdoc = await PDFNet.PDFDoc.create();
108
+ await pdfdoc.initSecurityHandler();
109
+ const inputFile = inputPath + testfile.inputFile;
110
+ const outputFile = outputPath + testfile.outputFile;
111
+ if (await PDFNet.Convert.requiresPrinter(inputFile)) {
112
+ console.log('Using PDFNet printer to convert file ' + testfile.inputFile);
113
+ }
114
+ await PDFNet.Convert.toPdf(pdfdoc, inputFile);
115
+ await pdfdoc.save(outputFile, PDFNet.SDFDoc.SaveOptions.e_linearized);
116
+ console.log('Converted file: ' + testfile.inputFile + '\nto: ' + testfile.outputFile);
117
+ } catch (err) {
118
+ console.log('Unable to convert file ' + testfile.inputFile);
119
+ console.log(err);
120
+ }
121
+ }
122
+ };
123
+
124
+ const convertSpecificFormats = async () => {
125
+
126
+ try {
127
+ // Convert MSWord document to XPS
128
+ console.log('Converting DOCX to XPS');
129
+ await PDFNet.Convert.fileToXps(inputPath + 'simple-word_2007.docx', outputPath + 'simple-word_2007.xps');
130
+ console.log('Saved simple-word_2007.xps');
131
+ } catch (err) {
132
+ console.log(err);
133
+ }
134
+ try {
135
+ const pdfdoc = await PDFNet.PDFDoc.create();
136
+ await pdfdoc.initSecurityHandler();
137
+
138
+ console.log('Converting from EMF');
139
+ await PDFNet.Convert.fromEmf(pdfdoc, inputPath + 'simple-emf.emf');
140
+ await pdfdoc.save(outputPath + 'emf2pdf v2.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
141
+ console.log('Saved emf2pdf v2.pdf');
142
+ } catch (err) {
143
+ console.log(err);
144
+ }
145
+ };
146
+ PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function(error) {
147
+ console.log('Error: ' + JSON.stringify(error));
148
+ }).then(function(){ return PDFNet.shutdown(); });
149
+ };
150
+ exports.runConvertPrintTest();
151
+ })(exports);
152
+ // eslint-disable-next-line spaced-comment
153
+ //# sourceURL=ConvertPrintTest.js
@@ -5,22 +5,11 @@
5
5
 
6
6
  //---------------------------------------------------------------------------------------
7
7
  // The following sample illustrates how to use the PDF::Convert utility class to convert
8
- // documents and files to PDF, XPS, SVG, or EMF.
8
+ // documents and files to PDF, XPS, or SVG, or EMF. The sample also shows how to convert MS Office files
9
+ // using our built in conversion.
9
10
  //
10
11
  // Certain file formats such as XPS, EMF, PDF, and raster image formats can be directly
11
- // converted to PDF or XPS. Other formats are converted using a virtual driver. To check
12
- // if ToPDF (or ToXPS) require that PDFNet printer is installed use Convert::RequiresPrinter(filename).
13
- // The installing application must be run as administrator. The manifest for this sample
14
- // specifies appropriate the UAC elevation.
15
- //
16
- // Note: the PDFNet printer is a virtual XPS printer supported on Vista SP1 and Windows 7.
17
- // For Windows XP SP2 or higher, or Vista SP0 you need to install the XPS Essentials Pack (or
18
- // equivalent redistributables). You can download the XPS Essentials Pack from:
19
- // http://www.microsoft.com/downloads/details.aspx?FamilyId=B8DCFFDD-E3A5-44CC-8021-7649FD37FFEE&displaylang=en
20
- // Windows XP Sp2 will also need the Microsoft Core XML Services (MSXML) 6.0:
21
- // http://www.microsoft.com/downloads/details.aspx?familyid=993C0BCF-3BCF-4009-BE21-27E85E1857B1&displaylang=en
22
- //
23
- // Note: Convert.FromEmf and Convert.ToEmf will only work on Windows and require GDI+.
12
+ // converted to PDF or XPS.
24
13
  //
25
14
  // Please contact us if you have any questions.
26
15
  //---------------------------------------------------------------------------------------
@@ -31,23 +20,18 @@ const PDFTronLicense = require('../LicenseKey/LicenseKey');
31
20
  ((exports) => {
32
21
  'use strict';
33
22
 
34
- let Testfile = function (inputFile, outputFile, requiresWindowsPlatform) {
23
+ let Testfile = function (inputFile, outputFile) {
35
24
  this.inputFile = inputFile;
36
25
  this.outputFile = outputFile;
37
- this.requiresWindowsPlatform = requiresWindowsPlatform;
38
26
  }
39
27
 
40
28
  const testfiles = [
41
- new Testfile('simple-word_2007.docx', 'docx2pdf.pdf', false),
42
- new Testfile('simple-powerpoint_2007.pptx', 'pptx2pdf.pdf', false),
43
- new Testfile('simple-excel_2007.xlsx', 'xlsx2pdf.pdf', false),
44
- new Testfile('simple-publisher.pub', 'pub2pdf.pdf', true),
45
- new Testfile('simple-text.txt', 'txt2pdf.pdf', false),
46
- new Testfile('simple-rtf.rtf', 'rtf2pdf.pdf', true),
47
- new Testfile('butterfly.png', 'png2pdf.pdf', false),
48
- new Testfile('simple-emf.emf', 'emf2pdf.pdf', true),
49
- new Testfile('simple-xps.xps', 'xps2pdf.pdf', false),
50
- new Testfile('simple-webpage.html', 'html2pdf.pdf', true),
29
+ new Testfile('simple-word_2007.docx', 'docx2pdf.pdf'),
30
+ new Testfile('simple-powerpoint_2007.pptx', 'pptx2pdf.pdf'),
31
+ new Testfile('simple-excel_2007.xlsx', 'xlsx2pdf.pdf'),
32
+ new Testfile('simple-text.txt', 'txt2pdf.pdf'),
33
+ new Testfile('butterfly.png', 'png2pdf.pdf'),
34
+ new Testfile('simple-xps.xps', 'xps2pdf.pdf'),
51
35
  ]
52
36
 
53
37
  const inputPath = '../TestFiles/';
@@ -72,47 +56,19 @@ const PDFTronLicense = require('../LicenseKey/LicenseKey');
72
56
  console.log(err);
73
57
  }
74
58
 
75
- if (process.platform === 'win32' && await PDFNet.Convert.printerIsInstalled()) {
76
- try {
77
- console.log('Uninstalling printer (requires Windows platform and administrator)');
78
- await PDFNet.Convert.printerUninstall();
79
- console.log('Uninstalled printer ' + await PDFNet.Convert.printerGetPrinterName());
80
- } catch (err) {
81
- console.log('Unable to uninstall printer');
82
- }
83
- }
84
-
85
59
  console.log('Done.');
86
60
  };
87
61
 
88
62
  const convertToPdfFromFile = async () => {
89
- if (process.platform === 'win32') {
90
- if (await PDFNet.Convert.printerIsInstalled('PDFTron PDFNet')) {
91
- await PDFNet.Convert.printerSetPrinterName('PDFTron PDFNet');
92
- } else if (!(await PDFNet.Convert.printerIsInstalled())) {
93
- try {
94
- // This will fail if not run as administrator. Harmless if PDFNet
95
- // printer already installed
96
- console.log('Installing printer (requires Windows platform and administrator)');
97
- await PDFNet.Convert.printerInstall();
98
- console.log('Installed printer ' + await PDFNet.Convert.printerGetPrinterName());
99
- } catch (err) {
100
- console.log('Unable to install printer');
101
- }
102
- }
103
- }
104
63
 
105
64
  for (const testfile of testfiles) {
106
- if (process.platform !== 'win32' && testfile.requiresWindowsPlatform) continue;
107
65
 
108
66
  try {
109
67
  const pdfdoc = await PDFNet.PDFDoc.create();
110
68
  await pdfdoc.initSecurityHandler();
111
69
  const inputFile = inputPath + testfile.inputFile;
112
70
  const outputFile = outputPath + testfile.outputFile;
113
- if (await PDFNet.Convert.requiresPrinter(inputFile)) {
114
- console.log('Using PDFNet printer to convert file ' + testfile.inputFile);
115
- }
71
+ await PDFNet.Convert.printerSetMode(PDFNet.Convert.PrinterMode.e_prefer_builtin_converter);
116
72
  await PDFNet.Convert.toPdf(pdfdoc, inputFile);
117
73
  await pdfdoc.save(outputFile, PDFNet.SDFDoc.SaveOptions.e_linearized);
118
74
  console.log('Converted file: ' + testfile.inputFile + '\nto: ' + testfile.outputFile);
@@ -136,19 +92,6 @@ const PDFTronLicense = require('../LicenseKey/LicenseKey');
136
92
  console.log(err);
137
93
  }
138
94
 
139
- if (process.platform === 'win32') {
140
- try {
141
- const pdfdoc = await PDFNet.PDFDoc.create();
142
- await pdfdoc.initSecurityHandler();
143
-
144
- console.log('Converting from EMF');
145
- await PDFNet.Convert.fromEmf(pdfdoc, inputPath + 'simple-emf.emf');
146
- await pdfdoc.save(outputPath + 'emf2pdf v2.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
147
- console.log('Saved emf2pdf v2.pdf');
148
- } catch (err) {
149
- console.log(err);
150
- }
151
- }
152
95
 
153
96
  try {
154
97
  const pdfdoc = await PDFNet.PDFDoc.create();
@@ -194,16 +137,6 @@ const PDFTronLicense = require('../LicenseKey/LicenseKey');
194
137
  console.log(err);
195
138
  }
196
139
 
197
- if (process.platform === 'win32') {
198
- try {
199
- // Convert MSWord document to XPS
200
- console.log('Converting DOCX to XPS');
201
- await PDFNet.Convert.fileToXps(inputPath + 'simple-word_2007.docx', outputPath + 'simple-word_2007.xps');
202
- console.log('Saved simple-word_2007.xps');
203
- } catch (err) {
204
- console.log(err);
205
- }
206
- }
207
140
 
208
141
  try {
209
142
  // Convert PDF document to XPS
@@ -9,7 +9,35 @@ const PDFTronLicense = require('../LicenseKey/LicenseKey');
9
9
  ((exports) => {
10
10
  'use strict';
11
11
 
12
- exports.runDocumentCreationTest = () => {
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
+ };
13
41
 
14
42
  const main = async () => {
15
43
 
@@ -31,35 +59,338 @@ const PDFTronLicense = require('../LicenseKey/LicenseKey');
31
59
 
32
60
  const flowdoc = await PDFNet.FlowDocument.create();
33
61
  const para = await flowdoc.addParagraph();
34
-
35
- para.setFontSize(24);
36
- para.setTextColor(255, 0, 0);
37
- para.addText("Start Red Text\n");
38
- flowdoc.setDefaultMargins(0, 72.0, 144.0, 228.0);
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);
39
252
  flowdoc.setDefaultPageSize(650, 750);
40
253
  flowdoc.addParagraphWithText(para_text);
41
-
42
- for (var i = 0; i < 50; i++) {
254
+
255
+ const clr1 = [50, 50, 199];
256
+ const clr2 = [30, 199, 30];
257
+
258
+ for (let i = 0; i < 50; i++) {
43
259
  const para = await flowdoc.addParagraph();
44
- const point_size = (17*i*i*i)%13+5;
45
- if (i % 2 == 0)
46
- {
47
- para.setItalic(true);
48
- para.setTextColor(50, 50, 199);
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);
49
266
  para.setSpaceBefore(20);
50
- para.setJustificationMode(PDFNet.ParagraphStyle.TextJustification.e_text_justify_left);
51
- }
52
- else
53
- {
54
- para.setTextColor(30, 199, 30);
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]);
55
271
  para.setSpaceBefore(50);
56
- para.setJustificationMode(PDFNet.ParagraphStyle.TextJustification.e_text_justify_right);
272
+ para.setEndIndent(20);
273
+ para.setJustificationMode(PDFNet.Paragraph.TextJustification.e_text_justify_right);
57
274
  }
58
-
275
+
59
276
  para.addText(para_text);
60
- para.setFontSize(point_size);
277
+ para.addText(' ' + para_text);
278
+ style.setFontSize(point_size);
61
279
  }
62
-
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
+
63
394
  const my_pdf = await flowdoc.paginateToPDF();
64
395
  my_pdf.save(outputPath + "created_doc.pdf", PDFNet.SDFDoc.SaveOptions.e_linearized);
65
396
  console.log('Done.');