Write page content to PDF using JavaScript

To write page content to a new document.

1WebViewer({ fullAPI: true})
2 .then(instance => {
3 const { PDFNet } = instance.Core;
4 await PDFNet.initialize();
5 const doc = await PDFNet.PDFDoc.create();
6
7 // ElementBuilder is used to build new Element objects
8 const builder = await PDFNet.ElementBuilder.create();
9
10 // ElementWriter is used to write Elements to the page
11 const writer = await PDFNet.ElementWriter.create();
12
13 // Start a new page
14 const pageRect = await PDFNet.Rect.init(0, 0, 612, 794);
15 let page = await doc.pageCreate(pageRect);
16
17 // begin writing to the page
18 writer.beginOnPage(page);
19
20 // Create an Image that can be reused multiple times in the document or multiple on the same page.
21 const img = await PDFNet.Image.createFromURL(doc, imagename);
22 let element = await builder.createImageFromMatrix(img, await PDFNet.Matrix2D.create(200, -145, 20, 300, 200, 150));
23 writer.writePlacedElement(element);
24
25 // use the same image (just change its matrix)
26 let gstate = await element.getGState();
27 gstate.setTransform(200, 0, 0, 300, 50, 450);
28 writer.writePlacedElement(element);
29
30 // use the same image again (just change its matrix).
31 writer.writePlacedElement(await builder.createImageScaled(img, 300, 600, 200, -150));
32
33 // save changes to the current page
34 writer.end();
35
36 // Add the new page to the document sequence
37 doc.pagePushBack(page);
38 })

Build, Write, Embed Elements in PDF Pages
Full code sample which illustrates how to use PDFNet page writing API, how to embed fonts and images and how to copy graphical elements from one page to another.

About writing page content

New page content can be added to an existing page or a blank new page using ElementBuilder and ElementWriter. ElementBuilder is used to instantiate one or more Elements that can be written to one or more pages using ElementWriter:

Apryse Docs Image

Note that once the Element is instantiated using ElementBuilder, you have full control over its properties and graphics state.

Page content can also come from existing pages. For example, you can use ElementReader to read paths, text, and images from existing pages and copy them to the current page. Note that, along the way, you can fully modify an Element's properties or its graphics state. This is how to perform page content editing. For example, the following copies all Elements from an existing page and changes text color to blue:

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 firstPage = 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 case PDFNet.Element.Type.e_text:
18 // Set all text to blue
19 gs = await element.getGState();
20 gs.setFillColorSpace(colorspace);
21 gs.setFillColorWithColorPt(blueColor);
22 writer.writeElement(element);
23 break;
24 default:
25 writer.writeElement(element);
26 }
27 }
28
29 writer.end();
30 reader.end();
31 })

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