@Test public void regexStartedWithNewLineTest() throws IOException { PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "regexStartedWithNewLineTest.pdf")); RegexBasedLocationExtractionStrategy extractionStrategy = new RegexBasedLocationExtractionStrategy("\\nstart"); new PdfCanvasProcessor(extractionStrategy).processPageContent(pdfDocument.getPage(1)); List<IPdfTextLocation> locations = new ArrayList<>(extractionStrategy.getResultantLocations()); pdfDocument.close(); Assert.assertEquals(1, locations.size()); Assert.assertEquals("\nstart", locations.get(0).getText()); Assert.assertTrue( new Rectangle(56.8f, 729.5970f, 20.6159f, 13.2839f).equalsWithEpsilon(locations.get(0).getRectangle())); }
@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();  }