@pdftron/pdfnet-node-samples 9.4.2 → 9.5.0
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 +18 -18
- package/readme.md +12 -12
- package/samples/AddImageTest/AddImageTest.js +1 -1
- package/samples/AddImageTest/NODEJS/AddImageTest.js +116 -0
- package/samples/AddImageTest/NODEJS/RunTest.sh +2 -0
- package/samples/AdvancedImagingTest/AdvancedImagingTest.js +1 -1
- package/samples/AdvancedImagingTest/NODEJS/AdvancedImagingTest.js +78 -0
- package/samples/AdvancedImagingTest/NODEJS/RunTest.sh +2 -0
- package/samples/AnnotationTest/AnnotationTest.js +1 -1
- package/samples/AnnotationTest/NODEJS/AnnotationTest.js +642 -0
- package/samples/AnnotationTest/NODEJS/RunTest.sh +2 -0
- package/samples/BookmarkTest/BookmarkTest.js +1 -1
- package/samples/BookmarkTest/NODEJS/BookmarkTest.js +220 -0
- package/samples/BookmarkTest/NODEJS/RunTest.sh +2 -0
- package/samples/CAD2PDFTest/CAD2PDFTest.js +79 -79
- package/samples/CAD2PDFTest/NODEJS/CAD2PDFTest.js +80 -0
- package/samples/CAD2PDFTest/NODEJS/RunTest.sh +2 -0
- package/samples/ContentReplacerTest/ContentReplacerTest.js +1 -1
- package/samples/ContentReplacerTest/NODEJS/ContentReplacerTest.js +75 -0
- package/samples/ContentReplacerTest/NODEJS/RunTest.sh +2 -0
- package/samples/ConvertTest/ConvertTest.js +270 -256
- package/samples/ConvertTest/NODEJS/ConvertTest.js +270 -0
- package/samples/ConvertTest/NODEJS/RunTest.sh +2 -0
- package/samples/DataExtractionTest/DataExtractionTest.js +180 -0
- package/samples/DataExtractionTest/NODEJS/DataExtractionTest.js +180 -0
- package/samples/DataExtractionTest/NODEJS/RunTest.sh +2 -0
- package/samples/DigitalSignaturesTest/DigitalSignaturesTest.js +526 -526
- package/samples/DigitalSignaturesTest/NODEJS/DigitalSignaturesTest.js +527 -0
- package/samples/DigitalSignaturesTest/NODEJS/RunTest.sh +2 -0
- package/samples/ElementBuilderTest/ElementBuilderTest.js +1 -1
- package/samples/ElementBuilderTest/NODEJS/ElementBuilderTest.js +514 -0
- package/samples/ElementBuilderTest/NODEJS/RunTest.sh +2 -0
- package/samples/ElementEditTest/ElementEditTest.js +1 -1
- package/samples/ElementEditTest/NODEJS/ElementEditTest.js +111 -0
- package/samples/ElementEditTest/NODEJS/RunTest.sh +2 -0
- package/samples/ElementReaderAdvTest/ElementReaderAdvTest.js +1 -1
- package/samples/ElementReaderAdvTest/NODEJS/ElementReaderAdvTest.js +305 -0
- package/samples/ElementReaderAdvTest/NODEJS/RunTest.sh +2 -0
- package/samples/ElementReaderTest/ElementReaderTest.js +1 -1
- package/samples/ElementReaderTest/NODEJS/ElementReaderTest.js +77 -0
- package/samples/ElementReaderTest/NODEJS/RunTest.sh +2 -0
- package/samples/EncTest/EncTest.js +1 -1
- package/samples/EncTest/NODEJS/EncTest.js +176 -0
- package/samples/EncTest/NODEJS/RunTest.sh +2 -0
- package/samples/FDFTest/FDFTest.js +218 -218
- package/samples/FDFTest/NODEJS/FDFTest.js +219 -0
- package/samples/FDFTest/NODEJS/RunTest.sh +2 -0
- package/samples/HTML2PDFTest/HTML2PDFTest.js +165 -165
- package/samples/HTML2PDFTest/NODEJS/HTML2PDFTest.js +166 -0
- package/samples/HTML2PDFTest/NODEJS/RunTest.sh +2 -0
- package/samples/HighlightsTest/HighlightsTest.js +1 -1
- package/samples/HighlightsTest/NODEJS/HighlightsTest.js +97 -0
- package/samples/HighlightsTest/NODEJS/RunTest.sh +2 -0
- package/samples/ImageExtractTest/ImageExtractTest.js +129 -129
- package/samples/ImageExtractTest/NODEJS/ImageExtractTest.js +130 -0
- package/samples/ImageExtractTest/NODEJS/RunTest.sh +2 -0
- package/samples/ImpositionTest/ImpositionTest.js +86 -86
- package/samples/ImpositionTest/NODEJS/ImpositionTest.js +87 -0
- package/samples/ImpositionTest/NODEJS/RunTest.sh +2 -0
- package/samples/InteractiveFormsTest/InteractiveFormsTest.js +381 -381
- package/samples/InteractiveFormsTest/NODEJS/InteractiveFormsTest.js +382 -0
- package/samples/InteractiveFormsTest/NODEJS/RunTest.sh +2 -0
- package/samples/JBIG2Test/JBIG2Test.js +88 -88
- package/samples/JBIG2Test/NODEJS/JBIG2Test.js +89 -0
- package/samples/JBIG2Test/NODEJS/RunTest.sh +2 -0
- package/samples/LicenseKey/NODEJS/LicenseKey.js +11 -0
- package/samples/LogicalStructureTest/LogicalStructureTest.js +1 -1
- package/samples/LogicalStructureTest/NODEJS/LogicalStructureTest.js +251 -0
- package/samples/LogicalStructureTest/NODEJS/RunTest.sh +2 -0
- package/samples/OCRTest/NODEJS/OCRTest.js +235 -0
- package/samples/OCRTest/NODEJS/RunTest.sh +2 -0
- package/samples/OCRTest/OCRTest.js +234 -234
- package/samples/OfficeTemplateTest/NODEJS/OfficeTemplateTest.js +79 -0
- package/samples/OfficeTemplateTest/NODEJS/RunTest.sh +2 -0
- package/samples/OfficeTemplateTest/OfficeTemplateTest.js +79 -77
- package/samples/OfficeToPDFTest/NODEJS/OfficeToPDFTest.js +125 -0
- package/samples/OfficeToPDFTest/NODEJS/RunTest.sh +2 -0
- package/samples/OfficeToPDFTest/OfficeToPDFTest.js +125 -125
- package/samples/OptimizerTest/NODEJS/OptimizerTest.js +192 -0
- package/samples/OptimizerTest/NODEJS/RunTest.sh +2 -0
- package/samples/OptimizerTest/OptimizerTest.js +191 -191
- package/samples/PDF2HtmlTest/NODEJS/PDF2HtmlTest.js +123 -0
- package/samples/PDF2HtmlTest/NODEJS/RunTest.sh +2 -0
- package/samples/PDF2HtmlTest/PDF2HtmlTest.js +1 -1
- package/samples/PDF2OfficeTest/NODEJS/PDF2OfficeTest.js +158 -0
- package/samples/PDF2OfficeTest/NODEJS/RunTest.sh +2 -0
- package/samples/PDF2OfficeTest/PDF2OfficeTest.js +1 -1
- package/samples/PDFATest/NODEJS/PDFATest.js +85 -0
- package/samples/PDFATest/NODEJS/RunTest.sh +2 -0
- package/samples/PDFATest/PDFATest.js +1 -1
- package/samples/PDFDocMemoryTest/NODEJS/PDFDocMemoryTest.js +85 -0
- package/samples/PDFDocMemoryTest/NODEJS/RunTest.sh +2 -0
- package/samples/PDFDocMemoryTest/PDFDocMemoryTest.js +84 -84
- package/samples/PDFDrawTest/NODEJS/PDFDrawTest.js +306 -0
- package/samples/PDFDrawTest/NODEJS/RunTest.sh +2 -0
- package/samples/PDFDrawTest/PDFDrawTest.js +1 -1
- package/samples/PDFLayersTest/NODEJS/PDFLayersTest.js +295 -0
- package/samples/PDFLayersTest/NODEJS/RunTest.sh +2 -0
- package/samples/PDFLayersTest/PDFLayersTest.js +1 -1
- package/samples/PDFPackageTest/NODEJS/PDFPackageTest.js +112 -0
- package/samples/PDFPackageTest/NODEJS/RunTest.sh +2 -0
- package/samples/PDFPackageTest/PDFPackageTest.js +111 -111
- package/samples/PDFPageTest/NODEJS/PDFPageTest.js +190 -0
- package/samples/PDFPageTest/NODEJS/RunTest.sh +2 -0
- package/samples/PDFPageTest/PDFPageTest.js +1 -1
- package/samples/PDFRedactTest/NODEJS/PDFRedactTest.js +75 -0
- package/samples/PDFRedactTest/NODEJS/RunTest.sh +2 -0
- package/samples/PDFRedactTest/PDFRedactTest.js +1 -1
- package/samples/PageLabelsTest/NODEJS/PageLabelsTest.js +139 -0
- package/samples/PageLabelsTest/NODEJS/RunTest.sh +2 -0
- package/samples/PageLabelsTest/PageLabelsTest.js +138 -138
- package/samples/PatternTest/NODEJS/PatternTest.js +227 -0
- package/samples/PatternTest/NODEJS/RunTest.sh +2 -0
- package/samples/PatternTest/PatternTest.js +226 -226
- package/samples/RectTest/NODEJS/RectTest.js +41 -0
- package/samples/RectTest/NODEJS/RunTest.sh +2 -0
- package/samples/RectTest/RectTest.js +1 -1
- package/samples/SDFTest/NODEJS/RunTest.sh +2 -0
- package/samples/SDFTest/NODEJS/SDFTest.js +88 -0
- package/samples/SDFTest/SDFTest.js +2 -3
- package/samples/StamperTest/NODEJS/RunTest.sh +2 -0
- package/samples/StamperTest/NODEJS/StamperTest.js +256 -0
- package/samples/StamperTest/StamperTest.js +1 -1
- package/samples/TestFiles/financial.pdf +0 -0
- package/samples/TestFiles/formfields-scanned.pdf +0 -0
- package/samples/TestFiles/formfields.pdf +0 -0
- package/samples/TestFiles/table.pdf +0 -0
- package/samples/TestFiles/tiger.svg +378 -0
- package/samples/TextExtractTest/NODEJS/RunTest.sh +2 -0
- package/samples/TextExtractTest/NODEJS/TextExtractTest.js +287 -0
- package/samples/TextExtractTest/TextExtractTest.js +1 -1
- package/samples/TextSearchTest/NODEJS/RunTest.sh +2 -0
- package/samples/TextSearchTest/NODEJS/TextSearchTest.js +122 -0
- package/samples/TextSearchTest/TextSearchTest.js +1 -1
- package/samples/U3DTest/NODEJS/RunTest.sh +2 -0
- package/samples/U3DTest/NODEJS/U3DTest.js +105 -0
- package/samples/U3DTest/U3DTest.js +104 -104
- package/samples/UndoRedoTest/NODEJS/RunTest.sh +2 -0
- package/samples/UndoRedoTest/NODEJS/UndoRedoTest.js +101 -0
- package/samples/UndoRedoTest/UndoRedoTest.js +1 -1
- package/samples/UnicodeWriteTest/NODEJS/RunTest.sh +2 -0
- package/samples/UnicodeWriteTest/NODEJS/UnicodeWriteTest.js +174 -0
- package/samples/UnicodeWriteTest/UnicodeWriteTest.js +1 -1
- package/samples/WebViewerConvertTest/NODEJS/RunTest.sh +2 -0
- package/samples/WebViewerConvertTest/NODEJS/WebViewerConvertTest.js +136 -0
- package/samples/WebViewerConvertTest/WebViewerConvertTest.js +135 -135
- package/samples/runall.bat +12 -12
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
//---------------------------------------------------------------------------------------
|
|
2
|
-
// Copyright (c) 2001-
|
|
2
|
+
// Copyright (c) 2001-2023 by PDFTron Systems Inc. All Rights Reserved.
|
|
3
3
|
// Consult legal.txt regarding legal and license information.
|
|
4
4
|
//---------------------------------------------------------------------------------------
|
|
5
5
|
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
//---------------------------------------------------------------------------------------
|
|
2
|
+
// Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.
|
|
3
|
+
// Consult legal.txt regarding legal and license information.
|
|
4
|
+
//---------------------------------------------------------------------------------------
|
|
5
|
+
|
|
6
|
+
//---------------------------------------------------------------------------------------
|
|
7
|
+
// This sample explores the structure and content of a tagged PDF document and dumps
|
|
8
|
+
// the structure information to the console window.
|
|
9
|
+
//
|
|
10
|
+
// In tagged PDF documents StructTree acts as a central repository for information
|
|
11
|
+
// related to a PDF document's logical structure. The tree consists of StructElement-s
|
|
12
|
+
// and ContentItem-s which are leaf nodes of the structure tree.
|
|
13
|
+
//
|
|
14
|
+
// The sample can be extended to access and extract the marked-content elements such
|
|
15
|
+
// as text and images.
|
|
16
|
+
//---------------------------------------------------------------------------------------
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
const { PDFNet } = require('../../../lib/pdfnet.js');
|
|
20
|
+
const PDFTronLicense = require('../../LicenseKey/NODEJS/LicenseKey');
|
|
21
|
+
|
|
22
|
+
((exports) => {
|
|
23
|
+
|
|
24
|
+
exports.runLogicalStructureTest = () => {
|
|
25
|
+
|
|
26
|
+
const printAndIndent = (printState, indent) => {
|
|
27
|
+
console.log(printState.str);
|
|
28
|
+
|
|
29
|
+
let indentStr = '';
|
|
30
|
+
for (let i = 0; i < indent; ++i) {
|
|
31
|
+
indentStr += ' ';
|
|
32
|
+
}
|
|
33
|
+
printState.str = indentStr;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
// Used in code snippet 1.
|
|
37
|
+
const processStructElement = async(element, indent, printState) => {
|
|
38
|
+
if (!(await element.isValid())) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
// Print out the type and title info, if any.
|
|
44
|
+
printAndIndent(printState, indent++);
|
|
45
|
+
printState.str += 'Type: ' + (await element.getType());
|
|
46
|
+
if (await element.hasTitle()) {
|
|
47
|
+
printState.str += '. Title: ' + (await element.getTitle());
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const num = await element.getNumKids();
|
|
51
|
+
for (let i = 0; i < num; ++i) {
|
|
52
|
+
// Check is the kid is a leaf node (i.e. it is a ContentItem).
|
|
53
|
+
if (await element.isContentItem(i)) {
|
|
54
|
+
const cont = await element.getAsContentItem(i);
|
|
55
|
+
const type = await cont.getType();
|
|
56
|
+
|
|
57
|
+
const page = await cont.getPage();
|
|
58
|
+
|
|
59
|
+
printAndIndent(printState, indent);
|
|
60
|
+
printState.str += 'Content Item. Part of page #' + (await page.getIndex());
|
|
61
|
+
|
|
62
|
+
printAndIndent(printState, indent);
|
|
63
|
+
switch (type) {
|
|
64
|
+
case PDFNet.ContentItem.Type.e_MCID:
|
|
65
|
+
case PDFNet.ContentItem.Type.e_MCR:
|
|
66
|
+
printState.str += 'MCID: ' + (await cont.getMCID());
|
|
67
|
+
break;
|
|
68
|
+
case PDFNet.ContentItem.Type.e_OBJR:
|
|
69
|
+
{
|
|
70
|
+
printState.str += 'OBJR ';
|
|
71
|
+
const refObj = await cont.getRefObj();
|
|
72
|
+
if (refObj) {
|
|
73
|
+
printState.str += '- Referenced Object#: ' + refObj.getObjNum();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
break;
|
|
77
|
+
default:
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
} else { // the kid is another StructElement node.
|
|
81
|
+
await processStructElement(await element.getAsStructElem(i), indent, printState);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
// Used in code snippet 2.
|
|
87
|
+
const processElements = async(reader, printState) => {
|
|
88
|
+
let element;
|
|
89
|
+
while (element = await reader.next()) { // Read page contents
|
|
90
|
+
// In this sample we process only paths & text, but the code can be
|
|
91
|
+
// extended to handle any element type.
|
|
92
|
+
const type = await element.getType();
|
|
93
|
+
if (type === PDFNet.Element.Type.e_path || type === PDFNet.Element.Type.e_text || type === PDFNet.Element.Type.e_path) {
|
|
94
|
+
switch (type) {
|
|
95
|
+
case PDFNet.Element.Type.e_path: // Process path ...
|
|
96
|
+
printState.str += '\nPATH: ';
|
|
97
|
+
break;
|
|
98
|
+
case PDFNet.Element.Type.e_text: // Process text ...
|
|
99
|
+
printState.str += '\nTEXT: ' + (await element.getTextString()) + '\n';
|
|
100
|
+
break;
|
|
101
|
+
case PDFNet.Element.Type.e_form: // Process form XObjects
|
|
102
|
+
printState.str += '\nFORM XObject: ';
|
|
103
|
+
// reader.formBegin();
|
|
104
|
+
// await ProcessElements(reader);
|
|
105
|
+
// reader.end();
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Check if the element is associated with any structural element.
|
|
110
|
+
// Content items are leaf nodes of the structure tree.
|
|
111
|
+
const structParent = await element.getParentStructElement();
|
|
112
|
+
if (await structParent.isValid()) {
|
|
113
|
+
// Print out the parent structural element's type, title, and object number.
|
|
114
|
+
printState.str += ' Type: ' + (await structParent.getType()) + ', MCID: ' + (await element.getStructMCID());
|
|
115
|
+
if (await structParent.hasTitle()) {
|
|
116
|
+
printState.str += '. Title: ' + (await structParent.getTitle());
|
|
117
|
+
}
|
|
118
|
+
printState.str += ', Obj#: ' + (await (await structParent.getSDFObj()).getObjNum());
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
// Used in code snippet 3.
|
|
125
|
+
const processElements2 = async(reader, mcidPageMap) => {
|
|
126
|
+
let element;
|
|
127
|
+
while (element = await reader.next()) { // Read page contents
|
|
128
|
+
// In this sample we process only text, but the code can be extended
|
|
129
|
+
// to handle paths, images, or any other Element type.
|
|
130
|
+
const mcid = await element.getStructMCID();
|
|
131
|
+
if (mcid >= 0 && (await element.getType()) === PDFNet.Element.Type.e_text) {
|
|
132
|
+
const val = await element.getTextString();
|
|
133
|
+
if (mcid in mcidPageMap) {
|
|
134
|
+
mcidPageMap[mcid] += val;
|
|
135
|
+
} else {
|
|
136
|
+
mcidPageMap[mcid] = val;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
// Used in code snippet 3.
|
|
143
|
+
const processStructElement2 = async(element, mcidDocMap, indent, printState) => {
|
|
144
|
+
if (!(await element.isValid())) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Print out the type and title info, if any.
|
|
149
|
+
printAndIndent(printState, indent);
|
|
150
|
+
printState.str += '<' + (await element.getType());
|
|
151
|
+
if (await element.hasTitle()) {
|
|
152
|
+
printState.str += ' title="' + (await element.getTitle()) + '"';
|
|
153
|
+
}
|
|
154
|
+
printState.str += '>';
|
|
155
|
+
|
|
156
|
+
const num = await element.getNumKids();
|
|
157
|
+
for (let i = 0; i < num; ++i) {
|
|
158
|
+
if (await element.isContentItem(i)) {
|
|
159
|
+
const cont = await element.getAsContentItem(i);
|
|
160
|
+
if ((await cont.getType()) === PDFNet.ContentItem.Type.e_MCID) {
|
|
161
|
+
const pageNum = await (await cont.getPage()).getIndex();
|
|
162
|
+
const mcidPageMap = mcidDocMap[pageNum];
|
|
163
|
+
if (mcidPageMap) {
|
|
164
|
+
const mcid = await cont.getMCID();
|
|
165
|
+
if (mcid in mcidPageMap) {
|
|
166
|
+
printState.str += mcidPageMap[mcid];
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
} else { // the kid is another StructElement node.
|
|
171
|
+
await processStructElement2(await element.getAsStructElem(i), mcidDocMap, indent + 1, printState);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
printAndIndent(printState, indent);
|
|
176
|
+
printState.str += '</' + (await element.getType()) + '>';
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
const main = async() => {
|
|
180
|
+
// Relative path to the folder containing test files.
|
|
181
|
+
const inputPath = '../../TestFiles/';
|
|
182
|
+
const printState = { str: '' };
|
|
183
|
+
try { // Extract logical structure from a PDF document
|
|
184
|
+
const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'tagged.pdf');
|
|
185
|
+
doc.initSecurityHandler();
|
|
186
|
+
|
|
187
|
+
let reader = null;
|
|
188
|
+
let tree = null;
|
|
189
|
+
|
|
190
|
+
console.log('____________________________________________________________');
|
|
191
|
+
console.log('Sample 1 - Traverse logical structure tree...');
|
|
192
|
+
tree = await doc.getStructTree();
|
|
193
|
+
if (await tree.isValid()) {
|
|
194
|
+
console.log('Document has a StructTree root.');
|
|
195
|
+
for (let i = 0, numKids = await tree.getNumKids(); i < numKids; ++i) {
|
|
196
|
+
// Recursively get structure info for all child elements.
|
|
197
|
+
await processStructElement(await tree.getKid(i), 0, printState);
|
|
198
|
+
}
|
|
199
|
+
} else {
|
|
200
|
+
console.log('This document does not contain any logical structure.');
|
|
201
|
+
}
|
|
202
|
+
printAndIndent(printState, 0);
|
|
203
|
+
console.log('Done 1.');
|
|
204
|
+
|
|
205
|
+
console.log('____________________________________________________________');
|
|
206
|
+
console.log('Sample 2 - Get parent logical structure elements from');
|
|
207
|
+
console.log('layout elements.');
|
|
208
|
+
reader = await PDFNet.ElementReader.create();
|
|
209
|
+
for (let itr = await doc.getPageIterator(); await itr.hasNext(); itr.next()) {
|
|
210
|
+
reader.beginOnPage(await itr.current());
|
|
211
|
+
await processElements(reader, printState);
|
|
212
|
+
reader.end();
|
|
213
|
+
}
|
|
214
|
+
printAndIndent(printState, 0);
|
|
215
|
+
console.log('Done 2.');
|
|
216
|
+
|
|
217
|
+
console.log('____________________________________________________________');
|
|
218
|
+
console.log("Sample 3 - 'XML style' extraction of PDF logical structure and page content.");
|
|
219
|
+
{
|
|
220
|
+
const mcidDocMap = {};
|
|
221
|
+
for (let itr = await doc.getPageIterator(); await itr.hasNext(); itr.next()) {
|
|
222
|
+
const page = await itr.current();
|
|
223
|
+
reader.beginOnPage(page);
|
|
224
|
+
const pageNum = await page.getIndex();
|
|
225
|
+
const pageMcidMap = {};
|
|
226
|
+
mcidDocMap[pageNum] = pageMcidMap;
|
|
227
|
+
await processElements2(reader, pageMcidMap);
|
|
228
|
+
reader.end();
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
tree = await doc.getStructTree();
|
|
232
|
+
if (await tree.isValid()) {
|
|
233
|
+
for (let i = 0, numKids = await tree.getNumKids(); i < numKids; ++i) {
|
|
234
|
+
await processStructElement2(await tree.getKid(i), mcidDocMap, 0, printState);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
printAndIndent(printState, 0);
|
|
239
|
+
console.log('Done 3.');
|
|
240
|
+
await doc.save(inputPath + 'Output/LogicalStructure.pdf', 0);
|
|
241
|
+
} catch (err) {
|
|
242
|
+
console.log(err);
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function(error){console.log('Error: ' + JSON.stringify(error));}).then(function(){return PDFNet.shutdown();});
|
|
247
|
+
};
|
|
248
|
+
exports.runLogicalStructureTest();
|
|
249
|
+
})(exports);
|
|
250
|
+
// eslint-disable-next-line spaced-comment
|
|
251
|
+
//# sourceURL=LogicalStructureTest.js
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
//---------------------------------------------------------------------------------------
|
|
2
|
+
// Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.
|
|
3
|
+
// Consult legal.txt regarding legal and license information.
|
|
4
|
+
//---------------------------------------------------------------------------------------
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
const { PDFNet } = require('../../../lib/pdfnet.js');
|
|
8
|
+
const PDFTronLicense = require('../../LicenseKey/NODEJS/LicenseKey');
|
|
9
|
+
|
|
10
|
+
((exports) => {
|
|
11
|
+
'use strict';
|
|
12
|
+
|
|
13
|
+
//---------------------------------------------------------------------------------------
|
|
14
|
+
// The following sample illustrates how to use OCR module
|
|
15
|
+
//---------------------------------------------------------------------------------------
|
|
16
|
+
exports.runOCRTest = () => {
|
|
17
|
+
const main = async () => {
|
|
18
|
+
try {
|
|
19
|
+
|
|
20
|
+
PDFNet.addResourceSearchPath('../../../lib/');
|
|
21
|
+
|
|
22
|
+
const useIRIS = await PDFNet.OCRModule.isIRISModuleAvailable();
|
|
23
|
+
if (!(await PDFNet.OCRModule.isModuleAvailable())) {
|
|
24
|
+
console.log('\nUnable to run OCRTest: PDFTron SDK OCR module not available.');
|
|
25
|
+
console.log('---------------------------------------------------------------');
|
|
26
|
+
console.log('The OCR module is an optional add-on, available for download');
|
|
27
|
+
console.log('at http://www.pdftron.com/. If you have already downloaded this');
|
|
28
|
+
console.log('module, ensure that the SDK is able to find the required files');
|
|
29
|
+
console.log('using the PDFNet.addResourceSearchPath() function.\n');
|
|
30
|
+
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Relative path to the folder containing test files.
|
|
35
|
+
const input_path = '../../TestFiles/OCR/';
|
|
36
|
+
const output_path = '../../TestFiles/Output/';
|
|
37
|
+
|
|
38
|
+
//--------------------------------------------------------------------------------
|
|
39
|
+
// Example 1) Process image without specifying options, default language - English - is used
|
|
40
|
+
try {
|
|
41
|
+
|
|
42
|
+
// A) Setup empty destination doc
|
|
43
|
+
const doc = await PDFNet.PDFDoc.create();
|
|
44
|
+
|
|
45
|
+
await doc.initSecurityHandler();
|
|
46
|
+
|
|
47
|
+
const opts = new PDFNet.OCRModule.OCROptions();
|
|
48
|
+
if(useIRIS) opts.setOCREngine('iris');
|
|
49
|
+
|
|
50
|
+
// B) Run OCR on the .png with options
|
|
51
|
+
await PDFNet.OCRModule.imageToPDF(doc, input_path + 'psychomachia_excerpt.png', opts);
|
|
52
|
+
|
|
53
|
+
// C) check the result
|
|
54
|
+
await doc.save(output_path + 'psychomachia_excerpt.pdf', 0);
|
|
55
|
+
|
|
56
|
+
console.log('Example 1: psychomachia_excerpt.png');
|
|
57
|
+
|
|
58
|
+
} catch (err) {
|
|
59
|
+
console.log(err);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
//--------------------------------------------------------------------------------
|
|
63
|
+
// Example 2) Process document using multiple languages
|
|
64
|
+
try {
|
|
65
|
+
// A) Setup empty destination doc
|
|
66
|
+
const doc = await PDFNet.PDFDoc.create();
|
|
67
|
+
await doc.initSecurityHandler();
|
|
68
|
+
|
|
69
|
+
// B) Setup options with multiple target languages, English will always be considered as secondary language
|
|
70
|
+
const opts = new PDFNet.OCRModule.OCROptions();
|
|
71
|
+
if(useIRIS) opts.setOCREngine('iris');
|
|
72
|
+
opts.addLang('rus');
|
|
73
|
+
opts.addLang('deu');
|
|
74
|
+
|
|
75
|
+
// C) Run OCR on the .jpg with options
|
|
76
|
+
await PDFNet.OCRModule.imageToPDF(doc, input_path + 'multi_lang.jpg', opts);
|
|
77
|
+
|
|
78
|
+
// D) check the result
|
|
79
|
+
await doc.save(output_path + 'multi_lang.pdf', 0);
|
|
80
|
+
|
|
81
|
+
console.log('Example 2: multi_lang.jpg');
|
|
82
|
+
} catch (err) {
|
|
83
|
+
console.log(err);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
//--------------------------------------------------------------------------------
|
|
87
|
+
// Example 3) Process a .pdf specifying a language - German - and ignore zone comprising a sidebar image
|
|
88
|
+
try {
|
|
89
|
+
// A) Open the .pdf document
|
|
90
|
+
const doc = await PDFNet.PDFDoc.createFromFilePath(input_path + 'german_kids_song.pdf');
|
|
91
|
+
doc.initSecurityHandler();
|
|
92
|
+
|
|
93
|
+
// B) Setup options with a single language and an ignore zone
|
|
94
|
+
const opts = new PDFNet.OCRModule.OCROptions();
|
|
95
|
+
if(useIRIS) opts.setOCREngine('iris');
|
|
96
|
+
opts.addLang('deu');
|
|
97
|
+
|
|
98
|
+
const ignore_zones = [];
|
|
99
|
+
ignore_zones.push(new PDFNet.Rect(424, 163, 493, 730));
|
|
100
|
+
opts.addIgnoreZonesForPage(ignore_zones, 1);
|
|
101
|
+
|
|
102
|
+
// C) Run OCR on the .pdf with options
|
|
103
|
+
await PDFNet.OCRModule.processPDF(doc, opts);
|
|
104
|
+
|
|
105
|
+
// D) check the result
|
|
106
|
+
await doc.save(output_path + 'german_kids_song.pdf', 0);
|
|
107
|
+
|
|
108
|
+
console.log('Example 3: german_kids_song.pdf');
|
|
109
|
+
} catch (err) {
|
|
110
|
+
console.log(err);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
//--------------------------------------------------------------------------------
|
|
114
|
+
// Example 4) Process multipage tiff with text/ignore zones specified for each page, optionally provide English as the target language
|
|
115
|
+
try {
|
|
116
|
+
// A) Setup empty destination doc
|
|
117
|
+
const doc = await PDFNet.PDFDoc.create();
|
|
118
|
+
await doc.initSecurityHandler();
|
|
119
|
+
|
|
120
|
+
// B) Setup options with a single language plus text/ignore zones
|
|
121
|
+
const opts = new PDFNet.OCRModule.OCROptions();
|
|
122
|
+
if(useIRIS) opts.setOCREngine('iris');
|
|
123
|
+
opts.addLang('eng');
|
|
124
|
+
|
|
125
|
+
var ignore_zones = [];
|
|
126
|
+
// ignore signature box in the first 2 pages
|
|
127
|
+
ignore_zones.push(new PDFNet.Rect(1492, 56, 2236, 432));
|
|
128
|
+
opts.addIgnoreZonesForPage(ignore_zones, 1);
|
|
129
|
+
|
|
130
|
+
ignore_zones = [];
|
|
131
|
+
ignore_zones.push(new PDFNet.Rect(1492, 56, 2236, 432));
|
|
132
|
+
opts.addIgnoreZonesForPage(ignore_zones, 2);
|
|
133
|
+
|
|
134
|
+
// can use a combination of ignore and text boxes to focus on the page area of interest,
|
|
135
|
+
// as ignore boxes are applied first, we remove the arrows before selecting part of the diagram
|
|
136
|
+
ignore_zones = [];
|
|
137
|
+
ignore_zones.push(new PDFNet.Rect(992, 1276, 1368, 1372));
|
|
138
|
+
opts.addIgnoreZonesForPage(ignore_zones, 3);
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
const text_zones = [];
|
|
142
|
+
// we only have text zones selected in page 3
|
|
143
|
+
|
|
144
|
+
// select horizontal BUFFER ZONE sign
|
|
145
|
+
text_zones.push(new PDFNet.Rect(900, 2384, 1236, 2480));
|
|
146
|
+
// select right vertical BUFFER ZONE sign
|
|
147
|
+
text_zones.push(new PDFNet.Rect(1960, 1976, 2016, 2296));
|
|
148
|
+
// select Lot No.
|
|
149
|
+
text_zones.push(new PDFNet.Rect(696, 1028, 1196, 1128));
|
|
150
|
+
|
|
151
|
+
// select part of the plan inside the BUFFER ZONE
|
|
152
|
+
text_zones.push(new PDFNet.Rect(428, 1484, 1784, 2344));
|
|
153
|
+
text_zones.push(new PDFNet.Rect(948, 1288, 1672, 1476));
|
|
154
|
+
opts.addTextZonesForPage(text_zones, 3);
|
|
155
|
+
|
|
156
|
+
// C) Run OCR on the .tif with options
|
|
157
|
+
await PDFNet.OCRModule.imageToPDF(doc, input_path + 'bc_environment_protection.tif', opts);
|
|
158
|
+
|
|
159
|
+
// D) check the result
|
|
160
|
+
await doc.save(output_path + 'bc_environment_protection.pdf', 0);
|
|
161
|
+
|
|
162
|
+
console.log('Example 4: bc_environment_protection.tif');
|
|
163
|
+
} catch (err) {
|
|
164
|
+
console.log(err);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
//--------------------------------------------------------------------------------
|
|
168
|
+
// Example 5) Alternative workflow for extracting OCR result JSON, postprocessing (e.g., removing words not in the dictionary or filtering special
|
|
169
|
+
// out special characters), and finally applying modified OCR JSON to the source PDF document
|
|
170
|
+
try {
|
|
171
|
+
// A) Open the .pdf document
|
|
172
|
+
const doc = await PDFNet.PDFDoc.createFromFilePath(input_path + 'zero_value_test_no_text.pdf');
|
|
173
|
+
await doc.initSecurityHandler();
|
|
174
|
+
|
|
175
|
+
const opts = new PDFNet.OCRModule.OCROptions();
|
|
176
|
+
if(useIRIS) opts.setOCREngine('iris');
|
|
177
|
+
|
|
178
|
+
// B) Run OCR on the .pdf with default English language
|
|
179
|
+
const json = await PDFNet.OCRModule.getOCRJsonFromPDF(doc, opts);
|
|
180
|
+
|
|
181
|
+
// C) Post-processing step (whatever it might be)
|
|
182
|
+
console.log('Have OCR result JSON, re-applying to PDF ');
|
|
183
|
+
|
|
184
|
+
// D) Apply potentially modified OCR JSON to the PDF
|
|
185
|
+
await PDFNet.OCRModule.applyOCRJsonToPDF(doc, json);
|
|
186
|
+
|
|
187
|
+
// E) Check the result
|
|
188
|
+
await doc.save(output_path + 'zero_value_test_no_text.pdf', 0);
|
|
189
|
+
|
|
190
|
+
console.log('Example 5: extracting and applying OCR JSON from zero_value_test_no_text.pdf');
|
|
191
|
+
} catch (err) {
|
|
192
|
+
console.log(err);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
//--------------------------------------------------------------------------------
|
|
196
|
+
// Example 6) The postprocessing workflow has also an option of extracting OCR results in XML format, similar to the one used by TextExtractor
|
|
197
|
+
try {
|
|
198
|
+
|
|
199
|
+
// A) Setup empty destination doc
|
|
200
|
+
const doc = await PDFNet.PDFDoc.create();
|
|
201
|
+
await doc.initSecurityHandler();
|
|
202
|
+
|
|
203
|
+
const opts = new PDFNet.OCRModule.OCROptions();
|
|
204
|
+
if(useIRIS) opts.setOCREngine('iris');
|
|
205
|
+
|
|
206
|
+
// B) Run OCR on the .tif with default English language, extracting OCR results in XML format. Note that
|
|
207
|
+
// in the process we convert the source image into PDF. We reuse this PDF document later to add hidden text layer to it.
|
|
208
|
+
const xml = await PDFNet.OCRModule.getOCRXmlFromImage(doc, input_path + 'physics.tif', opts);
|
|
209
|
+
|
|
210
|
+
// C) Post-processing step (whatever it might be)
|
|
211
|
+
console.log('Have OCR result XML, re-applying to PDF');
|
|
212
|
+
|
|
213
|
+
// D) Apply potentially modified OCR XML to the PDF
|
|
214
|
+
await PDFNet.OCRModule.applyOCRXmlToPDF(doc, xml);
|
|
215
|
+
|
|
216
|
+
// E) Check the result
|
|
217
|
+
await doc.save(output_path + 'physics.pdf', 0);
|
|
218
|
+
|
|
219
|
+
console.log('Example 6: extracting and applying OCR XML from physics.tif');
|
|
220
|
+
} catch (err) {
|
|
221
|
+
console.log(err);
|
|
222
|
+
}
|
|
223
|
+
console.log('Done.');
|
|
224
|
+
} catch (err) {
|
|
225
|
+
console.log(err);
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function(error) {
|
|
229
|
+
console.log('Error: ' + JSON.stringify(error));
|
|
230
|
+
}).then(function(){ return PDFNet.shutdown(); });
|
|
231
|
+
};
|
|
232
|
+
exports.runOCRTest();
|
|
233
|
+
})(exports);
|
|
234
|
+
// eslint-disable-next-line spaced-comment
|
|
235
|
+
//# sourceURL=OCRTest.js
|