Ubuntu – Command line tool to search and replace text on a PDF

command linelibreofficepdf

I have a PDF that has my name as an obnoxious watermark through out a rather long PDF file. I tried replacing the text in LibreOffice Draw with blanks, but while my name does appear as text, the find and replace function seems to tank my computer taking significant RAM and CPU time to do.

Is there a command line way to remove strings from PDF? Hmm… can sed do that?

Best Answer

As in many cases it’s just text, you can often remove it simply with sed or in fact any text editor – let’s say it says “watermark”:

sed 's/watermark//g' in.pdf >out.pdf

If your PDF file is compressed this doesn’t work, you need to uncompress it first, e.g. with pdftk (How can I install pdftk in Ubuntu 18.04 and later?):

pdftk in.pdf output out.pdf uncompress 

If sed’s output is not readable with your preferred PDF reader, try repairing it with pdftk:

pdftk out.pdf output out_pdftk.pdf

Further reading: How to Edit PDFs?

Source: How to remove watermark from pdf using pdftk • Super User