Some test text!

< Web samples

Customize or modify a PDF before saving

This JavaScript sample lets you customize or modify a PDF, DOCX, XLSX or PPTX document before it is saved/downloaded (no servers or other external dependencies required). In this example annotations are flattened before saving the PDF. This functionality allows you to do a variety of modifications to the document before downloading – some common examples include adding stamps / watermarks to the PDF or injecting information into a document. In the case of MS Office files, modifications are saved into the PDF file.This sample works on all browsers (including IE10) and mobile devices without using plug-ins.

Step 1: Get your free trial license key, or sign in

Start Trial
Sign in

Step 2: Add the code:

((exports) => {
  const PDFNet = exports.PDFNet;
  const mergeAndSave = (doc, xfdf) => {
    const main = async() => {
      // Import XFDF into FDF, then merge data from FDF into PDF
      // Annotations
      const fdfDoc = await PDFNet.FDFDoc.createFromXFDF(xfdf);

      const pitr = await doc.getPageIterator();
      let page, annotObj;
      /* eslint no-await-in-loop: 0 */
      for (; (await pitr.hasNext()); {
        try {
          page = await pitr.current();
          for (let i = (await page.getNumAnnots()); i > 0;) {
            annotObj = await page.getAnnot(--i);
            switch (await annotObj.getType()) {
              case PDFNet.Annot.Type.e_Widget:
              case PDFNet.Annot.Type.e_Link:
              case PDFNet.Annot.Type.e_Sound:
              case PDFNet.Annot.Type.e_Movie:
              case PDFNet.Annot.Type.e_FileAttachment:
                // these are not supported for import from webviewer
        } catch (e) {
          console.log('Error Removing Annotations: ' + e);
          (await page.getSDFObj()).erase('Annots');


      // run any custom logic here

      const docbuf = await doc.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_linearized);
      return docbuf;
    // start the generator
    return PDFNet.runWithCleanup(main);

  const customDownload = (options) => {
    const docViewer = readerControl.docViewer;
    const am = docViewer.getAnnotationManager();
    const annotationsToRemove = am.getAnnotationsList();
    const currentDocument = docViewer.getDocument();
    return PDFNet.initialize().then(() => currentDocument.getPDFDoc())
      .then(pdfDoc => mergeAndSave(pdfDoc, options.xfdfString))
      .then((data) => {
        // since we are flattening annotations we should remove the existing annotations in webviewer
        // and rerender so that the file displays correctly

        // clear the cache
        // update viewer with new document
        // Annotations may contain text so we need to regenerate
        // our text representation
        return data;

  $(document).on('documentLoaded', () => {
    const doc = readerControl.docViewer.getDocument();
    doc.getFileData = customDownload;
// eslint-disable-next-line spaced-comment
//# sourceURL=config.js