Search code examples
pdfghostscriptpdftk

How to trim unwanted text in PDF?


I can crop PDF (for example in Acrobat).

But text outside of the crop margin will still be maintained in the PDF (even though I don't see it in the viewable area).

I want to remove anything outside the crop margin. Is there a command line tool that can do so?


Solution

  • Ghostscript can do that. Ghostscript is a command line tool which is available for all major operating systems.

    The command which does it for Linux or Mac OS X:

    gs -o cropped-and-removed.pdf \
       -sDEVICE=pdfwrite          \
       -dUseCropBox               \
        in.pdf
    

    The command for Windows:

    gswin64c.exe -o cropped.pdf ^
       -sDEVICE=pdfwrite        ^
       -dUseCropBox             ^
        in.pdf
    

    Be sure to use a rather recent version of Ghostscript. Current is v9.16.