Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 5 down vote accepted

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'
share|improve this answer
Thanks! It now works! (1) I was wondering how sed search characters in binary content? Does sed firstly encode the query characters before search? (2) In the last command, what does !, \b and g mean? Can it be done without perl just with sed? – Tim Jul 22 '11 at 14:00
@Tim (1) Sed loads the data into memory, operates on it and prints it out. Why would it need to encode anything? (2) g means to replace all occurrences on each line, in both sed and perl. ! is the separator; you can choose (almost) any character as the separator for the s command (this goes both in sed and perl). \b means a word boundary; it exists in perl but not in sed. – Gilles Jul 22 '11 at 14:05
About (1), because the characters that you give to sed in the command are human readable. If the content to search in is completely binary, how can sed find the query word there? – Tim Jul 22 '11 at 14:08
@Tim Text is binary data that happens to be human readable. – Gilles Jul 22 '11 at 14:20
@Tim Yes, you can pass binary data in the query. You'll have to insert the characters literally in your sed or shell source code. – Gilles Jul 22 '11 at 14:30

Regarding your 1st question ("viewing source code, but no binary"): there are a few options which you have in order to de-compress the internal binary streams which are attached to many objects.

My favorite tool for this is QPDF, available on all major OS platforms. The following command de-compresses all streams and all object streams:

 qpdf --qdf --object-streams=disable orig.pdf expanded.pdf

Now you can open your PDF in any text editor. (There may still be some binary blobs in there: f.e. fontfiles and ICC profiles which wouldn't make sense for QPDF to expand).

To re-compress the expanded.pdf again after editing, you can run:

 qpdf expanded.pdf orig2.pdf

(Careful when manually editing PDFs! You need to know a lot about their internal syntax in order to do this right. As soon as you add or delete a single byte, you can get error messages from PDF readers who may no longer be able to open it, because the PDFs internal ToC is corrupted, which is based on byte-offset calculations. Just replacing Fit by XYZ strings should go fine, though...)

share|improve this answer

sed is line-oriented, that makes it not well suited for binary files, which are structured as blocks not lines.
Try using bbe (bbe-.sourceforge.net) instead.

Alternatively, both Emacs (GNU and XEmacs) and vim open PDF files seamlessly. It's not very pretty printed of course, as it is mixed text and binary, but it's sufficient for your editing purposes.
There is a Pdftk plugin for vim that makes everything easier, download here (zip file).
As you probably know, both above editors have powerful search-and-replace capabilities.

Also, converting the PDF to QDF mode before makes editing PDF files really easy.

share|improve this answer
You may also try to edit with sed using the -b switch. if it works I will add this to my answer. – Philomath Jul 22 '11 at 13:03
@Tim: what do you mean by "does not show anything", just empty? any error message? Also, can you try with XEmacs ? (all three of them worked for me). – Philomath Jul 22 '11 at 13:17
Never mind about -b, it's cygwin specific. – Philomath Jul 22 '11 at 13:18
Emacs says "File 1.pdf is large (9MB), really open? (y or n)". I chose "y", and then nothing is there. – Tim Jul 22 '11 at 13:30
Most probably a Emacs problem, do you have XEmacs? (I just opened a 31 MB PDF without any problems). – Philomath Jul 22 '11 at 13:36

Use LibreOffice or OpenOffice to open the PDF, view it, replace things, write a new PDF, etc. I think that you can even use it from the command line or programmatically if there are a lot of documents to process.

Note that PDFs from some sources, e.g. Scanners, often contain the pages as images rather than as text so you will be out of luck with them for using search and replace.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.