{throw new RuntimeException(filePath + " file access error.",e);}
Document document = file.getDocument();
// 2. Link extraction from the document pages.
TextExtractor extractor = new TextExtractor();
extractor.setAreaTolerance(2); // 2 pt tolerance on area boundary detection.
boolean linkFound = false;
for(Page page : document.getPages())
{
if(!promptNextPage(page, !linkFound))
return false;
Map<Rectangle2D,List<ITextString>> textStrings = null;
linkFound = false;
// Get the page annotations!
PageAnnotations annotations = page.getAnnotations();
if(annotations == null)
{
System.out.println("No annotations here.");
continue;
}
// Iterating through the page annotations looking for links...
for(Annotation annotation : annotations)
{
if(annotation instanceof Link)
{
linkFound = true;
if(textStrings == null)
{textStrings = extractor.extract(page);}
Link link = (Link)annotation;
Rectangle2D linkBox = link.getBox();
// Text.
/*
Extracting text superimposed by the link...
NOTE: As links have no strong relation to page text but a weak location correspondence,
we have to filter extracted text by link area.
*/
StringBuilder linkTextBuilder = new StringBuilder();
for(ITextString linkTextString : extractor.filter(textStrings,linkBox))
{linkTextBuilder.append(linkTextString.getText());}
System.out.println("Link '" + linkTextBuilder + "' ");
// Position.
System.out.println(