InternalValue[] values = data.getValues();
if (values.length > 0) {
final BLOBFileValue blob = (BLOBFileValue) values[0].internalValue();
LazyReader reader = new LazyReader() {
protected void initializeReader() throws IOException {
PDFParser parser = null;
InputStream in;
try {
in = blob.getStream();
} catch (RepositoryException e) {
throw new IOException(e.getMessage());
}
try {
parser = new PDFParser(new BufferedInputStream(in));
parser.parse();
PDDocument document = parser.getPDDocument();
try {
CharArrayWriter writer = new CharArrayWriter();
PDFTextStripper stripper = new PDFTextStripper();
stripper.setLineSeparator("\n");
stripper.writeText(document, writer);
delegate = new CharArrayReader(writer.toCharArray());
} finally {
document.close();
}
} catch (Exception e) {
// it may happen that PDFParser throws a runtime
// exception when parsing certain pdf documents
// JCR-764: Check if document is still open and
// close it appropriately. Otherwise some temporary
// files may get left behind and document finalization
// will log a warning.
if (parser != null) {
try {
parser.getDocument().close();
} catch (Exception ioe) {
// ignore, this means doc has not been generated
}
}