Some test text!

menu
Write contentkeyboard_arrow_down

Write page content to PDF using JavaScript

Only available with the Full API

To write page content to a new document.

WebViewer({ fullAPI: true})
  .then(instance => {
    const { PDFNet } = instance;
    await PDFNet.initialize();
    const doc = await PDFNet.PDFDoc.create();

    // ElementBuilder is used to build new Element objects
    const builder = await PDFNet.ElementBuilder.create();

    // ElementWriter is used to write Elements to the page
    const writer = await PDFNet.ElementWriter.create();

    // Start a new page
    const pageRect = await PDFNet.Rect.init(0, 0, 612, 794);
    let page = await doc.pageCreate(pageRect);

    // begin writing to the page
    writer.beginOnPage(page);

    // Create an Image that can be reused multiple times in the document or multiple on the same page.
    const img = await PDFNet.Image.createFromURL(doc, imagename);
    let element = await builder.createImageFromMatrix(img, await PDFNet.Matrix2D.create(200, -145, 20, 300, 200, 150));
    writer.writePlacedElement(element);

    // use the same image (just change its matrix)
    let gstate = await element.getGState();
    gstate.setTransform(200, 0, 0, 300, 50, 450);
    writer.writePlacedElement(element);

    // use the same image again (just change its matrix).
    writer.writePlacedElement(await builder.createImageScaled(img, 300, 600, 200, -150));

    // save changes to the current page
    writer.end();

    // Add the new page to the document sequence
    doc.pagePushBack(page);
  })

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.

linkAbout 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:

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:

WebViewer({ fullAPI: true })
  .then(instance => {
    const { PDFNet } = instance;
    await PDFNet.initialize();
    const doc = await PDFNet.PDFDoc.createFromURL(filename);
    const firstPage = await doc.getPage(1);

    const writer = await PDFNet.ElementWriter.create();
    const reader = await PDFNet.ElementReader.create();
    reader.beginOnPage(page);
    writer.beginOnPage(page, PDFNet.ElementWriter.WriteMode.e_replacement, false);

    for (let element = await reader.next(); element !== null; element = await reader.next()) 
    {
      const elementType = await element.getType();
      switch (elementType) {
        case PDFNet.Element.Type.e_text:
          // Set all text to blue
          gs = await element.getGState();
          gs.setFillColorSpace(colorspace);
          gs.setFillColorWithColorPt(blueColor);
          writer.writeElement(element);
          break;
        default:
          writer.writeElement(element);
      }
    }

    writer.end();
    reader.end();
  })

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

Get the answers you need: Support

close

Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.

Join our live demo to learn about use cases & capabilities for WebViewer

Learn more
close