Hello,
Im trying to write a function to copy the stream of a page to another page.
The thing is that it seems the PDFStreamParser is not parsing texts, cause
I´m not getting any texts on my new page.
And besides, I´m getting a warning when opening the newpages on adobe
reader.
Have someone made a similar function, or could give me a little help here?
Ps: does someone known a pdf utility which could look at elements of a
stream?
private void copyPageWithoutImage(PDPage page, PDPage newpage) throws
IOException {
PDStream contents = page.getContents();
contents.getStream();
PDFStreamParser parser = new
PDFStreamParser(contents.getStream());
try {
List tokensNovos = new LinkedList();
Iterator<Object> iter = parser.getTokenIterator();
List arguments = new ArrayList();
while (iter.hasNext()) {
boolean allowNext = true;
Object next = iter.next();
Object aux2 = next;
if (aux2 instanceof COSName) {
COSName objectName2 = (COSName) aux2;
System.out.println(objectName2.getName());
}
if (next instanceof COSObject) {
arguments.add(((COSObject) next).getObject());
} else if (next instanceof PDFOperator) {
if (next instanceof PDFOperator) {
PDFOperator op = (PDFOperator) next;
String operation = op.getOperation();
if (operation.equals("Do")) {
if (arguments.size() > 0) {
Object aux =
arguments.get(0);
if (aux instanceof COSName) {
COSName objectName =
(COSName) aux;
PDXObject xobject =
(PDXObject) page.getResources().getXObjects().get(objectName.getName());
if (xobject instanceof
PDXObjectImage) {
allowNext =
false;
}
}
}
}
}
arguments = new ArrayList();
} else {
arguments.add(next);
}
if (allowNext) {
tokensNovos.add(next);
}
}
PDPageContentStream contentStream = new
PDPageContentStream(this.pdf, newpage);
contentStream.beginText();
contentStream.endText();
contentStream.close();
PDStream updatedStream = newpage.getContents();
ContentStreamWriter tokenWriter = new
ContentStreamWriter(updatedStream.createOutputStream());
tokenWriter.writeTokens(tokensNovos);
newpage.setContents(updatedStream);
} finally {
if (parser != null) {
parser.close();
}
}
}