@pdftron/pdfnet-node-samples 10.9.0 → 10.10.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/{samples/AddImageTest → AddImageTest}/AddImageTest.js +115 -115
- package/{samples/AdvancedImagingTest → AdvancedImagingTest}/AdvancedImagingTest.js +78 -78
- package/{samples/AnnotationTest → AnnotationTest}/AnnotationTest.js +641 -641
- package/{samples/BookmarkTest → BookmarkTest}/BookmarkTest.js +219 -219
- package/{samples/CAD2PDFTest → CAD2PDFTest}/CAD2PDFTest.js +79 -79
- package/{samples/ContentReplacerTest → ContentReplacerTest}/ContentReplacerTest.js +75 -75
- package/{samples/ConvertPrintTest → ConvertPrintTest}/ConvertPrintTest.js +153 -153
- package/{samples/ConvertTest → ConvertTest}/ConvertTest.js +203 -203
- package/{samples/DataExtractionTest → DataExtractionTest}/DataExtractionTest.js +214 -214
- package/{samples/DigitalSignaturesTest → DigitalSignaturesTest}/DigitalSignaturesTest.js +622 -527
- package/{samples/DocumentCreationTest → DocumentCreationTest}/DocumentCreationTest.js +409 -409
- package/{samples/ElementBuilderTest → ElementBuilderTest}/ElementBuilderTest.js +513 -513
- package/{samples/ElementEditTest → ElementEditTest}/ElementEditTest.js +110 -110
- package/{samples/ElementReaderAdvTest → ElementReaderAdvTest}/ElementReaderAdvTest.js +305 -305
- package/{samples/ElementReaderTest → ElementReaderTest}/ElementReaderTest.js +77 -77
- package/{samples/EncTest → EncTest}/EncTest.js +175 -175
- package/{samples/FDFTest → FDFTest}/FDFTest.js +218 -218
- package/{samples/HTML2PDFTest → HTML2PDFTest}/HTML2PDFTest.js +164 -164
- package/{samples/HighlightsTest → HighlightsTest}/HighlightsTest.js +97 -97
- package/{samples/ImageExtractTest → ImageExtractTest}/ImageExtractTest.js +129 -129
- package/{samples/ImpositionTest → ImpositionTest}/ImpositionTest.js +86 -86
- package/{samples/InteractiveFormsTest → InteractiveFormsTest}/InteractiveFormsTest.js +381 -381
- package/{samples/JBIG2Test → JBIG2Test}/JBIG2Test.js +88 -88
- package/{samples/LicenseKey → LicenseKey}/LicenseKey.js +11 -11
- package/{samples/LogicalStructureTest → LogicalStructureTest}/LogicalStructureTest.js +250 -250
- package/{samples/OCRTest → OCRTest}/OCRTest.js +235 -235
- package/{samples/OfficeTemplateTest → OfficeTemplateTest}/OfficeTemplateTest.js +79 -79
- package/{samples/OfficeToPDFTest → OfficeToPDFTest}/OfficeToPDFTest.js +125 -125
- package/{samples/OptimizerTest → OptimizerTest}/OptimizerTest.js +191 -191
- package/{samples/PDF2HtmlTest → PDF2HtmlTest}/PDF2HtmlTest.js +123 -123
- package/{samples/PDF2OfficeTest → PDF2OfficeTest}/PDF2OfficeTest.js +158 -158
- package/{samples/PDFATest → PDFATest}/PDFATest.js +85 -85
- package/{samples/PDFDocMemoryTest → PDFDocMemoryTest}/PDFDocMemoryTest.js +84 -84
- package/{samples/PDFDrawTest → PDFDrawTest}/PDFDrawTest.js +305 -305
- package/{samples/PDFLayersTest → PDFLayersTest}/PDFLayersTest.js +294 -294
- package/{samples/PDFPackageTest → PDFPackageTest}/PDFPackageTest.js +111 -111
- package/{samples/PDFPageTest → PDFPageTest}/PDFPageTest.js +189 -189
- package/{samples/PDFRedactTest → PDFRedactTest}/PDFRedactTest.js +74 -74
- package/{samples/PageLabelsTest → PageLabelsTest}/PageLabelsTest.js +138 -138
- package/{samples/PatternTest → PatternTest}/PatternTest.js +226 -226
- package/{samples/RectTest → RectTest}/RectTest.js +40 -40
- package/{samples/SDFTest → SDFTest}/SDFTest.js +87 -87
- package/{samples/StamperTest → StamperTest}/StamperTest.js +255 -255
- package/{samples/TestFiles → TestFiles}/Misc-Fixed.pfa +1166 -1166
- package/{samples/TestFiles → TestFiles}/SHA-2 Root USERTrust RSA CA Sectigo timestamping.crt +34 -34
- package/{samples/TestFiles → TestFiles}/form1_annots.xfdf +33 -33
- package/{samples/TestFiles → TestFiles}/form1_data.xfdf +139 -139
- package/{samples/TestFiles → TestFiles}/my_stream.txt +2310 -2310
- package/{samples/TestFiles → TestFiles}/tiger.svg +378 -378
- package/{samples/TextExtractTest → TextExtractTest}/TextExtractTest.js +286 -286
- package/{samples/TextSearchTest → TextSearchTest}/TextSearchTest.js +121 -121
- package/{samples/U3DTest → U3DTest}/U3DTest.js +104 -104
- package/{samples/UndoRedoTest → UndoRedoTest}/UndoRedoTest.js +101 -101
- package/{samples/UnicodeWriteTest → UnicodeWriteTest}/UnicodeWriteTest.js +173 -173
- package/{samples/WebViewerConvertTest → WebViewerConvertTest}/WebViewerConvertTest.js +135 -135
- package/legal.txt +632 -0
- package/license.pdf +0 -0
- package/package.json +20 -21
- package/readme.md +38 -13
- package/{samples/runall.bat → runall.bat} +12 -12
- package/{samples/runall.sh → runall.sh} +15 -15
- /package/{samples/TestFiles → TestFiles}/BusinessCardTemplate.pdf +0 -0
- /package/{samples/TestFiles → TestFiles}/Fishermen.docx +0 -0
- /package/{samples/TestFiles → TestFiles}/Font_licenses.txt +0 -0
- /package/{samples/TestFiles → TestFiles}/GlobalSignRootForTST.cer +0 -0
- /package/{samples/TestFiles → TestFiles}/License.txt +0 -0
- /package/{samples/TestFiles → TestFiles}/NotoSans_with_hindi.ttf +0 -0
- /package/{samples/TestFiles → TestFiles}/Output/empty +0 -0
- /package/{samples/TestFiles → TestFiles}/SYH_Letter.docx +0 -0
- /package/{samples/TestFiles → TestFiles}/TigerText.pdf +0 -0
- /package/{samples/TestFiles → TestFiles}/US061222892-a.pdf +0 -0
- /package/{samples/TestFiles → TestFiles}/butterfly.png +0 -0
- /package/{samples/TestFiles → TestFiles}/credit card numbers.pdf +0 -0
- /package/{samples/TestFiles → TestFiles}/dice.jpg +0 -0
- /package/{samples/TestFiles → TestFiles}/dice.u3d +0 -0
- /package/{samples/TestFiles → TestFiles}/doc_to_sign.pdf +0 -0
- /package/{samples/TestFiles → TestFiles}/factsheet_Arabic.docx +0 -0
- /package/{samples/TestFiles → TestFiles}/financial.pdf +0 -0
- /package/{samples/TestFiles → TestFiles}/fish.pdf +0 -0
- /package/{samples/TestFiles → TestFiles}/font.ttf +0 -0
- /package/{samples/TestFiles → TestFiles}/form1.pdf +0 -0
- /package/{samples/TestFiles → TestFiles}/form1_data.fdf +0 -0
- /package/{samples/TestFiles → TestFiles}/formfields-scanned-withfields.pdf +0 -0
- /package/{samples/TestFiles → TestFiles}/formfields-scanned.pdf +0 -0
- /package/{samples/TestFiles → TestFiles}/formfields.pdf +0 -0
- /package/{samples/TestFiles → TestFiles}/grayscale.tif +0 -0
- /package/{samples/TestFiles → TestFiles}/hindi_sample_utf16le.txt +0 -0
- /package/{samples/TestFiles → TestFiles}/imagemask.dat +0 -0
- /package/{samples/TestFiles → TestFiles}/logo_red.png +0 -0
- /package/{samples/TestFiles → TestFiles}/lorem_ipsum.pdf +0 -0
- /package/{samples/TestFiles → TestFiles}/multipage.tif +0 -0
- /package/{samples/TestFiles → TestFiles}/newsletter.pdf +0 -0
- /package/{samples/TestFiles → TestFiles}/newsletter.xod +0 -0
- /package/{samples/TestFiles → TestFiles}/numbered.pdf +0 -0
- /package/{samples/TestFiles → TestFiles}/op_blend_test.pdf +0 -0
- /package/{samples/TestFiles → TestFiles}/palm.jp2 +0 -0
- /package/{samples/TestFiles → TestFiles}/paragraphs_and_tables.pdf +0 -0
- /package/{samples/TestFiles → TestFiles}/pdfnet.gif +0 -0
- /package/{samples/TestFiles → TestFiles}/pdftron.bmp +0 -0
- /package/{samples/TestFiles → TestFiles}/pdftron.cer +0 -0
- /package/{samples/TestFiles → TestFiles}/pdftron.pfx +0 -0
- /package/{samples/TestFiles → TestFiles}/pdftron_smart_substitution.plugin +0 -0
- /package/{samples/TestFiles → TestFiles}/peppers.jpg +0 -0
- /package/{samples/TestFiles → TestFiles}/signature.jpg +0 -0
- /package/{samples/TestFiles → TestFiles}/simple-emf.emf +0 -0
- /package/{samples/TestFiles → TestFiles}/simple-excel_2007.xlsx +0 -0
- /package/{samples/TestFiles → TestFiles}/simple-outlook.msg +0 -0
- /package/{samples/TestFiles → TestFiles}/simple-powerpoint_2007.pptx +0 -0
- /package/{samples/TestFiles → TestFiles}/simple-publisher.pub +0 -0
- /package/{samples/TestFiles → TestFiles}/simple-rtf.rtf +0 -0
- /package/{samples/TestFiles → TestFiles}/simple-text.txt +0 -0
- /package/{samples/TestFiles → TestFiles}/simple-visio.vsd +0 -0
- /package/{samples/TestFiles → TestFiles}/simple-webpage.html +0 -0
- /package/{samples/TestFiles → TestFiles}/simple-webpage.mht +0 -0
- /package/{samples/TestFiles → TestFiles}/simple-webpage_files/colorschememapping.xml +0 -0
- /package/{samples/TestFiles → TestFiles}/simple-webpage_files/filelist.xml +0 -0
- /package/{samples/TestFiles → TestFiles}/simple-webpage_files/image001.gif +0 -0
- /package/{samples/TestFiles → TestFiles}/simple-webpage_files/image002.png +0 -0
- /package/{samples/TestFiles → TestFiles}/simple-webpage_files/image003.jpg +0 -0
- /package/{samples/TestFiles → TestFiles}/simple-webpage_files/image004.emz +0 -0
- /package/{samples/TestFiles → TestFiles}/simple-webpage_files/image005.gif +0 -0
- /package/{samples/TestFiles → TestFiles}/simple-webpage_files/image006.png +0 -0
- /package/{samples/TestFiles → TestFiles}/simple-webpage_files/image007.gif +0 -0
- /package/{samples/TestFiles → TestFiles}/simple-webpage_files/oledata.mso +0 -0
- /package/{samples/TestFiles → TestFiles}/simple-webpage_files/themedata.thmx +0 -0
- /package/{samples/TestFiles → TestFiles}/simple-word_2007.docx +0 -0
- /package/{samples/TestFiles → TestFiles}/simple-xps.xps +0 -0
- /package/{samples/TestFiles → TestFiles}/table.pdf +0 -0
- /package/{samples/TestFiles → TestFiles}/tagged.pdf +0 -0
- /package/{samples/TestFiles → TestFiles}/the_rime_of_the_ancient_mariner.docx +0 -0
- /package/{samples/TestFiles → TestFiles}/tiger.pdf +0 -0
- /package/{samples/TestFiles → TestFiles}/waiver.pdf +0 -0
- /package/{samples/TestFiles → TestFiles}/waiver_withApprovalField.pdf +0 -0
- /package/{samples/TestFiles → TestFiles}/waiver_withApprovalField_certified.pdf +0 -0
- /package/{samples/TestFiles → TestFiles}/waiver_withApprovalField_certified_approved.pdf +0 -0
|
@@ -1,77 +1,77 @@
|
|
|
1
|
-
//---------------------------------------------------------------------------------------
|
|
2
|
-
// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
|
|
3
|
-
// Consult legal.txt regarding legal and license information.
|
|
4
|
-
//---------------------------------------------------------------------------------------
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const { PDFNet } = require('@pdftron/pdfnet-node');
|
|
8
|
-
const PDFTronLicense = require('../LicenseKey/LicenseKey');
|
|
9
|
-
|
|
10
|
-
((exports) => {
|
|
11
|
-
|
|
12
|
-
exports.runElementReaderTest = () => {
|
|
13
|
-
|
|
14
|
-
const ProcessElements = async(reader) => {
|
|
15
|
-
// Read page contents
|
|
16
|
-
for (let element = await reader.next(); element !== null; element = await reader.next()) {
|
|
17
|
-
const temp = await element.getType();
|
|
18
|
-
switch (temp) {
|
|
19
|
-
case PDFNet.Element.Type.e_path: // Process path data...
|
|
20
|
-
{
|
|
21
|
-
const data = await element.getPathData();
|
|
22
|
-
/* eslint-disable no-unused-vars */
|
|
23
|
-
const operators = data.operators;
|
|
24
|
-
const points = data.points;
|
|
25
|
-
/* eslint-enable no-unused-vars */
|
|
26
|
-
}
|
|
27
|
-
break;
|
|
28
|
-
case PDFNet.Element.Type.e_text: // Process text strings...
|
|
29
|
-
{
|
|
30
|
-
const data = await element.getTextString();
|
|
31
|
-
console.log(data);
|
|
32
|
-
}
|
|
33
|
-
break;
|
|
34
|
-
case PDFNet.Element.Type.e_form: // Process form XObjects
|
|
35
|
-
reader.formBegin();
|
|
36
|
-
await ProcessElements(reader);
|
|
37
|
-
reader.end();
|
|
38
|
-
break;
|
|
39
|
-
default:
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
const main = async() => {
|
|
45
|
-
console.log('-------------------------------------------------');
|
|
46
|
-
console.log('Sample 1 - Extract text data from all pages in the document.');
|
|
47
|
-
console.log('Opening the input pdf...');
|
|
48
|
-
const ret = 0;
|
|
49
|
-
|
|
50
|
-
// Relative path to the folder containing test files.
|
|
51
|
-
const inputUrl = '../TestFiles/';
|
|
52
|
-
|
|
53
|
-
const doc = await PDFNet.PDFDoc.createFromFilePath(inputUrl + 'newsletter.pdf');// await if there is ret that we care about.
|
|
54
|
-
doc.initSecurityHandler();
|
|
55
|
-
|
|
56
|
-
// eslint-disable-next-line no-unused-vars
|
|
57
|
-
const pgnum = await doc.getPageCount();
|
|
58
|
-
const pageReader = await PDFNet.ElementReader.create();
|
|
59
|
-
const itr = await doc.getPageIterator(1);
|
|
60
|
-
|
|
61
|
-
// Read every page
|
|
62
|
-
for (itr; await itr.hasNext(); itr.next()) {
|
|
63
|
-
const curritr = await itr.current();
|
|
64
|
-
pageReader.beginOnPage(curritr);
|
|
65
|
-
await ProcessElements(pageReader);
|
|
66
|
-
pageReader.end();
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
console.log('Done.');
|
|
70
|
-
return ret;
|
|
71
|
-
};
|
|
72
|
-
PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function(error){console.log('Error: ' + JSON.stringify(error));}).then(function(){return PDFNet.shutdown();});
|
|
73
|
-
};
|
|
74
|
-
exports.runElementReaderTest();
|
|
75
|
-
})(exports);
|
|
76
|
-
// eslint-disable-next-line spaced-comment
|
|
77
|
-
//# sourceURL=ElementReaderTest.js
|
|
1
|
+
//---------------------------------------------------------------------------------------
|
|
2
|
+
// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
|
|
3
|
+
// Consult legal.txt regarding legal and license information.
|
|
4
|
+
//---------------------------------------------------------------------------------------
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
const { PDFNet } = require('@pdftron/pdfnet-node');
|
|
8
|
+
const PDFTronLicense = require('../LicenseKey/LicenseKey');
|
|
9
|
+
|
|
10
|
+
((exports) => {
|
|
11
|
+
|
|
12
|
+
exports.runElementReaderTest = () => {
|
|
13
|
+
|
|
14
|
+
const ProcessElements = async(reader) => {
|
|
15
|
+
// Read page contents
|
|
16
|
+
for (let element = await reader.next(); element !== null; element = await reader.next()) {
|
|
17
|
+
const temp = await element.getType();
|
|
18
|
+
switch (temp) {
|
|
19
|
+
case PDFNet.Element.Type.e_path: // Process path data...
|
|
20
|
+
{
|
|
21
|
+
const data = await element.getPathData();
|
|
22
|
+
/* eslint-disable no-unused-vars */
|
|
23
|
+
const operators = data.operators;
|
|
24
|
+
const points = data.points;
|
|
25
|
+
/* eslint-enable no-unused-vars */
|
|
26
|
+
}
|
|
27
|
+
break;
|
|
28
|
+
case PDFNet.Element.Type.e_text: // Process text strings...
|
|
29
|
+
{
|
|
30
|
+
const data = await element.getTextString();
|
|
31
|
+
console.log(data);
|
|
32
|
+
}
|
|
33
|
+
break;
|
|
34
|
+
case PDFNet.Element.Type.e_form: // Process form XObjects
|
|
35
|
+
reader.formBegin();
|
|
36
|
+
await ProcessElements(reader);
|
|
37
|
+
reader.end();
|
|
38
|
+
break;
|
|
39
|
+
default:
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const main = async() => {
|
|
45
|
+
console.log('-------------------------------------------------');
|
|
46
|
+
console.log('Sample 1 - Extract text data from all pages in the document.');
|
|
47
|
+
console.log('Opening the input pdf...');
|
|
48
|
+
const ret = 0;
|
|
49
|
+
|
|
50
|
+
// Relative path to the folder containing test files.
|
|
51
|
+
const inputUrl = '../TestFiles/';
|
|
52
|
+
|
|
53
|
+
const doc = await PDFNet.PDFDoc.createFromFilePath(inputUrl + 'newsletter.pdf');// await if there is ret that we care about.
|
|
54
|
+
doc.initSecurityHandler();
|
|
55
|
+
|
|
56
|
+
// eslint-disable-next-line no-unused-vars
|
|
57
|
+
const pgnum = await doc.getPageCount();
|
|
58
|
+
const pageReader = await PDFNet.ElementReader.create();
|
|
59
|
+
const itr = await doc.getPageIterator(1);
|
|
60
|
+
|
|
61
|
+
// Read every page
|
|
62
|
+
for (itr; await itr.hasNext(); itr.next()) {
|
|
63
|
+
const curritr = await itr.current();
|
|
64
|
+
pageReader.beginOnPage(curritr);
|
|
65
|
+
await ProcessElements(pageReader);
|
|
66
|
+
pageReader.end();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
console.log('Done.');
|
|
70
|
+
return ret;
|
|
71
|
+
};
|
|
72
|
+
PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function(error){console.log('Error: ' + JSON.stringify(error));}).then(function(){return PDFNet.shutdown();});
|
|
73
|
+
};
|
|
74
|
+
exports.runElementReaderTest();
|
|
75
|
+
})(exports);
|
|
76
|
+
// eslint-disable-next-line spaced-comment
|
|
77
|
+
//# sourceURL=ElementReaderTest.js
|
|
@@ -1,176 +1,176 @@
|
|
|
1
|
-
//---------------------------------------------------------------------------------------
|
|
2
|
-
// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
|
|
3
|
-
// Consult legal.txt regarding legal and license information.
|
|
4
|
-
//---------------------------------------------------------------------------------------
|
|
5
|
-
|
|
6
|
-
//---------------------------------------------------------------------------------------
|
|
7
|
-
// This sample shows encryption support in PDFNet. The sample reads an encrypted document and
|
|
8
|
-
// sets a new SecurityHandler. The sample also illustrates how password protection can
|
|
9
|
-
// be removed from an existing PDF document.
|
|
10
|
-
//---------------------------------------------------------------------------------------
|
|
11
|
-
const { PDFNet } = require('@pdftron/pdfnet-node');
|
|
12
|
-
const PDFTronLicense = require('../LicenseKey/LicenseKey');
|
|
13
|
-
|
|
14
|
-
((exports) => {
|
|
15
|
-
exports.runEncTest = () => {
|
|
16
|
-
|
|
17
|
-
const main = async () => {
|
|
18
|
-
let ret = 0;
|
|
19
|
-
// Relative path to the folder containing test files.
|
|
20
|
-
const inputPath = '../TestFiles/';
|
|
21
|
-
const outputPath = inputPath + 'Output/';
|
|
22
|
-
// Example 1:
|
|
23
|
-
// secure a PDF document with password protection and adjust permissions
|
|
24
|
-
try {
|
|
25
|
-
// Open the test file
|
|
26
|
-
console.log('-------------------------------------------------/nSecuring an existing document...');
|
|
27
|
-
const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'fish.pdf');
|
|
28
|
-
if (!(await doc.initSecurityHandler())) {
|
|
29
|
-
console.log('Document authentication error...');
|
|
30
|
-
ret = 1;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const performOperation = true; // optional parameter
|
|
34
|
-
|
|
35
|
-
// Perform some operation on the document. In this case we use low level SDF API
|
|
36
|
-
// to replace the content stream of the first page with contents of file 'my_stream.txt'
|
|
37
|
-
// Results in fish.pdf becoming a pair of feathers.
|
|
38
|
-
if (performOperation) {
|
|
39
|
-
console.log('Replacing the content stream, use Flate compression...');
|
|
40
|
-
// Get the page dictionary using the following path: trailer/Root/Pages/Kids/0
|
|
41
|
-
const pageTrailer = await doc.getTrailer();
|
|
42
|
-
const pageRoot = await pageTrailer.get('Root');
|
|
43
|
-
const pageRootValue = await pageRoot.value();
|
|
44
|
-
const pages = await pageRootValue.get('Pages');
|
|
45
|
-
const pagesVal = await pages.value();
|
|
46
|
-
const kids = await pagesVal.get('Kids');
|
|
47
|
-
const kidsVal = await kids.value();
|
|
48
|
-
const pageDict = await kidsVal.getAt(0);
|
|
49
|
-
|
|
50
|
-
const embedFile = await PDFNet.Filter.createMappedFileFromUString(inputPath + 'my_stream.txt');
|
|
51
|
-
const mystm = await PDFNet.FilterReader.create(embedFile);
|
|
52
|
-
|
|
53
|
-
const flateEncode = await PDFNet.Filter.createFlateEncode();
|
|
54
|
-
|
|
55
|
-
const indStream = await doc.createIndirectStreamFromFilter(mystm, flateEncode);
|
|
56
|
-
await pageDict.put('Contents', indStream);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// Encrypt the document
|
|
60
|
-
// Apply a new security handler with given security settings.
|
|
61
|
-
// In order to open saved PDF you will need a user password 'test'.
|
|
62
|
-
const newHandler = await PDFNet.SecurityHandler.createDefault();
|
|
63
|
-
|
|
64
|
-
// Set a new password required to open a document
|
|
65
|
-
newHandler.changeUserPasswordUString('test');
|
|
66
|
-
|
|
67
|
-
// Set Permissions
|
|
68
|
-
newHandler.setPermission(PDFNet.SecurityHandler.Permission.e_print, true);
|
|
69
|
-
await newHandler.setPermission(PDFNet.SecurityHandler.Permission.e_extract_content, false);
|
|
70
|
-
|
|
71
|
-
// Note: document takes the ownership of newHandler.
|
|
72
|
-
doc.setSecurityHandler(newHandler);
|
|
73
|
-
|
|
74
|
-
// Save the changes
|
|
75
|
-
console.log('Saving modified file...');
|
|
76
|
-
await doc.save(outputPath + 'secured.pdf', 0);
|
|
77
|
-
console.log('Done. Result saved in secured.pdf');
|
|
78
|
-
} catch (err) {
|
|
79
|
-
console.log(err);
|
|
80
|
-
console.log(err.stack);
|
|
81
|
-
ret = 1;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// Example 2:
|
|
85
|
-
// Opens an encrypted PDF document and removes its security.
|
|
86
|
-
try {
|
|
87
|
-
console.log('-------------------------------------------------');
|
|
88
|
-
console.log('Open the password protected document from the first example...');
|
|
89
|
-
const securedDoc = await PDFNet.PDFDoc.createFromFilePath(outputPath + 'secured.pdf');
|
|
90
|
-
console.log('Initializing security handler without any user interaction...');
|
|
91
|
-
|
|
92
|
-
// At this point MySecurityHandler callbacks will be invoked.
|
|
93
|
-
// MySecurityHandler.GetAuthorizationData() should collect the password and
|
|
94
|
-
// AuthorizeFailed() is called if user repeatedly enters a wrong password.
|
|
95
|
-
if (!(await securedDoc.initStdSecurityHandlerUString('test'))) {
|
|
96
|
-
console.log('Document authentication error.../nThe password is not valid.');
|
|
97
|
-
ret = 1;
|
|
98
|
-
return ret;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
console.log('The password is correct! Document can now be used for reading and editing');
|
|
102
|
-
|
|
103
|
-
// Remove the password security and save the changes to a new file.
|
|
104
|
-
securedDoc.removeSecurity();
|
|
105
|
-
await securedDoc.save(outputPath + 'secured_nomore1.pdf', 0);
|
|
106
|
-
console.log('Done. Result saved in secured_nomore1.pdf');
|
|
107
|
-
|
|
108
|
-
/*
|
|
109
|
-
const hdlr = await securedDoc.getSecurityHandler();
|
|
110
|
-
|
|
111
|
-
console.log('Document Open Password: ' + (await hdlr.isUserPasswordRequired()));
|
|
112
|
-
console.log('Permissions Password: ' + (await hdlr.isMasterPasswordRequired()));
|
|
113
|
-
console.log('Permissions: ');
|
|
114
|
-
console.log("\tHas 'owner' permissions: " + (await hdlr.getPermission(PDFNet.SecurityHandler.Permission.e_owner)));
|
|
115
|
-
|
|
116
|
-
console.log('\tOpen and decrypt the document: ' + (await hdlr.getPermission(PDFNet.SecurityHandler.Permission.e_doc_open)));
|
|
117
|
-
console.log('\tAllow content extraction: ' + (await hdlr.getPermission(PDFNet.SecurityHandler.Permission.e_extract_content)));
|
|
118
|
-
console.log('\tAllow full document editing: ' + (await hdlr.getPermission(PDFNet.SecurityHandler.Permission.e_doc_modify)));
|
|
119
|
-
console.log('\tAllow printing: ' + (await hdlr.getPermission(PDFNet.SecurityHandler.Permission.e_print)));
|
|
120
|
-
console.log('\tAllow high resolution printing: ' + (await hdlr.getPermission(PDFNet.SecurityHandler.Permission.e_print_high)));
|
|
121
|
-
console.log('\tAllow annotation editing: ' + (await hdlr.getPermission(PDFNet.SecurityHandler.Permission.e_mod_annot)));
|
|
122
|
-
console.log('\tAllow form fill: ' + (await hdlr.getPermission(PDFNet.SecurityHandler.Permission.e_fill_forms)));
|
|
123
|
-
console.log('\tAllow content extraction for accessibility: ' + (await hdlr.getPermission(PDFNet.SecurityHandler.Permission.e_access_support)));
|
|
124
|
-
console.log('\tAllow document assembly: ' + (await hdlr.getPermission(PDFNet.SecurityHandler.Permission.e_assemble_doc)));
|
|
125
|
-
*/
|
|
126
|
-
} catch (err) {
|
|
127
|
-
console.log(err.stack);
|
|
128
|
-
ret = 1;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
// Example 3:
|
|
132
|
-
// Encrypt/Decrypt a PDF using PDFTron custom security handler
|
|
133
|
-
try {
|
|
134
|
-
console.log('-------------------------------------------------');
|
|
135
|
-
console.log('Encrypt a document using PDFTron Custom Security handler with a custom id and password...');
|
|
136
|
-
const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + "BusinessCardTemplate.pdf");
|
|
137
|
-
|
|
138
|
-
// Create PDFTron custom security handler with a custom id. Replace this with your own integer
|
|
139
|
-
const custom_id = 123456789;
|
|
140
|
-
const custom_handler = await PDFNet.PDFTronCustomSecurityHandler.create(custom_id);
|
|
141
|
-
|
|
142
|
-
// Add a password to the custom security handler
|
|
143
|
-
const pass = 'test';
|
|
144
|
-
await custom_handler.changeUserPasswordUString(pass);
|
|
145
|
-
|
|
146
|
-
// Save the encrypted document
|
|
147
|
-
doc.setSecurityHandler(custom_handler);
|
|
148
|
-
await doc.save(outputPath + 'BusinessCardTemplate_enc.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
|
|
149
|
-
|
|
150
|
-
console.log('Decrypt the PDFTron custom security encrypted document above...');
|
|
151
|
-
// Register the PDFTron Custom Security handler with the same custom id used in encryption
|
|
152
|
-
await PDFNet.addPDFTronCustomHandler(custom_id);
|
|
153
|
-
|
|
154
|
-
const doc_enc = await PDFNet.PDFDoc.createFromFilePath(outputPath + 'BusinessCardTemplate_enc.pdf');
|
|
155
|
-
doc_enc.initStdSecurityHandlerUString(pass);
|
|
156
|
-
doc_enc.removeSecurity();
|
|
157
|
-
// Save the decrypted document
|
|
158
|
-
await doc_enc.save(outputPath + 'BusinessCardTemplate_enc_dec.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
|
|
159
|
-
console.log('Done. Result saved in BusinessCardTemplate_enc_dec.pdf');
|
|
160
|
-
} catch (err) {
|
|
161
|
-
console.log(err.stack);
|
|
162
|
-
ret = 1;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
console.log('-------------------------------------------------');
|
|
166
|
-
console.log('Tests completed.');
|
|
167
|
-
|
|
168
|
-
return ret;
|
|
169
|
-
};
|
|
170
|
-
|
|
171
|
-
PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function (error) { console.log('Error: ' + JSON.stringify(error)); }).then(function () { return PDFNet.shutdown(); });
|
|
172
|
-
};
|
|
173
|
-
exports.runEncTest();
|
|
174
|
-
})(exports);
|
|
175
|
-
// eslint-disable-next-line spaced-comment
|
|
1
|
+
//---------------------------------------------------------------------------------------
|
|
2
|
+
// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
|
|
3
|
+
// Consult legal.txt regarding legal and license information.
|
|
4
|
+
//---------------------------------------------------------------------------------------
|
|
5
|
+
|
|
6
|
+
//---------------------------------------------------------------------------------------
|
|
7
|
+
// This sample shows encryption support in PDFNet. The sample reads an encrypted document and
|
|
8
|
+
// sets a new SecurityHandler. The sample also illustrates how password protection can
|
|
9
|
+
// be removed from an existing PDF document.
|
|
10
|
+
//---------------------------------------------------------------------------------------
|
|
11
|
+
const { PDFNet } = require('@pdftron/pdfnet-node');
|
|
12
|
+
const PDFTronLicense = require('../LicenseKey/LicenseKey');
|
|
13
|
+
|
|
14
|
+
((exports) => {
|
|
15
|
+
exports.runEncTest = () => {
|
|
16
|
+
|
|
17
|
+
const main = async () => {
|
|
18
|
+
let ret = 0;
|
|
19
|
+
// Relative path to the folder containing test files.
|
|
20
|
+
const inputPath = '../TestFiles/';
|
|
21
|
+
const outputPath = inputPath + 'Output/';
|
|
22
|
+
// Example 1:
|
|
23
|
+
// secure a PDF document with password protection and adjust permissions
|
|
24
|
+
try {
|
|
25
|
+
// Open the test file
|
|
26
|
+
console.log('-------------------------------------------------/nSecuring an existing document...');
|
|
27
|
+
const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'fish.pdf');
|
|
28
|
+
if (!(await doc.initSecurityHandler())) {
|
|
29
|
+
console.log('Document authentication error...');
|
|
30
|
+
ret = 1;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const performOperation = true; // optional parameter
|
|
34
|
+
|
|
35
|
+
// Perform some operation on the document. In this case we use low level SDF API
|
|
36
|
+
// to replace the content stream of the first page with contents of file 'my_stream.txt'
|
|
37
|
+
// Results in fish.pdf becoming a pair of feathers.
|
|
38
|
+
if (performOperation) {
|
|
39
|
+
console.log('Replacing the content stream, use Flate compression...');
|
|
40
|
+
// Get the page dictionary using the following path: trailer/Root/Pages/Kids/0
|
|
41
|
+
const pageTrailer = await doc.getTrailer();
|
|
42
|
+
const pageRoot = await pageTrailer.get('Root');
|
|
43
|
+
const pageRootValue = await pageRoot.value();
|
|
44
|
+
const pages = await pageRootValue.get('Pages');
|
|
45
|
+
const pagesVal = await pages.value();
|
|
46
|
+
const kids = await pagesVal.get('Kids');
|
|
47
|
+
const kidsVal = await kids.value();
|
|
48
|
+
const pageDict = await kidsVal.getAt(0);
|
|
49
|
+
|
|
50
|
+
const embedFile = await PDFNet.Filter.createMappedFileFromUString(inputPath + 'my_stream.txt');
|
|
51
|
+
const mystm = await PDFNet.FilterReader.create(embedFile);
|
|
52
|
+
|
|
53
|
+
const flateEncode = await PDFNet.Filter.createFlateEncode();
|
|
54
|
+
|
|
55
|
+
const indStream = await doc.createIndirectStreamFromFilter(mystm, flateEncode);
|
|
56
|
+
await pageDict.put('Contents', indStream);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Encrypt the document
|
|
60
|
+
// Apply a new security handler with given security settings.
|
|
61
|
+
// In order to open saved PDF you will need a user password 'test'.
|
|
62
|
+
const newHandler = await PDFNet.SecurityHandler.createDefault();
|
|
63
|
+
|
|
64
|
+
// Set a new password required to open a document
|
|
65
|
+
newHandler.changeUserPasswordUString('test');
|
|
66
|
+
|
|
67
|
+
// Set Permissions
|
|
68
|
+
newHandler.setPermission(PDFNet.SecurityHandler.Permission.e_print, true);
|
|
69
|
+
await newHandler.setPermission(PDFNet.SecurityHandler.Permission.e_extract_content, false);
|
|
70
|
+
|
|
71
|
+
// Note: document takes the ownership of newHandler.
|
|
72
|
+
doc.setSecurityHandler(newHandler);
|
|
73
|
+
|
|
74
|
+
// Save the changes
|
|
75
|
+
console.log('Saving modified file...');
|
|
76
|
+
await doc.save(outputPath + 'secured.pdf', 0);
|
|
77
|
+
console.log('Done. Result saved in secured.pdf');
|
|
78
|
+
} catch (err) {
|
|
79
|
+
console.log(err);
|
|
80
|
+
console.log(err.stack);
|
|
81
|
+
ret = 1;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Example 2:
|
|
85
|
+
// Opens an encrypted PDF document and removes its security.
|
|
86
|
+
try {
|
|
87
|
+
console.log('-------------------------------------------------');
|
|
88
|
+
console.log('Open the password protected document from the first example...');
|
|
89
|
+
const securedDoc = await PDFNet.PDFDoc.createFromFilePath(outputPath + 'secured.pdf');
|
|
90
|
+
console.log('Initializing security handler without any user interaction...');
|
|
91
|
+
|
|
92
|
+
// At this point MySecurityHandler callbacks will be invoked.
|
|
93
|
+
// MySecurityHandler.GetAuthorizationData() should collect the password and
|
|
94
|
+
// AuthorizeFailed() is called if user repeatedly enters a wrong password.
|
|
95
|
+
if (!(await securedDoc.initStdSecurityHandlerUString('test'))) {
|
|
96
|
+
console.log('Document authentication error.../nThe password is not valid.');
|
|
97
|
+
ret = 1;
|
|
98
|
+
return ret;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
console.log('The password is correct! Document can now be used for reading and editing');
|
|
102
|
+
|
|
103
|
+
// Remove the password security and save the changes to a new file.
|
|
104
|
+
securedDoc.removeSecurity();
|
|
105
|
+
await securedDoc.save(outputPath + 'secured_nomore1.pdf', 0);
|
|
106
|
+
console.log('Done. Result saved in secured_nomore1.pdf');
|
|
107
|
+
|
|
108
|
+
/*
|
|
109
|
+
const hdlr = await securedDoc.getSecurityHandler();
|
|
110
|
+
|
|
111
|
+
console.log('Document Open Password: ' + (await hdlr.isUserPasswordRequired()));
|
|
112
|
+
console.log('Permissions Password: ' + (await hdlr.isMasterPasswordRequired()));
|
|
113
|
+
console.log('Permissions: ');
|
|
114
|
+
console.log("\tHas 'owner' permissions: " + (await hdlr.getPermission(PDFNet.SecurityHandler.Permission.e_owner)));
|
|
115
|
+
|
|
116
|
+
console.log('\tOpen and decrypt the document: ' + (await hdlr.getPermission(PDFNet.SecurityHandler.Permission.e_doc_open)));
|
|
117
|
+
console.log('\tAllow content extraction: ' + (await hdlr.getPermission(PDFNet.SecurityHandler.Permission.e_extract_content)));
|
|
118
|
+
console.log('\tAllow full document editing: ' + (await hdlr.getPermission(PDFNet.SecurityHandler.Permission.e_doc_modify)));
|
|
119
|
+
console.log('\tAllow printing: ' + (await hdlr.getPermission(PDFNet.SecurityHandler.Permission.e_print)));
|
|
120
|
+
console.log('\tAllow high resolution printing: ' + (await hdlr.getPermission(PDFNet.SecurityHandler.Permission.e_print_high)));
|
|
121
|
+
console.log('\tAllow annotation editing: ' + (await hdlr.getPermission(PDFNet.SecurityHandler.Permission.e_mod_annot)));
|
|
122
|
+
console.log('\tAllow form fill: ' + (await hdlr.getPermission(PDFNet.SecurityHandler.Permission.e_fill_forms)));
|
|
123
|
+
console.log('\tAllow content extraction for accessibility: ' + (await hdlr.getPermission(PDFNet.SecurityHandler.Permission.e_access_support)));
|
|
124
|
+
console.log('\tAllow document assembly: ' + (await hdlr.getPermission(PDFNet.SecurityHandler.Permission.e_assemble_doc)));
|
|
125
|
+
*/
|
|
126
|
+
} catch (err) {
|
|
127
|
+
console.log(err.stack);
|
|
128
|
+
ret = 1;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Example 3:
|
|
132
|
+
// Encrypt/Decrypt a PDF using PDFTron custom security handler
|
|
133
|
+
try {
|
|
134
|
+
console.log('-------------------------------------------------');
|
|
135
|
+
console.log('Encrypt a document using PDFTron Custom Security handler with a custom id and password...');
|
|
136
|
+
const doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + "BusinessCardTemplate.pdf");
|
|
137
|
+
|
|
138
|
+
// Create PDFTron custom security handler with a custom id. Replace this with your own integer
|
|
139
|
+
const custom_id = 123456789;
|
|
140
|
+
const custom_handler = await PDFNet.PDFTronCustomSecurityHandler.create(custom_id);
|
|
141
|
+
|
|
142
|
+
// Add a password to the custom security handler
|
|
143
|
+
const pass = 'test';
|
|
144
|
+
await custom_handler.changeUserPasswordUString(pass);
|
|
145
|
+
|
|
146
|
+
// Save the encrypted document
|
|
147
|
+
doc.setSecurityHandler(custom_handler);
|
|
148
|
+
await doc.save(outputPath + 'BusinessCardTemplate_enc.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
|
|
149
|
+
|
|
150
|
+
console.log('Decrypt the PDFTron custom security encrypted document above...');
|
|
151
|
+
// Register the PDFTron Custom Security handler with the same custom id used in encryption
|
|
152
|
+
await PDFNet.addPDFTronCustomHandler(custom_id);
|
|
153
|
+
|
|
154
|
+
const doc_enc = await PDFNet.PDFDoc.createFromFilePath(outputPath + 'BusinessCardTemplate_enc.pdf');
|
|
155
|
+
doc_enc.initStdSecurityHandlerUString(pass);
|
|
156
|
+
doc_enc.removeSecurity();
|
|
157
|
+
// Save the decrypted document
|
|
158
|
+
await doc_enc.save(outputPath + 'BusinessCardTemplate_enc_dec.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
|
|
159
|
+
console.log('Done. Result saved in BusinessCardTemplate_enc_dec.pdf');
|
|
160
|
+
} catch (err) {
|
|
161
|
+
console.log(err.stack);
|
|
162
|
+
ret = 1;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
console.log('-------------------------------------------------');
|
|
166
|
+
console.log('Tests completed.');
|
|
167
|
+
|
|
168
|
+
return ret;
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function (error) { console.log('Error: ' + JSON.stringify(error)); }).then(function () { return PDFNet.shutdown(); });
|
|
172
|
+
};
|
|
173
|
+
exports.runEncTest();
|
|
174
|
+
})(exports);
|
|
175
|
+
// eslint-disable-next-line spaced-comment
|
|
176
176
|
//# sourceURL=EncTest.js
|