-
-
Save elleryq/e068a277d219f83481ab to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env groovy | |
| @Grab('org.apache.pdfbox:pdfbox:1.8.8') | |
| import org.apache.pdfbox.cos.COSArray | |
| import org.apache.pdfbox.cos.COSString | |
| import org.apache.pdfbox.pdfparser.PDFStreamParser | |
| import org.apache.pdfbox.pdfwriter.ContentStreamWriter | |
| import org.apache.pdfbox.pdmodel.PDDocument | |
| import org.apache.pdfbox.pdmodel.PDPage | |
| import org.apache.pdfbox.pdmodel.common.PDStream | |
| import org.apache.pdfbox.util.PDFOperator | |
| def doc = PDDocument.load(args[0]) | |
| doc.documentCatalog.allPages.each { PDPage page -> | |
| def parser = new PDFStreamParser(page.contents) | |
| parser.parse() | |
| def j = 0 | |
| def flags = [] | |
| parser.tokens.each { token -> | |
| if (token instanceof PDFOperator) { | |
| PDFOperator op = (PDFOperator) token; | |
| if (op.operation == 'Tj') { | |
| COSString str = (COSString) parser.tokens.get(j - 1) | |
| if (str.string.contains('www.it-ebooks.info')) { | |
| flags << j - 1 | |
| flags << j | |
| } | |
| } | |
| } | |
| j++ | |
| } | |
| List tokens = [] | |
| for (int i = 0; i < parser.tokens.size(); i++) { | |
| if (!flags.contains(i)) { | |
| tokens << parser.tokens[i] | |
| } | |
| } | |
| def newContents = new PDStream(doc) | |
| def writer = new ContentStreamWriter(newContents.createOutputStream()) | |
| writer.writeTokens(tokens) | |
| newContents.addCompression() | |
| page.setContents(newContents) | |
| } | |
| doc.save(args[1]) | |
| if (doc) { | |
| doc.close() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment