Some test text!

< Web samples

ViewerCustomSave

This sample demonstrates how to customize document save/download. In the example annotations are flattened before saving.

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

Start Trial
Sign in

Step 2: Add the code:

(function() {
  var mergeAndSave = function(doc, xfdf) {
    function* main() {
      // Import XFDF into FDF, then merge data from FDF into PDF
      // Annotations
      var fdf_doc = yield PDFNet.FDFDoc.createFromXFDF(xfdf);

      var pitr = yield doc.getPageIterator();
      for (; (yield pitr.hasNext()); pitr.next()) {
        try {
          var page = yield pitr.current();
          for (var i = (yield page.getNumAnnots()); i > 0;) {
            var annot_obj = yield page.getAnnot(--i);
            switch (yield annot_obj.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
                break;
              default:
                page.annotRemoveByIndex(i);
                break;
            }
          }
        } catch (e) {
          console.log('Error Removing Annotations: ' + e);
          (yield page.getSDFObj()).erase('Annots');
        }
      }

      doc.fdfMerge(fdf_doc);

      // run any custom logic here
      doc.flattenAnnotations();

      var docbuf = yield doc.saveMemoryBuffer(PDFNet.SDFDoc.SaveOptions.e_linearized);
      return docbuf;
    }
    // start the generator
    return PDFNet.runGeneratorWithCleanup(main());
  };

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

      am.deleteAnnotations(annotationsToRemove);
      // clear the cache
      docViewer.refreshAll();
      // update viewer with new document
      docViewer.updateView();
      // Annotations may contain text so we need to regenerate
      // our text representation
      docViewer.getDocument().refreshTextData();
      return data;
    });
  };

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