How to remove JavaScript from PDFs

Q: I have a document in which I would like to remove any JavaScript actions as a way to sanitize it. How can I do it with the SDK?

A: With the SDK, its possible to traverse through the document at the SDF object level and remove any references any JavaScript actions as defined in section of the PDF specification.

Please see the attached C# code that does so.

sanitize.cs (1.65 KB)

1 Like

Is there anyway to do this with PHP SDK?

1 Like

Hi Gary,

The API for PHP is pretty much identical to C#. For reference, you can refer to the C++ API, as this is what the PHP API is based off of.

In addition, you can also refer to the SDF API PHP example for reference.

1 Like

For JavaScript:

export const removeJavascripts = async ({
}: {
  doc: PDFNet.PDFDoc;
  options?: { logScript: boolean; removeScript: boolean };
}) => {
  console.log('Removing javascripts...');

  const pageItr = await doc.getPageIterator();
  for (; await pageItr.hasNext(); await {
    const currentPage = await pageItr.current();
    const annotsDictArray = await currentPage.getAnnots();

    if (annotsDictArray) {
      const numAnnots = await currentPage.getNumAnnots();

      for (let i = 0; i < numAnnots; i++) {
        const annot = await currentPage.getAnnot(i);
        const annotIsValid = await annot.isValid();
        if (!annotIsValid) continue;

        const annotType = await annot.getType();
        if (annotType === PDFNet.Annot.Type.e_Widget) {
          const widget = await PDFNet.WidgetAnnot.createFromAnnot(annot);
          const existingAction = await widget.getAction();
          if (existingAction) {
            const type = await existingAction.getType(); // 13
            if (type === PDFNet.Action.Type.e_JavaScript) {
              const actionSdfObj = await existingAction.getSDFObj();
              try {
                const dictItr = await actionSdfObj.get('JS');
                const JS = await dictItr.value();
                const isString = await JS.isString();
                if (JS && isString) {
                  if (options && options.logScript) {
                    const script = await JS.getAsPDFText();

                  if (options && options.removeScript) {
                    await JS.setString('// removed');
              } catch (error) {
1 Like