ElementEdit

Sample JavaScript code for using Apryse SDK to programmatically edit an existing PDF document's page display list and the graphics state attributes on existing elements. In particular, this sample strips all images from the page and changes the text color to blue. You can also build a GUI with interactive PDF editor widgets. Some of Apryse SDK's other functions for programmatically editing PDFs include the Cos/SDF low-level API, page manipulation, and more. 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
14 exports.runElementEditTest = () => {
15 const PDFNet = exports.Core.PDFNet;
16
17 async function ProcessElements(reader, writer, visited) {
18 await PDFNet.startDeallocateStack();
19 const colorspace = await PDFNet.ColorSpace.createDeviceRGB();
20 const redColor = await PDFNet.ColorPt.init(1, 0, 0, 0);
21 const blueColor = await PDFNet.ColorPt.init(0, 0, 1, 0);
22
23 for (let element = await reader.next(); element !== null; element = await reader.next()) {
24 const elementType = await element.getType();
25 let gs;
26 let formObj;
27 let formObjNum = null;
28 switch (elementType) {
29 case PDFNet.Element.Type.e_image:
30 case PDFNet.Element.Type.e_inline_image:
31 // remove all images by skipping them
32 break;
33 case PDFNet.Element.Type.e_path:
34 // Set all paths to red
35 gs = await element.getGState();
36 gs.setFillColorSpace(colorspace);
37 gs.setFillColorWithColorPt(redColor);
38 writer.writeElement(element);
39 break;
40 case PDFNet.Element.Type.e_text:
41 // Set all text to blue
42 gs = await element.getGState();
43 gs.setFillColorSpace(colorspace);
44 gs.setFillColorWithColorPt(blueColor);
45 writer.writeElement(element);
46 break;
47 case PDFNet.Element.Type.e_form:
48 writer.writeElement(element);
49 formObj = await element.getXObject();
50 formObjNum = formObj.getObjNum();
51 // if XObject not yet processed
52 if (visited.indexOf(formObjNum) === -1) {
53 // Set Replacement
54 const insertedObj = await formObj.getObjNum();
55 if (!visited.includes(insertedObj)) {
56 visited.push(insertedObj);
57 }
58 const newWriter = await PDFNet.ElementWriter.create();
59 reader.formBegin();
60 newWriter.beginOnObj(formObj, true);
61 await ProcessElements(reader, newWriter, visited);
62 newWriter.end();
63 reader.end();
64 }
65 break;
66 default:
67 writer.writeElement(element);
68 }
69 }
70 await PDFNet.endDeallocateStack();
71 }
72
73 const main = async () => {
74 console.log('Beginning Test');
75 const ret = 0;
76 // Relative path to the folder containing test files.
77 const inputUrl = '../TestFiles/';
78 const doc = await PDFNet.PDFDoc.createFromURL(inputUrl + 'newsletter.pdf');
79 doc.initSecurityHandler();
80 doc.lock();
81
82 console.log('PDF document initialized and locked');
83 const writer = await PDFNet.ElementWriter.create();
84 const reader = await PDFNet.ElementReader.create();
85 const visited = [];
86
87 const totalPageNumber = await doc.getPageCount();
88
89 const itr = await doc.getPageIterator(1);
90
91 // Read every page
92 for (itr; await itr.hasNext(); itr.next()) {
93 const page = await itr.current();
94 const currentPageNumber = await page.getIndex();
95 console.log('Processing elements on page ' + currentPageNumber + '/' + totalPageNumber);
96 const sdfObj = await page.getSDFObj();
97 // Set Replacement
98 const insertedObj = await sdfObj.getObjNum();
99 if (!visited.includes(insertedObj)) {
100 visited.push(insertedObj);
101 }
102 reader.beginOnPage(page);
103 writer.beginOnPage(page, PDFNet.ElementWriter.WriteMode.e_replacement, false);
104 await ProcessElements(reader, writer, visited);
105 writer.end();
106 reader.end();
107 }
108
109 const docBuffer = await doc.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_remove_unused);
110 saveBufferAsPDFDoc(docBuffer, 'newsletter_edited.pdf');
111 console.log('Done.');
112 return ret;
113 };
114
115 // add your own license key as the second parameter, e.g. PDFNet.runWithCleanup(main, 'YOUR_LICENSE_KEY')
116 PDFNet.runWithCleanup(main);
117 };
118})(window);
119// eslint-disable-next-line spaced-comment
120//# sourceURL=ElementEditTest.js

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales