1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6(exports => {
7 exports.runSDFTest = () => {
8 const PDFNet = exports.Core.PDFNet;
9
10 const main = async () => {
11 console.log('Beginning SDF Test.');
12 const inputURL = '../TestFiles/';
13
14 try {
15 // Here we create a SDF/Cos document directly from PDF file. In case you have
16 // PDFDoc you can always access SDF/Cos document using PDFDoc.GetSDFDoc() method.
17 const docorig = await PDFNet.PDFDoc.createFromURL(inputURL + 'fish.pdf');
18 const doc = await docorig.getSDFDoc();
19 doc.initSecurityHandler();
20 doc.lock();
21 console.log('Modifying into dictionary, adding custom properties, embedding a stream...');
22
23 const trailer = await doc.getTrailer(); // Get the trailer
24
25 // Now we will change PDF document information properties using SDF API
26
27 // Get the Info dictionary.
28
29 let itr = await trailer.find('Info');
30 let info;
31 if (await itr.hasNext()) {
32 info = await itr.value();
33 // Modify 'Producer' entry.
34 info.putString('Producer', 'PDFTron PDFNet');
35
36 // read title entry if it is present
37 itr = await info.find('Author');
38 if (await itr.hasNext()) {
39 const itrval = await itr.value();
40 const oldstr = await itrval.getAsPDFText();
41 info.putText('Author', oldstr + ' - Modified');
42 } else {
43 info.putString('Author', 'Me, myself, and I');
44 }
45 } else {
46 // Info dict is missing.
47 info = await trailer.putDict('Info');
48 info.putString('Producer', 'PDFTron PDFNet');
49 info.putString('Title', 'My document');
50 }
51
52 // Create a custom inline dictionary within Infor dictionary
53 const customDict = await info.putDict('My Direct Dict');
54 customDict.putNumber('My Number', 100); // Add some key/value pairs
55 customDict.putArray('My Array');
56
57 // Create a custom indirect array within Info dictionary
58 const customArray = await doc.createIndirectArray();
59 info.put('My Indirect Array', customArray); // Add some entries
60
61 // create indirect link to root
62 const trailerRoot = await trailer.get('Root');
63 customArray.pushBack(await trailerRoot.value());
64
65 // Embed a custom stream (file mystream.txt).
66 const embedFile = await PDFNet.Filter.createURLFilter(inputURL + 'my_stream.txt');
67 const mystm = await PDFNet.FilterReader.create(embedFile);
68 const indStream = await doc.createIndirectStreamFromFilter(mystm);
69 customArray.pushBack(indStream);
70
71 const docbuf = await doc.saveMemory(0, '%PDF-1.4'); // PDFNet.SDFDoc.SaveOptions.e_remove_unused
72 saveBufferAsPDFDoc(docbuf, 'sdftest_out.pdf');
73 console.log('Done.');
74 } catch (err) {
75 console.log(err);
76 }
77 };
78 // add your own license key as the second parameter, e.g. PDFNet.runWithCleanup(main, 'YOUR_LICENSE_KEY')
79 PDFNet.runWithCleanup(main);
80 };
81})(window);
82// eslint-disable-next-line spaced-comment
83//# sourceURL=SDFTest.js