Copy a PDF documents with unique, numbered watermarks
Hi everyone!
I am trying to figure out how to copy a PDF document with multiple different watermarks. I have Adobe Acrobat pro. I found a code online (copied below), but when I enter this, the code only produces a 'COPY OF THE INITIAL FILE' and 'Doc COPY 01' but it does not create the additional watermarked files. Additionally, one line is appears at the bottom of the javascript code saying 'undefined'. If anybody has any suggestions, that would be hugely appreciated.
Code:
// ———————————————————————————————————————————————————————————————————————————————————————————————————— // ———————————————————————————————————————————————————————————————————————————————————————————————————— // ———————————————————————————————————————————————————————————————————————————————————————————————————— // // C R E A T E C O P I E S O F A P D F W I T H U N I Q U E W A T E R M A R K S // // // This script prompts the user to select a PDF // file and makes copies of the file, each with a // unique watermark. The script prompts the user // to enter a list of watermark values separated by // commas. // // Up to 50 IDs can be set at a time in this variable. // Set the variable equal to text surrounded by // double quotes, with each item separated by a // comma. The following example would create 3 // copies with the watermarks: // // "COPY 01,COPY 02,COPY 03" // // ———————————————————————————————————————————————————————————————————————————————————————————————————— // ———————————————————————————————————————————————————————————————————————————————————————————————————— // ———————————————————————————————————————————————————————————————————————————————————————————————————— // ————————————————————————————————————————————————————— // ————————————————————————————————————————————————————— // HOW TO RUN JAVACRIPTS IN ACROBAT: // ————————————————————————————————————————————————————— // // 1) First turn on JavaScript in Acrobat. Go to // Edit > Preferences. Select the "JavaScript" // category. Check the boxes for "Enable // Acrobat JavaScript" and "Enable Interactive // Console". // // 2) To open the JavaScript Debugger in Acrobat, // press CTRL + J. // // 3) In the main "Console" area of the JavaScript // Debugger, paste the entire text of the // script. NOTE: Make sure any existing text // is cleared out first. // // 4) In the main "Console" area of the Javascript // Debugger, press CTRL + A to select all of // the text you just pasted into the "Console". // // 5) Press CTRL + ENTER to run the script. // Remember, it won't work unless all the text // of the script (and only that text) is // selected. // ————————————————————————————————————————————————————— // ————————————————————————————————————————————————————— // ————————————————————————————————————————————————————— // ————————————————————————————————————————————————————— // GET USER INPUT // ————————————————————————————————————————————————————— var csvListOfIDs = app.response({cQuestion:"Enter a list of text watermarks. Separate each item with a comma.\n\n - Don't include spaces between commas.\n - The only special characters allowed are dashes and underscores.", cTitle:"Multiple Copies with Unique Watermarks", cDefault:"COPY 01, COPY 02, COPY 03, COPY 04, COPY 05", cLabel:"Watermarks"}); // ————————————————————————————————————————————————————— // ————————————————————————————————————————————————————— // USER VARIABLES // ————————————————————————————————————————————————————— // Watermark Settings var iWatermarkFont = "Arial"; // Name of a font to use in the watermark, for example, "Arial". var iWatermarkFontSize = 64; var iWatermarkOpacity = .10; // Enter a decimal. Default=.15 Transparent=0 Opaque=1 var iWatermarkRotation = 45; // ————————————————————————————————————————————————————— // ————————————————————————————————————————————————————— // CREATE NUMBERED, WATERMARKED COPIES // ————————————————————————————————————————————————————— if (csvListOfIDs == null) { app.alert("Cancelled", 3); } else { // Get an array of values from the CSV IDs and trim the // excess whitespace. var str_array = csvListOfIDs.split(","); for(var i = 0; i < str_array.length; i++) { str_array[i] = str_array[i].replace(/^\s*/, "").replace(/\s*$/, ""); }; // Prompt the user to pick a file. var userPickedDoc = app.browseForDoc(); // Validate the user selection. if (typeof userPickedDoc != "undefined") { // User picked a document. // Define the user picked document object. var myURL = (userPickedDoc.cFS=="CHTTP") ? encodeURI(userPickedDoc.cPath) : userPickedDoc.cPath; var sourceDoc = app.openDoc({cPath: myURL, cFS: userPickedDoc.cFS}); // Get the folder where the user picked document is saved. var lastSlashPos = sourceDoc.path.lastIndexOf("/"); var folderPath = sourceDoc.path.substr(0, lastSlashPos) + "/"; // Define the base file name to use when saving output documents. var baseFileName = "Doc" // ————————————————————————————————————————————————————— // ————————————————————————————————————————————————————— // Create a temp copy of the original file to protect // the original during the script. var tempInitialFileName = "COPY OF INITIAL FILE" // Save and close the COPY of the original file. sourceDoc.saveAs(folderPath + tempInitialFileName + ".pdf"); sourceDoc.closeDoc(true); // Create Watermarked Copies for(var i = 0; i < str_array.length; i++) { // Open the unmodified temp copy of the original file. var iNewFile = app.openDoc(folderPath + tempInitialFileName + ".pdf"); // Apply a watermark. iNewFile.addWatermarkFromText({ cText: str_array[i], nTextAlign:app.constants.align.center, cFont: iWatermarkFont, nFontSize: iWatermarkFontSize, aColor: color.black, nOpacity: iWatermarkOpacity, nRotation: iWatermarkRotation }); // Save the new document with a unique name using the ID. iNewFile.saveAs(folderPath + baseFileName + " " + str_array[i] + ".pdf"); // Close the new document. iNewFile.closeDoc(true); }; } else { // User cancelled file selection. app.alert("Cancelled", 3); } }
