PDF – How to View and Edit the Code of a PDF File

pdf

I was wondering how to view and edit the code of a PDF file?

  1. By viewing, I don't want to see the binary format, so I think hexdump may not be what I want. I tried gedit,
    but no encoding method can be used to decode the PDF content.

  2. By editing, I would like to search for /Fit and change them to
    /XYZ by for example sed. But my command sed s/\/Fit/\/XYZ/ < 1.pdf > 2.pdf seem not change the appearance of my PDF as I expected,
    although it doesn't report any error. I was wondering if sed can
    actually work on PDF files as if they were plain text?

The context of my questions can be found from this question. My OS is Ubuntu 10.10.

Best Answer

You can use sed with binary files (at least GNU sed; some implementations may have trouble with files containing null characters or not ending with a newline character). But the command you used only replaces the first occurrence of /Fit on each line, and lines are pretty much meaningless in a PDF file. You need to replace all occurrences:

 sed s/\/Fit/\/XYZ/g

It would be more robust only replace /Fit if it's not followed by a word constituent (e.g. not replacing /Fitness; I don't know if your file contains occurrences of /Fit that would cause trouble). Here's one way:

perl -pe 's!/Fit\b!/XYZ!g'
Related Question