@Test public void test01() throws IOException { System.out.println(new File(sourceFolder).getAbsolutePath()); PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "in01.pdf")); RegexBasedLocationExtractionStrategy extractionStrategy = new RegexBasedLocationExtractionStrategy(Pattern.compile("\\{\\{Signature\\}\\}")); List<IPdfTextLocation> locationList = new ArrayList<>(); for (int x = 1; x <= pdfDocument.getNumberOfPages(); x++) { new PdfCanvasProcessor(extractionStrategy).processPageContent(pdfDocument.getPage(x)); for(IPdfTextLocation location : extractionStrategy.getResultantLocations()) { if(location != null) { locationList.add(location); } } } Assert.assertEquals(1, locationList.size()); IPdfTextLocation loc = locationList.get(0); Assert.assertEquals("{{Signature}}", loc.getText()); Assert.assertEquals(23, (int) loc.getRectangle().getX()); Assert.assertEquals(375, (int) loc.getRectangle().getY()); Assert.assertEquals(55, (int) loc.getRectangle().getWidth()); Assert.assertEquals(11, (int) loc.getRectangle().getHeight()); pdfDocument.close(); }
@Override public Collection<IPdfTextLocation> getResultantLocations() { Collections.sort(parseResult, new TextChunkLocationBasedComparator(new DefaultTextChunkLocationComparator())); List<IPdfTextLocation> retval = new ArrayList<>(); CharacterRenderInfo.StringConversionInfo txt = CharacterRenderInfo.mapString(parseResult); Matcher mat = pattern.matcher(txt.text); while (mat.find()) { Integer startIndex = getStartIndex(txt.indexMap, mat.start(), txt.text); Integer endIndex = getEndIndex(txt.indexMap, mat.end() - 1); if (startIndex != null && endIndex != null && startIndex <= endIndex) { for (Rectangle r : toRectangles(parseResult.subList(startIndex, endIndex + 1))) { retval.add(new DefaultPdfTextLocation(0, r, mat.group(0))); } } } java.util.Collections.sort(retval, new Comparator<IPdfTextLocation>() { @Override public int compare(IPdfTextLocation l1, IPdfTextLocation l2) { Rectangle o1 = l1.getRectangle(); Rectangle o2 = l2.getRectangle(); if (o1.getY() == o2.getY()) { return o1.getX() == o2.getX() ? 0 : (o1.getX() < o2.getX() ? -1 : 1); } else { return o1.getY() < o2.getY() ? -1 : 1; } } }); removeDuplicates(retval); return retval; }
RegexBasedLocationExtractionStrategy implements ILocationExtractionStrategy { @Override public Collection<IPdfTextLocation> getResultantLocations() { Collections.sort(parseResult, new TextChunkLocationBasedComparator(new DefaultTextChunkLocationComparator())); List<IPdfTextLocation> retval = new ArrayList<>(); CharacterRenderInfo.StringConversionInfo txt = CharacterRenderInfo.mapString(parseResult); Matcher mat = pattern.matcher(txt.text); while (mat.find()) { Integer startIndex = getStartIndex(txt.indexMap, mat.start(), txt.text); Integer endIndex = getEndIndex(txt.indexMap, mat.end() - 1); if (startIndex != null && endIndex != null && startIndex <= endIndex) { for (Rectangle r : toRectangles(parseResult.subList(startIndex, endIndex + 1))) { retval.add(new DefaultPdfTextLocation(0, r, mat.group(0))); } } } java.util.Collections.sort(retval, new Comparator<IPdfTextLocation>() { @Override public int compare(IPdfTextLocation l1, IPdfTextLocation l2) { Rectangle o1 = l1.getRectangle(); Rectangle o2 = l2.getRectangle(); if (o1.getY() == o2.getY()) { return o1.getX() == o2.getX() ? 0 : (o1.getX() < o2.getX() ? -1 : 1); } else { return o1.getY() < o2.getY() ? -1 : 1; } } }); removeDuplicates(retval); return retval; } }
RegexBasedLocationExtractionStrategy implements ILocationExtractionStrategy { @Override public Collection<IPdfTextLocation> getResultantLocations() { Collections.sort(parseResult, new TextChunkLocationBasedComparator(new DefaultTextChunkLocationComparator())); List<IPdfTextLocation> retval = new ArrayList<>(); CharacterRenderInfo.StringConversionInfo txt = CharacterRenderInfo.mapString(parseResult); Matcher mat = pattern.matcher(txt.text); while (mat.find()) { Integer startIndex = getStartIndex(txt.indexMap, mat.start(), txt.text); Integer endIndex = getEndIndex(txt.indexMap, mat.end() - 1); if (startIndex != null && endIndex != null && startIndex <= endIndex) { for (Rectangle r : toRectangles(parseResult.subList(startIndex, endIndex + 1))) { retval.add(new DefaultPdfTextLocation(0, r, mat.group(0))); } } } java.util.Collections.sort(retval, new Comparator<IPdfTextLocation>() { @Override public int compare(IPdfTextLocation l1, IPdfTextLocation l2) { Rectangle o1 = l1.getRectangle(); Rectangle o2 = l2.getRectangle(); if (o1.getY() == o2.getY()) { return o1.getX() == o2.getX() ? 0 : (o1.getX() < o2.getX() ? -1 : 1); } else { return o1.getY() < o2.getY() ? -1 : 1; } } }); removeDuplicates(retval); return retval; } RegexBasedLocationExtractionStrategy(String regex);  RegexBasedLocationExtractionStrategy(Pattern pattern); }
RegexBasedLocationExtractionStrategy implements ILocationExtractionStrategy { @Override public Collection<IPdfTextLocation> getResultantLocations() { Collections.sort(parseResult, new TextChunkLocationBasedComparator(new DefaultTextChunkLocationComparator())); List<IPdfTextLocation> retval = new ArrayList<>(); CharacterRenderInfo.StringConversionInfo txt = CharacterRenderInfo.mapString(parseResult); Matcher mat = pattern.matcher(txt.text); while (mat.find()) { Integer startIndex = getStartIndex(txt.indexMap, mat.start(), txt.text); Integer endIndex = getEndIndex(txt.indexMap, mat.end() - 1); if (startIndex != null && endIndex != null && startIndex <= endIndex) { for (Rectangle r : toRectangles(parseResult.subList(startIndex, endIndex + 1))) { retval.add(new DefaultPdfTextLocation(0, r, mat.group(0))); } } } java.util.Collections.sort(retval, new Comparator<IPdfTextLocation>() { @Override public int compare(IPdfTextLocation l1, IPdfTextLocation l2) { Rectangle o1 = l1.getRectangle(); Rectangle o2 = l2.getRectangle(); if (o1.getY() == o2.getY()) { return o1.getX() == o2.getX() ? 0 : (o1.getX() < o2.getX() ? -1 : 1); } else { return o1.getY() < o2.getY() ? -1 : 1; } } }); removeDuplicates(retval); return retval; } RegexBasedLocationExtractionStrategy(String regex);  RegexBasedLocationExtractionStrategy(Pattern pattern); @Override Collection<IPdfTextLocation> getResultantLocations(); @Override void eventOccurred(IEventData data, EventType type); @Override Set<EventType> getSupportedEvents(); }
RegexBasedLocationExtractionStrategy implements ILocationExtractionStrategy { @Override public Collection<IPdfTextLocation> getResultantLocations() { Collections.sort(parseResult, new TextChunkLocationBasedComparator(new DefaultTextChunkLocationComparator())); List<IPdfTextLocation> retval = new ArrayList<>(); CharacterRenderInfo.StringConversionInfo txt = CharacterRenderInfo.mapString(parseResult); Matcher mat = pattern.matcher(txt.text); while (mat.find()) { Integer startIndex = getStartIndex(txt.indexMap, mat.start(), txt.text); Integer endIndex = getEndIndex(txt.indexMap, mat.end() - 1); if (startIndex != null && endIndex != null && startIndex <= endIndex) { for (Rectangle r : toRectangles(parseResult.subList(startIndex, endIndex + 1))) { retval.add(new DefaultPdfTextLocation(0, r, mat.group(0))); } } } java.util.Collections.sort(retval, new Comparator<IPdfTextLocation>() { @Override public int compare(IPdfTextLocation l1, IPdfTextLocation l2) { Rectangle o1 = l1.getRectangle(); Rectangle o2 = l2.getRectangle(); if (o1.getY() == o2.getY()) { return o1.getX() == o2.getX() ? 0 : (o1.getX() < o2.getX() ? -1 : 1); } else { return o1.getY() < o2.getY() ? -1 : 1; } } }); removeDuplicates(retval); return retval; } RegexBasedLocationExtractionStrategy(String regex);  RegexBasedLocationExtractionStrategy(Pattern pattern); @Override Collection<IPdfTextLocation> getResultantLocations(); @Override void eventOccurred(IEventData data, EventType type); @Override Set<EventType> getSupportedEvents();  }