I have on each page of my PDF document a line with this string:
%REPLACE%
Which I'd like to find and replace with another string.
Does anyone know how to do this with some command line application such as PDFTK?
This folk gave me an important clue however I'd like something more direct.
Thanks.
Click on 'Edit' and then 'Find' 3. Type the word you want to find in 'Find' box 4. Click the Next button (You should now see the selection that will be changed highlighted in the pdf) 5. Click the arrow next to 'Replace with' and then type you word you would like to replace it with 6.
By inserting page[NameObject("/Contents")] = contents. decodedSelf before writer. addPage(page) , we force pyPDF2 to update content of the page object. This way I was able to overcome this problem and replace text from pdf file.
Grep will not work as PDF is a binary format and the text is often compressed or encoded in a variety of ways.
You can try to modify content of your PDF as follows
Uncompress the text streams of PDF
pdftk file.pdf output uncompressed.pdf uncompress
Use sed to replace your text with another
sed -e "s/ORIGINALSTRING/NEWSTRING/g" <uncompressed.pdf >modified.pdf
If this attempt was successful, re-compress the PDF with pdftk
pdftk modified.pdf output recompressed.pdf compress
Note: This way is not successful every time, mainly due to font subsetting
For making a small change just on a few pages, inkscape
can do a good job. It can also fix some issues in diagrams and with table borders. One must process each page separately, though, and stick the pages back together using pdfunite
. (Unchanged page ranges can be extracted with pdfseparate
.)
Inspiration: https://tatica.org/2015/07/13/edit-pdf-inkscape/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With