22

How can I easily remove all comments and annotations (added with Foxit Reader) from all the PDFs in a folder?

2
  • 1
    is it a valid assumption to think you want only suggestions for free (as in beer) or for Free (as in liberty) solutions? Dec 19, 2010 at 11:31
  • @pipitas I am interested in any kind of solution.
    – Andrew
    Dec 22, 2010 at 16:26

6 Answers 6

12

I just came across this problem, and none of the answers given here worked for me. What did work was the rewritepdf tool from the Ubuntu package libcam-pdf-perl:

rewritepdf -C in.pdf out.pdf

Wrapping this into a little scripting to remove annotations from all pdf files in a directory is now easy:

for i in *.pdf; do rewritepdf -C '$i' '$i'.new; done

As usual, you can install libcam-pdf-perl via the Software Center or using sudo apt install libcam-pdf-perl

2
  • It worked fine.:) Some help: The Ubuntu (i.e., debian) package is here packages.debian.org/sid/perl/libcam-pdf-perl Dependences are automatically installed using the "Ubuntu software center". (Oh, and watch out with the capital "-C". I first ran "-c" and nothing happened, even no error was output.) Sep 21, 2016 at 13:34
  • The single quote apparently needs to be replaced by double quotes, otherwise literal $i is passed as a filename to the command (I'm a Windows user and installed WSL to try this).
    – s_a
    Sep 8, 2021 at 0:46
5

Providing you're on a Unix system:

cd <directory containing PDFs>
find . -type f -name '*.pdf' -exec perl -pi -e 's:/Annots \[[^]]+\]::g' {} +

This is a hack that removes all /Annots commands from the PDF (the commands that draws the annotations). It leaves the annotation objects there (you can open the PDF with a text editor and search for them), they're just not drawn.

5
  • Can you explain the RE? What does [^]]+ match
    – jftuga
    May 25, 2012 at 12:03
  • 1
    @jftuga, s: (substitute) /Annots \[ (the text "/Annots [") [^]]+ (one or more instances of any character besides "]") \] (the literal character "]") :: (replace anything matching the former with nothing) g (replace multiple times per line if necessary). May 25, 2012 at 12:39
  • 1
    One potentially confusing part of the regex is that a literal ] normally has to be escaped, but not right after a ^ negation. May 25, 2012 at 12:45
  • This also removes internal document links (as, I believe, those are implemented in a pdf as /Annots, too) Aug 29, 2019 at 12:41
  • Doesn't work for me.
    – okh
    Oct 12, 2020 at 17:28
5

Haven't tested it a great deal, but the following seems to work. It deletes all annotations, except internal document links (which none of the answers here seem to do). This script depends on the pdfrw python library.

#!/usr/bin/python

import sys, pdfrw

try:
    in_path = sys.argv[1]
    out = sys.argv[2]
except:
    print("Usage:\tannotclean IN.pdf OUT.pdf")
    exit(0)

reader = pdfrw.PdfReader(in_path)

for p in reader.pages:
    if p.Annots:
        # See PDF reference, Sec. 12.5.6 for all annotation types
        p.Annots = [a for a in p.Annots if a.Subtype == "/Link"]

pdfrw.PdfWriter(out, trailer=reader).write()

Usage:

  1. Save as a script somewhere (I assume in your PATH), e.g. /usr/local/bin/annotclean.
  2. annotclean in.pdf cleaned.pdf
  3. (optional) batch processing:
# fish shell syntax
for p in **pdf # pdfs from current directory and subdirectories
    annotclean $p $p.new
    mv $p.new $p # overwrite the old
end 
2

I think you can do that most easily by "refrying" the PDF. Refrying means: first convert the file to PostScript, then convert the PostScript back to PDF. Usually refrying is frowned upon, because usually you loose quality and some content. In your case you want to loose the content. The re-frying can be done with Ghostscript (and the helper batch files shipping with it -- download the gs900w32.exe if you are on Windows), so here you go, with 2 easy commands:

pdf2ps.bat input.pdf output.ps
ps2pdf.bat output.ps input_refried.pdf
5
  • 1
    This doesn't work. Written-in comments remain (not as comments, but as part of the pdf).
    – Andrew
    Dec 22, 2010 at 16:33
  • 2
    If the comments are actually added to the content of the PDF, they can only be removed manually. Actual PDF annotations are separate.
    – CarlF
    Dec 22, 2010 at 18:30
  • Is there any way without using any converter?
    – user
    Apr 23, 2011 at 17:45
  • This also removes internal document links. Aug 29, 2019 at 12:40
  • @AlecJacobson: Of course. If you convert to PostScript you loose a lot of the "rich" content that was part of PDF. PostScript does not have the means to represent ANY links, not even internal document links.... Aug 29, 2019 at 13:32
2

OK, you said you'd also consider a commercial solution....

I'd recommend you try callas pdfToolbox. It's available for Windows and Mac OS X. (They have a CLI for Linux as well, but you can only use pre-configured "profiles" with it. With the Windows GUI, you can create your custom profiles and re-use them with the Linux CLI, though.

The pdfToolbox has lots and lots and lots of way to manipulate and fix many, many individual PDF problems.

One of the "Fixups" is to remove all annotations.

You don't need to shell out any money to test it first; callas gives out 14days trial licenses for free.

2
  • It indeed does have a way to to remove all annotations, but I'm not sure how to do a batch job.
    – Andrew
    Dec 23, 2010 at 15:20
  • Dunno about previous versions --- but the latest pdfToolbox5 release allows to run it in batch mode against complete folders containing PDFs..... Apr 27, 2011 at 11:30
2

A fast (in-memory) way is to use pdftk and sed as in the following shell pipeline:

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .