Search:   Help

Navigation

Groups

LyX documentation

Edit

Shared groups

Links

Adding an "invisible" tag to a PDF

Categories: Tag me
<< | Page list | >>

Description of how you can add an "invisible" tag to your PDF output that is not visible or printable.

This page was created by chr based on an post to the LyX users' list by Steve Litt.

Motivation for add an "invisible" tag

If you are publishing an E-book in PDF format, you may want to discourage copying. However, you want to avoid obnoxious DRM that disallows printing, degrades the printing or locks it to a specific computer. Here are two things that can be done instead:

  1. Print the customer's name in a footer at the bottom of the page. This discourages the customer from giving the PDF to his friends, because it's his name on the book, and if it "escapes" into the wild, it's obvious who did it.
    Note: An experience computer user knows several ways to get rid of the name, but your average user wont.
  2. In case the PDF escapes into the wild and maybe someone altered the name in the footer, I want a code inside the pdf that doesn't print. The easiest way I know to do this is to make the code a metadata property with a key and a value. The key will be a nonobvious string, and the value will look like gibberish, but I can scan the PDF for the key, get the value, and relate it back to my book sales.

So the end purpose is simply to tag the file on the off chance that it is later desirable to match it against a sold file.

How to add an invisible tag to a PDF file

The tag will be added as a metadata property with a key and value by using pdftk dump_data to write all metadata to a text file, then append the new key and value to the text file, then use pdftk update_info to put back all the metadata, including the new key value pair.

How to read the tag from a PDF file

You can use grep to get the line containing the key and value, and then sed to partially remove garbage, and my the listed C program to strip it down to nothing but the value so it can be assigned to a variable in a script using backticks or $(mycommand) syntax.

Details

Here's how it's done, assuming you want to tweak org.pdf so it includes metadata MYKEY=MYVALUE in the output PDF, tweaked.pdf:

pdftk org.pdf dump_data output mymetadata.txt
echo InfoKey: MYKEY >> mymetadata.txt
echo InfoValue: MYVALUE >> mymetadata.txt
pdftk org.pdf update_info mymetadata.txt output tweaked.pdf

You've now put metadata key MYKEY with value MYVALUE into tweaked.pdf.

You can quickly pull that data out with my check_pdf.sh script:

./check_pdf.sh tweaked.pdf MYKEY

Here's the (bash script) code for check_pdf.sh:

#!/bin/bash
cat $1 | grep -a "$2" | sed -e "s/.*$2\s*//" | \
                            format_pdf_metadata_value.exe

The program format_pdf_metadata_value.exe simply grabs the value from the line containing the key. It should work under Linux or Windows (or BSD, which I assume would make it Mac compatible).

Here is the C source code for format_pdf_metadata_value.exe:

#include <stdio.h>
int main()
    {
    int ord = getchar();
    int count = 0;

    /* GO TO OPENING PAREN */
    for(; ord != '(' && count < 20; count++, ord=getchar());
    if(count >= 20) /* magic number, much bigger than needed */
            {
            fprintf(stderr, "Value not found\n");
            return 1;
            }

    /* GO PAST OPENING PAREN */
    for(; ord == '('; ord=getchar());

    /* BLOW OFF LEADING NONPRINTABLES */
    for(;((ord < 0) || (ord > 127)); ord = getchar());

    /* PRINT UP TO BUT NOT INCLUDING CLOSING PAREN */
    /* DO NOT PRINT ALL THE NULL BYTES */
    for(; ord != ')'; ord = getchar())
            if(ord != 0)
                    putchar(ord);
    return 0;
    }

Contributors

  • Steve Litt
  • chr

Tag me

Edit - History - Print - Recent Changes - All Recent Changes - Search
Page last modified on 2010-02-11 11:27 UTC