Removing images from PDF page using JavaScript

To remove all images from a document page.

1WebViewer({ fullAPI: true })
2 .then(instance => {
3 const { PDFNet } = instance.Core;
4 await PDFNet.initialize();
5 const doc = await PDFNet.PDFDoc.createFromURL(filename);
6 const page = await doc.getPage(1);
7
8 const writer = await PDFNet.ElementWriter.create();
9 const reader = await PDFNet.ElementReader.create();
10 reader.beginOnPage(page);
11 writer.beginOnPage(page, PDFNet.ElementWriter.WriteMode.e_replacement, false);
12
13 for (let element = await reader.next(); element !== null; element = await reader.next())
14 {
15 const elementType = await element.getType();
16 switch (elementType)
17 {
18 case PDFNet.Element.Type.e_image:
19 case PDFNet.Element.Type.e_inline_image:
20 // remove all images by skipping them
21 break;
22 default:
23 writer.writeElement(element);
24 break;
25 }
26 }
27
28 writer.end();
29 reader.end();
30 })

PDF Editor (Programmatic)
Full code sample which strips all images from the page and changes text color to blue.

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales