PDFs With Password

Script for Adobe Illustrator

Batch convert art and images to password-protected PDFs.

  • Creates PDFs from vector art or raster images
  • Variation available to disallow print, edit, or copy
  • Adapt open source to customize or create other scripts
Download
PDFs With Password

Download
PDFs No Print Or Edit
Help me keep making new scripts by supporting my work. Click the PayPal button to contribute any amount you choose. Thank you. William Campbell

How-to Video

How to use the script

Choose a folder of Illustrator files or raster images, and choose a folder to which PDFs are output. Select a PDF preset, and enter the desired password. Click the OK button to proceed. The original version of the script applies the password entered to each PDF saved to the output folder. Without the password, the PDFs cannot be opened and viewed. A variation of the script has security options set to allow viewing, but prevent printing, editing, or copying any content of the PDFs.

Input Folder — select the desired folder to process, which may contain Illustrator artwork or raster images. The script recognizes the extensions ai, eps, gif, jpg, jpeg, pdf, png, psd, tif, and tiff. The script may be edited to recognize others.

Output Folder — the folder to which PDFs are saved.

PDF preset — the PDF preset to use when saving PDFs.

Password — the password required to open the PDFs output by the script, or for the variation of the script, password required to enable print, edit, or copy.

Multi-page PDF

The script saves a separate PDF for each input file. To have all in a single PDF file, use Acrobat to combine the PDFs. However, be sure in the script to leave the password input blank. Otherwise the password must be entered repeatedly, once for every single-page PDFs as they are combined, and once combined, the result does not inherit the prior security anyway. Save yourself the headache and leave the password blank. Then combine PDFs in Acrobat. Go to the File menu, Create, and select Combine Files into a Single PDF. In addition, Windows users may select the desired PDFs in File Explorer, right-click, and choose Combine files in Acrobat. Then use Acrobat to set the desired security options and password. Go to the File menu, Properties, and select the Security tab. Change the Security Method to Password Security. In the dialog window that opens, set the security options and password as desired, which are then in force once the PDF is saved.

Source code

(download button below)

/*

PDFs With Password
Copyright 2022 William Campbell
All Rights Reserved
https://www.marspremedia.com/contact

Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

*/

(function () {

    var title = "PDFs With Password";

    if (!/illustrator/i.test(app.name)) {
        alert("Script for Illustrator", title, false);
        return;
    }

    // Script variables.
    var doneMessage;
    var error;
    var folderInput;
    var folderOutput;
    var pdfPreset;
    var progress;

    // Reusable UI variables.
    var g; // group
    var p; // panel
    var w; // window

    // Permanent UI variables.
    var btnCancel;
    var btnFolderInput;
    var btnFolderOutput;
    var btnOk;
    var inpPassword;
    var listPdfPreset;
    var txtFolderInput;
    var txtFolderOutput;

    // CREATE PROGRESS WINDOW

    progress = new Window("palette", "Progress", undefined, {
        "closeButton": false
    });
    progress.t = progress.add("statictext");
    progress.t.preferredSize.width = 450;
    progress.b = progress.add("progressbar");
    progress.b.preferredSize.width = 450;
    progress.display = function (message) {
        message && (this.t.text = message);
        this.show();
        this.update();
    };
    progress.increment = function () {
        this.b.value++;
    };
    progress.set = function (steps) {
        this.b.value = 0;
        this.b.minvalue = 0;
        this.b.maxvalue = steps;
    };

    // CREATE USER INTERFACE

    w = new Window("dialog", title);
    w.alignChildren = "fill";

    // Panel 'Input'
    p = w.add("panel", undefined, "Input");
    p.margins = [24, 18, 18, 18];
    g = p.add("group");
    btnFolderInput = g.add("button", undefined, "Folder...");
    txtFolderInput = g.add("statictext", undefined, "", {
        truncate: "middle"
    });
    txtFolderInput.preferredSize.width = 350;

    // Panel 'Output'
    p = w.add("panel", undefined, "Output");
    p.margins = [24, 18, 18, 18];
    g = p.add("group");
    btnFolderOutput = g.add("button", undefined, "Folder...");
    txtFolderOutput = g.add("statictext", undefined, "", {
        truncate: "middle"
    });
    txtFolderOutput.preferredSize.width = 350;

    // Panel 'Options'
    p = w.add("panel", undefined, "Options");
    p.margins = [24, 18, 18, 18];
    g = p.add("group");
    g.alignment = "left";
    g.add("statictext", undefined, "PDF preset:");
    listPdfPreset = g.add("dropdownlist", undefined, app.PDFPresetsList);
    g = p.add("group");
    g.alignment = "left";
    g.margins = [7, 0, 0, 0];
    g.add("statictext", undefined, "Password:");
    inpPassword = g.add("edittext");
    inpPassword.preferredSize.width = 195;

    // Action buttons
    g = w.add("group");
    g.alignment = "center";
    btnOk = g.add("button", undefined, "OK");
    btnCancel = g.add("button", undefined, "Cancel");

    // Panel Copyright
    p = w.add("panel");
    p.add("statictext", undefined, "Copyright 2022 William Campbell");

    // UI EVENT HANDLERS

    btnFolderInput.onClick = function () {
        var f = Folder.selectDialog("Select input folder", txtFolderInput.text);
        if (f) {
            txtFolderInput.text = Folder.decode(f.fullName);
        }
    };

    btnFolderOutput.onClick = function () {
        var f = Folder.selectDialog("Select output folder", txtFolderOutput.text);
        if (f) {
            txtFolderOutput.text = Folder.decode(f.fullName);
        }
    };

    btnOk.onClick = function () {
        folderInput = new Folder(txtFolderInput.text);
        if (!(folderInput && folderInput.exists)) {
            txtFolderInput.text = "";
            alert("Select input folder", " ", false);
            return;
        }
        folderOutput = new Folder(txtFolderOutput.text);
        if (!(folderOutput && folderOutput.exists)) {
            txtFolderOutput.text = "";
            alert("Select output folder", " ", false);
            return;
        }
        if (!listPdfPreset.selection) {
            alert("Select a PDF preset", " ", false);
            return;
        }
        w.close(1);
    };

    btnCancel.onClick = function () {
        w.close(0);
    };

    // SHOW THE WINDOW

    if (w.show() == 1) {
        doneMessage = "";
        try {
            process();
            doneMessage = doneMessage || "Done";
        } catch (e) {
            error = error || e;
            doneMessage = "An error has occurred.\nLine " + error.line + ": " + error.message;
        }
        progress.close();
        doneMessage && alert(doneMessage, title, error);
    }

    //====================================================================
    //               END PROGRAM EXECUTION, BEGIN FUNCTIONS
    //====================================================================

    function process() {
        var files;
        var i;
        // Ignore messages when opening documents.
        app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
        progress.display("Reading folder...");
        try {
            // Get files in folder.
            files = folderInput.getFiles(function (f) {
                if (f instanceof File && !f.hidden) {
                    if (/\.ai|eps|gif|jpg|jpeg|pdf|png|psd|tif|tiff$/i.test(f.name)) {
                        return true;
                    }
                }
                return false;
            });
            if (!files.length) {
                doneMessage = "No files found in selected folder";
                return;
            }
            progress.set(files.length);
            // Get PDF preset to use.
            pdfPreset = listPdfPreset.selection.text;
            // Loop through files array.
            for (i = 0; i < files.length; i++) {
                progress.increment();
                progress.display(File.decode(files[i].name));
                processFile(files[i]);
            }
        } finally {
            app.userInteractionLevel = UserInteractionLevel.DISPLAYALERTS;
        }
    }

    function processFile(file) {
        var doc;
        var filePdf;
        var saveOptions;
        doc = app.open(file);
        try {
            // IS INPUT FILE AN IMAGE?
            // If so, set artboard to its size.
            // PSD the artboard is set correctly so don't include here.
            if (/\.gif|jpg|jpeg|png|tif|tiff$/i.test(file.name)) {
                doc.artboards[0].artboardRect = doc.pageItems[0].geometricBounds;
            }
            if (/\.eps$/i.test(file.name)) {
                // EPS could be raster or vector.
                // Raster will be one group with one image.
                if (doc.groupItems.length == 1) {
                    if (doc.groupItems[0].rasterItems.length == 1) {
                        doc.artboards[0].artboardRect = doc.groupItems[0].rasterItems[0].geometricBounds;
                    }
                }
            }

            // SAVE PDF

            filePdf = new File(folderOutput + "/" + file.name.replace(/\.[^\.]*$/, "") + ".pdf");
            saveOptions = new PDFSaveOptions();
            saveOptions.pDFPreset = pdfPreset;
            // Above sets most options from the selected PDF preset.
            // Here are some other properties. Set as desired.
            saveOptions.preserveEditability = false;
            saveOptions.viewAfterSaving = false;

            // SECURITY OPTIONS

            if (inpPassword.text) {
                // Require a password to open the document.
                saveOptions.requireDocumentPassword = true;
                // A password string to open the document.
                // *** Uncomment if above is 'true' Comment to disable if above is 'false'
                saveOptions.documentPassword = inpPassword.text;
            }

            // ================= IMPORTANT NOTE ==================
            // When using 'document password' (above),
            // the remaining properties have no effect.
            // To instead use 'permission password' (below),
            // enable it and disable document password (above).
            // Then the remaining options are applied to the PDF.
            // ====================================================

            if (inpPassword.text) {
                // Use a password to restrict editing security settings.
                saveOptions.requirePermissionPassword = false;
                // A password string to restrict editing security settings.
                // *** Uncomment if above is 'true'; Comment to disable if above is 'false'
                // saveOptions.permissionPassword = inpPassword.text;
            }

            // PDF security printing permission.
            // *** Uncomment desired choice, leave others commented to
            // *** disable, OR comment all to ignore and use default.
            // *** To disallow printing, choose 'PRINT128NONE'
            // *** (choices 40 vs 128 is the bits encryption; use 128)
            saveOptions.pDFAllowPrinting = PDFPrintAllowedEnum.PRINT128HIGHRESOLUTION;
            // saveOptions.pDFAllowPrinting = PDFPrintAllowedEnum.PRINT128LOWRESOLUTION
            // saveOptions.pDFAllowPrinting = PDFPrintAllowedEnum.PRINT128NONE
            // saveOptions.pDFAllowPrinting = PDFPrintAllowedEnum.PRINT40HIGHRESOLUTION
            // saveOptions.pDFAllowPrinting = PDFPrintAllowedEnum.PRINT40NONE

            // PDF security changes allowed.
            // *** Uncomment desired choice, leave others commented to
            // *** disable, OR comment all to ignore and use default.
            // *** To disallow changes, choose 'CHANGE128NONE'
            // *** (choices 40 vs 128 is the bits encryption; use 128)
            saveOptions.pDFChangesAllowed = PDFChangesAllowedEnum.CHANGE128ANYCHANGES;
            // saveOptions.pDFChangesAllowed = PDFChangesAllowedEnum.CHANGE128COMMENTING
            // saveOptions.pDFChangesAllowed = PDFChangesAllowedEnum.CHANGE128EDITPAGE
            // saveOptions.pDFChangesAllowed = PDFChangesAllowedEnum.CHANGE128FILLFORM
            // saveOptions.pDFChangesAllowed = PDFChangesAllowedEnum.CHANGE128NONE
            // saveOptions.pDFChangesAllowed = PDFChangesAllowedEnum.CHANGE40ANYCHANGES
            // saveOptions.pDFChangesAllowed = PDFChangesAllowedEnum.CHANGE40COMMENTING
            // saveOptions.pDFChangesAllowed = PDFChangesAllowedEnum.CHANGE40NONE
            // saveOptions.pDFChangesAllowed = PDFChangesAllowedEnum.CHANGE40PAGELAYOUT

            // *** Remaining properties are optional. Set true or false
            // *** as desired, or comment any to ignore and use default.
            // Enable copying of text 128-bit.
            saveOptions.enableCopy = true;
            // Enable accessing 128-bit.
            saveOptions.enableAccess = true;

            // SAVE AS PDF
            doc.saveAs(filePdf, saveOptions);
        } finally {
            doc.close(SaveOptions.DONOTSAVECHANGES);
        }
    }

})();
Help me keep making new scripts by supporting my work. Click the PayPal button to contribute any amount you choose. Thank you. William Campbell
Download
PDFs With Password
Download
PDFs No Print Or Edit

For help installing scripts, see How to Install and Use Scripts in Adobe Creative Cloud Applications.

IMPORTANT: scripts are developed for the latest Adobe Creative Cloud applications. Many scripts work in CC 2018 and later, even some as far back as CS6, but may not perform as expected, or run at all, when used in versions prior to 2018. Photoshop features Select Subject and Preserve Details 2.0 definitely fail prior to CC 2018 (version 19) as the features do not exist in earlier versions. For best results use the latest versions of Adobe Creative Cloud applications.

IMPORTANT: by downloading any of the scripts on this page you agree that the software is provided without any warranty, express or implied. USE AT YOUR OWN RISK. Always make backups of important data.

IMPORTANT: fees paid for software products are the purchase of a non-exclusive license to use the software product and do not grant the purchaser any degree of ownership of the software code. Author of the intellectual property and copyright holder William Campbell retains 100% ownership of all code used in all software products regardless of the inspiration for the software product design or functionality.