List of usage examples for com.itextpdf.text.pdf PdfArray getAsStream
public PdfStream getAsStream(final int idx)
PdfObject as a PdfStream, resolving indirect references. From source file:com.poet.ar.remover.AnnotationRemover.java
/** * remove content that matches keywords/*from w w w.j a v a 2 s .c o m*/ * * @param page * @return count of removed content */ private static int doRemoveContent(PdfDictionary page) { // all contents in page i PdfArray contentArray = page.getAsArray(PdfName.CONTENTS); PdfDictionary resources = page.getAsDict(PdfName.RESOURCES); List<Integer> willRemovedIx = new ArrayList<Integer>(); if (contentArray != null) { PdfStream stream = null; for (int i = 0; i < contentArray.size(); i++) { stream = contentArray.getAsStream(i); PRStream pr = (PRStream) stream; // TODO // FIXME: 2016/1/27 0027 java.lang.ClassCastException: com.itextpdf.text.pdf.PdfArray cannot be cast to com.itextpdf.text.pdf.PdfLiteral // get display text // String text = StreamContentExtractor.extractFromPdfStream(stream, resources); // // if (keywords.contains(text)) { // willRemovedIx.add(i); // } try { String text = StreamContentExtractor.extractFromPdfStream(stream, resources); if (keywords.contains(text)) { willRemovedIx.add(i); } } catch (Exception ex) { } } int i = 0; for (Integer ix : willRemovedIx) { contentArray.remove(ix - i++); } } return willRemovedIx.size(); }