Skip to content

Instantly share code, notes, and snippets.

@stefanschmidt
Last active February 1, 2024 17:01
Show Gist options
  • Star 47 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save stefanschmidt/5248592 to your computer and use it in GitHub Desktop.
Save stefanschmidt/5248592 to your computer and use it in GitHub Desktop.
Remove all annotations from a PDF document
pdftk original.pdf output uncompressed.pdf uncompress
LANG=C sed -n '/^\/Annots/!p' uncompressed.pdf > stripped.pdf
pdftk stripped.pdf output final.pdf compress
@swenson
Copy link

swenson commented Oct 21, 2013

Sweet! This worked great for me.

@Gabriel-p
Copy link

Thank for sharing this script, it worked perfectly!

@JohnRobson
Copy link

Thank you

@dvska
Copy link

dvska commented Apr 24, 2017

sed: RE error: illegal byte sequence
hmmm

@naveendennis
Copy link

Works perfectly! Thank you

@wenlibin02
Copy link

wenlibin02 commented Jun 20, 2017

It works, thank you very much.
I would suggest using LANG=C sed -i '/^\/Annots/d' uncompressed.pdf to modify the uncompressed.pdf in place.

@Dabolus
Copy link

Dabolus commented Mar 28, 2018

For those getting the Illegal byte sequence error, try adding LC_CTYPE=C at the beginning of the second command as well

e.g. LANG=C LC_CTYPE=C sed -n '/^\/Annots/!p' uncompressed.pdf > stripped.pdf

@rafaelbeirigo
Copy link

Thanks!

@hruzee
Copy link

hruzee commented Aug 8, 2018

Thaank you! Worked great 👍

@mstrauss
Copy link

Just be aware that the annotation text (there is any) remains in the file. It's just not visible any more.

@KlaraSaary
Copy link

Thanks! Here a for loop for cleaning several pdfs:
for file in *.pdf; do pdftk "$file" output un.pdf uncompress; LANG=C sed -n '/^/Annots/!p' un.pdf > str.pdf; pdftk str.pdf output "$file" compress; echo "Done $file"; done;

@davidcesarino
Copy link

This did not work for me. I had to remove all "/Type /Annot" commands as well in sed for the yellow annotations to disappear.

@faridcher
Copy link

A faster (in-memory) way is to use a shell pipeline:

pdftk in.pdf output - uncompress | sed '/^\/Annots/d' | pdftk - output out.pdf compress

@nisarkhanatwork
Copy link

Thank you! It is cool!

@hdatma
Copy link

hdatma commented Jan 27, 2021

Be aware that pdftk requires gcj, which was deprecated in 2017. This is old software that needs to be updated.

@chrisgrieser
Copy link

Just for convenience for anyone finding this via google like me: This is the code to remove all annotations from all pdfs in a directory.

# these are needed on Mac
export LC_CTYPE=C
export LANG=C

# cd /directory/with/pdfs/

for file in *.pdf
do
    outname=`sed -e "s/\.pdf$/_.pdf/"<<<"$file"`
    pdftk $file output - uncompress | sed '/^\/Annots/d' | pdftk - output $outname compress
    echo "$file: done"
done

@toehold
Copy link

toehold commented Dec 2, 2021

Is it possible to reduce the opacity?

@muelli
Copy link

muelli commented Jun 3, 2022

this leave me with a PDF with a broken xref table :(

pdfcpu annotations remove my.pdf

works reasonably well :)
https://pdfcpu.io/annot/annot

@jfines
Copy link

jfines commented Aug 23, 2023

Still works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment