Can I use sed to edit a PDF???

steelspy used Ask the Experts™
Can I use sed (or vi) to edit a PDF? or will it cause bad things to happen?

I want to change all occurences of

/Rotate 0


/Rotate 90

I was told that this will corrupt the PDF.  Something about offsets within the PDF and the change in the file size.

I haven't run into the problem with acrobat reader 6.0 or 7.0 when viewing the PDF after modifying them in such a fashion.

Should I be concerned?

Is there another tool I can use in a UNIX environment to perform the rotate I am trying to achieve?

If so, what is the tool and the syntax I need to use?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Yes, it will definitely corrupt your PDF file. DO NOT USE A TEXT EDITOR ON A PDF FILE!

The most important information in a PDF file is the so called cross-reference table. This table contains byte offsets for all elements in the PDF file. If you change 0 to 90, you will introduce at least one extra byte. You did run into problems after editing the PDFs... But, you seem to be using a fast machine, so the message that gets displayed when you load the file goes away too fast. Acrobat can repair _SOME_ of these files, but other PDF processing applications may not be able to do so. Your PDFs are corrupt, and need to be repaired. Acrobat - in your case - does this repair for you.

Another potential problem is that the PDF file content may actually be compressed, and you would not be able to find the rotation command.

One option is to use pdftk (, which can uncompress PDFs so that you can edit them, and then repair them after your edits so that you end up with correct PDFs again.

This page contains some sample command lines for this tool:

You will probably need these two commands:
pdftk mydoc.pdf output mydoc.clear.pdf uncompress
pdftk broken.pdf output fixed.pdf

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial