if (xobjects == null) {
xobjects = new PdfDictionary();
resources.put(PdfName.XOBJECT, xobjects);
}
// parsing the content stream of the page
PRStream stream = (PRStream)page.getAsStream(PdfName.CONTENTS);
byte[] contentBytes = PdfReader.getStreamBytes(stream);
PRTokeniser tokeniser = new PRTokeniser(new RandomAccessFileOrArray(RASFACTORY.createSource(contentBytes)));
PdfContentParser ps = new PdfContentParser(tokeniser);
ArrayList<PdfObject> operands = new ArrayList<PdfObject>();
while (ps.parse(operands).size() > 0){
PdfLiteral operator = (PdfLiteral)operands.get(operands.size() - 1);
processOperator(operator, operands);
}
// dealing with orphans
while (items.size() > 0 && items.get(0).getPageref() == pageref.getNumber()) {
StructureItem item = items.get(0);
if (item instanceof StructureObject) {
convertToXObject((StructureObject)item);
items.remove(0);
}
}
if (annots.size() == 0) {
page.remove(PdfName.ANNOTS);
}
else {
PdfDictionary annot;
for (int i = 0; i < annots.size(); i++) {
annot = annots.getAsDict(i);
if (annot.getAsNumber(PdfName.STRUCTPARENT) == null)
throw new DocumentException(MessageLocalization.getComposedMessage("could.not.flatten.file.untagged.annotations.found"));
}
}
// replacing the content stream
baos.flush();
baos.close();
stream.setData(baos.toByteArray());
// showing how many items are left
LOGGER.info(String.format("There are %d items left for processing", items.size()));
}