I'm trying to add page labels to a PDF file by modifying the file directly with a text editor.

When I open the PDF in a text editor and save it, without making any changes, the file becomes corrupted and can't be opened by Adobe Reader.

Why does this happen?

The solution that came to my mind is using HEX editor, but that doesn't seem to be a comfortable way of working with files. Is there any other way?

As a text editor, I use Sublime Text.

  • 1
    The problem is probably relating to the text encoding. You should check which encoding the text editor is defaulting to and change it if necessary. – James P Dec 1 '16 at 13:34
  • I've tried using several encodings with no success. Which encoding should I use? The file is mostly binary. However, since I'm not changing the file, I don't understand why encoding matters. – Draex_ Dec 1 '16 at 13:40
  • 2
    Well, PDF's aren't designed to be edited this way anyway, but if your text editor attempts to change the encoding then it just makes matters worse. Have you tried using Notepad++ instead? If I open a PDF and save it then it still seems to work. – James P Dec 1 '16 at 13:51
  • 1
    The question is not "which encoding should I use", the point is that your text editor probably assumes the PDF binary data is text in some particular encoding, and makes some changes that are valid for that particular encoding (like adding BOM marks), but that are invalid for the PDF binary data. So your text editor does make changes just by opening the file. Fix the problem by using a text edit which doesn't do that. The next problem is that by editing the file, you'll make the xref table invalid, so you need to recompute it. – dirkt Dec 1 '16 at 13:55
  • 1
    Some viewers repair the xref table automatically if they detect that it's corrupt, some don't. I'm on Linux and use mainly xpdf and mupdf, so I can't tell you what Windows viewers do. But if the position of the objects changed, the xref table is corrupt and should be regenerated if you want to have a standard-conforming file. – dirkt Dec 1 '16 at 14:59

Using Notepad++ instead of Sublime Text solved the issue.

Apparently, my Sublime Text made some changes to the file even when it wasn't asked to do so.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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