• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Copy a PDF documents with unique, numbered watermarks

New Here ,
Jul 13, 2021 Jul 13, 2021

Copy link to clipboard

Copied

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);
    }
}
TOPICS
Acrobat SDK and JavaScript

Views

990

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 13, 2021 Jul 13, 2021

Copy link to clipboard

Copied

Works fine for me... What happens when you run it, exactly? Also, what's your version of Acrobat?

Acrobat DC has a bug where the closeDoc command doesn't work in all versions, so this might be what's causing it to malfunction.

 

And the "undefined" line is not an issue. It actually just means the code completed running without errors and without returning any values.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 19, 2021 Jul 19, 2021

Copy link to clipboard

Copied

Thank you so much for your response!

 

Noted re the "undefined" line - that's good to know that it is not an issue. My version of Acrobat is Adobe Acrobat Pro 2017 (let me know if any more details would be helpful). 

 

When I run the code, I copy it into the Javascript debugger console. I follow the instructions, press control all then control + enter. The 'multiple copies with unique watermarks' then pops up and asks for a list input (it automatically comes up as COPY 01, COPY 02, COPY 03, COPY 04, COPY 05). I then press okay and file browser comes up for me to select a PDF. When I press enter, about a second passes and then the 'undefined' pops up at the bottom of the code. In the folder where I had the original PDF (called X), two additional PDFs appear: 'Doc COPY 01' and 'COPY OF INITIAL FILE'. 'COPY OF INITIAL FILE' is just the same document as X, whereas 'Doc COPY 01' is document X but with the following watermark: 'COPY 01'.

 

I would be very grateful if you think there's anything I could do or adjust in the code so that it creates the full 5 copies (or however many inputs there are). Thank you in advance!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 21, 2021 Jul 21, 2021

Copy link to clipboard

Copied

Remove following line and it works:

iNewFile.closeDoc(true);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 24, 2021 Jul 24, 2021

Copy link to clipboard

Copied

Thank you so much for your help - that has worked! Really appreciated.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 12, 2021 Nov 12, 2021

Copy link to clipboard

Copied

This code is fantastic and working well for me. Do you know how to add line breaks and centerring in the text in order to fit a longer watermark?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 12, 2021 Nov 12, 2021

Copy link to clipboard

Copied

LATEST

Unfortunately there are no good ways with Acrobat JavaScript to determine text length, which you need in order to firgure out where where to break a string.   However, Acrobat of course, has this capability built in into muline text fields and markup text boxes. Markup text boxes have an opacity control, so you could easily replace the part of the code that places the watermark, with code that places a rich text box annotation.  

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines