1

I have over 50,000 PDF documents that need to be allocated to customer accounts. However each PDF contains pages I do not want the customer to see (internal company stuff) and so I would like to be able to delete specific pages based on their content ("for office use only" appearing on the page) and save the PDF as a new file.

I am comfortable with the scripting side, I just need to know what software is capable of such a task.

2

Python!

There are a number of libraries for manipulating PDF files, including the unmaintained pyPdf and its fork PyPDF2. You can process a file page by page, extract the text and copy the pages whose text is suitable.

I've run the following using Python3.6, and PyPDF2==1.26.0:

#!/usr/bin/env python3
import re
import sys

from PyPDF2 import PdfFileWriter, PdfFileReader

pdf_in = PdfFileReader(sys.stdin.buffer)
pdf_out = PdfFileWriter()
for p in [pdf_in.getPage(i) for i in range(0, pdf_in.getNumPages())]:
    text = p.extractText()
    if not re.search(r'for\s+office\s+use\s+only', text, re.I):
        pdf_out.addPage(p)
pdf_out.write(sys.stdout.buffer)

Beware that the text may not appear in the document exactly the way you want it. In a PDF document, lines, words or even characters can appear out of order. Rather than a stream of text, the document may be constructed with pieces appearing at certain coordinates.

Even if the text is in order, which is usually the case, text that spans multiple pages will have a footer and a header in the middle. Text that spans multiple lines may have extra spaces around the line breaks. Even text on the same lines may have multiple spaces between words, due to text justification (that's why I used \s+ rather than a plain space in the regular expression above).

Tools that extract text from PDF try to reconstruct the page as text but don't always do a perfect job. If pyPdf doesn't work well with your documents, you can try other libraries for text extraction; see this and this thread for some examples.

-1

One option would be to look at command-line PDF editors like PDFEdit, Pdftk, or PDFSaM. pdftotext may be useful as a simple way to identify the page numbers to remove.

  • Why would you convert to postscript? What's the point? Extracting text from PS isn't easier than extracting from PDF, quite the contrary. – Gilles Oct 26 '16 at 0:04

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.