import org.apache.pdfbox.exceptions.COSVisitorException; import org.apache.pdfbox.exceptions.CryptographyException; import org.apache.pdfbox.exceptions.InvalidPasswordException; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDDocumentCatalog; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDResources; import java.io.IOException; public class Main { public static void main(String[] argv) throws COSVisitorException, InvalidPasswordException, CryptographyException, IOException { PDDocument document = PDDocument.load("input.pdf"); if (document.isEncrypted()) { document.decrypt(""); } PDDocumentCatalog catalog = document.getDocumentCatalog(); for (Object pageObj : catalog.getAllPages()) { PDPage page = (PDPage) pageObj; PDResources resources = page.findResources(); resources.getImages().clear(); } document.save("strippedOfImages.pdf"); } }
public InputStream convertDocument(InputStream dataStream) throws Exception { // just acts as a pass through since already in pdf format PipedOutputStream os = new PipedOutputStream(); PipedInputStream is = new PipedInputStream(os); System.setProperty("org.apache.pdfbox.baseParser.pushBackSize", "2024768"); //for large files PDDocument doc = PDDocument.load(dataStream, true); if (doc.isEncrypted()) { //remove the security before adding protections doc.decrypt(""); doc.setAllSecurityToBeRemoved(true); } doc.save(os); doc.close(); dataStream.close(); os.close(); return is; }
PDDocument document = PDDocument.load( args[0] ); if( document.isEncrypted() ) { document.decrypt( "" ); } PDFTextStripperByArea stripper = new PDFTextStripperByArea(); stripper.setSortByPosition( true ); Rectangle rect = new Rectangle( 10, 280, 275, 60 ); stripper.addRegion( "class1", rect ); List allPages = document.getDocumentCatalog().getAllPages(); PDPage firstPage = (PDPage)allPages.get( 0 ); stripper.extractRegions( firstPage ); System.out.println( "Text in the area:" + rect ); System.out.println( stripper.getTextForRegion( "class1" ) );
document.decrypt("");
password = ""; pdfDocument.decrypt(password); } catch (Exception e) {
if (document.isEncrypted()) { try { document.decrypt(""); } catch (InvalidPasswordException e) { System.err.println("Error: Document is encrypted with a password.");
pdfDocument.decrypt( "" );
document.decrypt( password );
pdfDocument.decrypt( "" );
document.decrypt("");
document.decrypt("");
document.decrypt( password );
document.decrypt( password );
pdDocument.decrypt("");