Search code examples
pdfbatch-processingacrobat

Batch PDF Watermark [PDF -> JPG -> PDF]


I am working with over thousands PDF files for a Sheet Music publisher.

All of these PDF files needs a preview PDF. A watermark for PDF files can easily be removed so I am asking for a true way to watermark our PDF:s in a batch operation.

PDF->Apply Watermark->JPG->Back to PDF

How can I do this? Is there a good tool for this operations?


Solution

  • The free route

    ImageMagick can do the complete process for you, especially with the composite command's -watermark operator.

    #!/bin/sh
    
    # ImageMagick will pick the correct conversion formats based on filename suffixes, or maybe actual binary content?
    InputPDF=$1
    WatermarkImg=$2
    OutputPDF=$3
    
    pdfToImage=pdfToImage.png
    imageWithWatermark=imageWithWatermark.png
    
    # Convert PDF to image
    convert           \
       -density 300   \
       -trim          \
        "$InputPDF"   \
       -quality 100   \
       -flatten       \
       -sharpen 0x1.0 \
       $pdfToImage
    
    # Add watermark to intermediate image
    composite          \
       -dissolve 15    \
       -tile           \
       "$WatermarkImg" \
       $pdfToImage     \
       $imageWithWatermark
    
    # Convert intermediate image back to PDF
    convert                \
       $imageWithWatermark \
       "$OutputPDF"
    
    # Clean up
    rm $pdfToImage $imageWithWatermark
    

    I find the PDF to image conversion acceptable in terms of quality, though you can see some differences when looking at the before and after side-by-side, especially in how bolded glyphs seem less bold:

    enter image description here

    You can check this good post and its answers for a number of options for converting a PDF to an image, Convert PDF to image with high resolution.

    I checked out PDFtoPPM, which was also highly mentioned in that thread, and I still see some degrading of the bolded fonts when converted:

    enter image description here

    Some more tiling Magick

    I used this copyright symbol from Wikimedia Commons and this ImageMagick script:

    #!/bin/sh
    
    Infile="Copyright.png"
    Outfile="Copyright_tiled.png"
    
    h2=$(convert $Infile -format "%[fx:round(h/2)]" info:)
    
    convert $Infile                   \
        \( -clone 0 -roll +0+"$h2" \) \
        +append                       \
        -write mpr:sometile           \
        +delete                       \
        -size 1224X1584               \
        tile:mpr:sometile             \
    $Outfile
    

    to create this staggered tiling (1224X1584 is the page size (8.5in x 11in) multiplied by 72 px/in, times 2 for a good density of tiles):

    enter image description here