Sample JavaScript code for editing an existing PDF document at the object level by using the Apryse SDK Cos/SDF low-level API. Learn more about our Web SDK and PDF Editing & Manipulation Library.
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
8
9
10
11
12
13 exports.runSDFTest = () => {
14 const PDFNet = exports.Core.PDFNet;
15
16 const main = async () => {
17 console.log('Beginning SDF Test.');
18 const inputURL = '../TestFiles/';
19
20 try {
21 // Here we create a SDF/Cos document directly from PDF file. In case you have
22 // PDFDoc you can always access SDF/Cos document using PDFDoc.GetSDFDoc() method.
23 const docorig = await PDFNet.PDFDoc.createFromURL(inputURL + 'fish.pdf');
24 const doc = await docorig.getSDFDoc();
25 doc.initSecurityHandler();
26 doc.lock();
27 console.log('Modifying into dictionary, adding custom properties, embedding a stream...');
28
29 const trailer = await doc.getTrailer(); // Get the trailer
30
31 // Now we will change PDF document information properties using SDF API
32
33 // Get the Info dictionary.
34
35 let itr = await trailer.find('Info');
36 let info;
37 if (await itr.hasNext()) {
38 info = await itr.value();
39 // Modify 'Producer' entry.
40 info.putString('Producer', 'PDFTron PDFNet');
41
42 // read title entry if it is present
43 itr = await info.find('Author');
44 if (await itr.hasNext()) {
45 const itrval = await itr.value();
46 const oldstr = await itrval.getAsPDFText();
47 info.putText('Author', oldstr + ' - Modified');
48 } else {
49 info.putString('Author', 'Me, myself, and I');
50 }
51 } else {
52 // Info dict is missing.
53 info = await trailer.putDict('Info');
54 info.putString('Producer', 'PDFTron PDFNet');
55 info.putString('Title', 'My document');
56 }
57
58 // Create a custom inline dictionary within Infor dictionary
59 const customDict = await info.putDict('My Direct Dict');
60 customDict.putNumber('My Number', 100); // Add some key/value pairs
61 customDict.putArray('My Array');
62
63 // Create a custom indirect array within Info dictionary
64 const customArray = await doc.createIndirectArray();
65 info.put('My Indirect Array', customArray); // Add some entries
66
67 // create indirect link to root
68 const trailerRoot = await trailer.get('Root');
69 customArray.pushBack(await trailerRoot.value());
70
71 // Embed a custom stream (file mystream.txt).
72 const embedFile = await PDFNet.Filter.createURLFilter(inputURL + 'my_stream.txt');
73 const mystm = await PDFNet.FilterReader.create(embedFile);
74 const indStream = await doc.createIndirectStreamFromFilter(mystm);
75 customArray.pushBack(indStream);
76
77 const docbuf = await doc.saveMemory(0, '%PDF-1.4'); // PDFNet.SDFDoc.SaveOptions.e_remove_unused
78 saveBufferAsPDFDoc(docbuf, 'sdftest_out.pdf');
79 console.log('Done.');
80 } catch (err) {
81 console.log(err);
82 }
83 };
84 // add your own license key as the second parameter, e.g. PDFNet.runWithCleanup(main, 'YOUR_LICENSE_KEY')
85 PDFNet.runWithCleanup(main);
86 };
87})(window);
88// eslint-disable-next-line spaced-comment
89//# sourceURL=SDFTest.js
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales