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();  }
@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(); }
@Test public void testLigatureBeforeLigature() throws IOException { System.out.println(new File(sourceFolder).getAbsolutePath()); PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "ligature.pdf")); RegexBasedLocationExtractionStrategy extractionStrategy = new RegexBasedLocationExtractionStrategy("ca"); 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("ca", loc.getText()); Rectangle rect = loc.getRectangle(); Assert.assertEquals(36, rect.getX(), 0.0001); Assert.assertEquals(655.4600, rect.getY(), 0.0001); Assert.assertEquals(25.1000, rect.getWidth(), 0.0001); Assert.assertEquals(20, rect.getHeight(), 0.0001); pdfDocument.close(); }
@Test public void testLigatureCrossLigature() throws IOException { System.out.println(new File(sourceFolder).getAbsolutePath()); PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "ligature.pdf")); RegexBasedLocationExtractionStrategy extractionStrategy = new RegexBasedLocationExtractionStrategy("al"); 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("al", loc.getText()); Rectangle rect = loc.getRectangle(); Assert.assertEquals(48.7600, rect.getX(), 0.0001); Assert.assertEquals(655.4600, rect.getY(), 0.0001); Assert.assertEquals(25.9799, rect.getWidth(), 0.0001); Assert.assertEquals(20, rect.getHeight(), 0.0001); pdfDocument.close(); }
@Test public void testLigatureInLigature() throws IOException { System.out.println(new File(sourceFolder).getAbsolutePath()); PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "ligature.pdf")); RegexBasedLocationExtractionStrategy extractionStrategy = new RegexBasedLocationExtractionStrategy("l"); 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("l", loc.getText()); Rectangle rect = loc.getRectangle(); Assert.assertEquals(61.0999, rect.getX(), 0.0001); Assert.assertEquals(655.4600, rect.getY(), 0.0001); Assert.assertEquals(13.6399, rect.getWidth(), 0.0001); Assert.assertEquals(20, rect.getHeight(), 0.0001); pdfDocument.close(); }
@Test public void testRotatedText() throws IOException { PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "rotatedText.pdf")); RegexBasedLocationExtractionStrategy extractionStrategy = new RegexBasedLocationExtractionStrategy("abc"); 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(2, locationList.size()); Assert.assertTrue(locationList.get(0).getRectangle().equalsWithEpsilon(new Rectangle(188.512f, 450f, 14.800003f, 25.791992f))); Assert.assertTrue(locationList.get(1).getRectangle().equalsWithEpsilon(new Rectangle(36f, 746.688f, 25.792f, 14.799988f))); pdfDocument.close(); }
@Test public void regexStartedWithWhiteSpaceTest() throws IOException { PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "regexStartedWithWhiteSpaceTest.pdf")); RegexBasedLocationExtractionStrategy extractionStrategy = new RegexBasedLocationExtractionStrategy("\\sstart"); new PdfCanvasProcessor(extractionStrategy).processPageContent(pdfDocument.getPage(1)); List<IPdfTextLocation> locations = new ArrayList<>(extractionStrategy.getResultantLocations()); pdfDocument.close(); Assert.assertEquals(1, locations.size()); Assert.assertEquals(" start", locations.get(0).getText()); Assert.assertTrue( new Rectangle(92.3f, 743.3970f, 20.6159f, 13.2839f).equalsWithEpsilon(locations.get(0).getRectangle())); }
@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())); }
@Test public void regexWithWhiteSpacesTest() throws IOException { PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "regexWithWhiteSpacesTest.pdf")); RegexBasedLocationExtractionStrategy extractionStrategy = new RegexBasedLocationExtractionStrategy( "\\sstart\\s"); new PdfCanvasProcessor(extractionStrategy).processPageContent(pdfDocument.getPage(1)); List<IPdfTextLocation> locations = new ArrayList<>(extractionStrategy.getResultantLocations()); pdfDocument.close(); Assert.assertEquals(1, locations.size()); Assert.assertEquals(" start ", locations.get(0).getText()); Assert.assertTrue( new Rectangle(92.3f, 743.3970f, 20.6159f, 13.2839f).equalsWithEpsilon(locations.get(0).getRectangle())); }
@Test public void regexWithNewLinesTest() throws IOException { PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "regexWithNewLinesTest.pdf")); RegexBasedLocationExtractionStrategy extractionStrategy = new RegexBasedLocationExtractionStrategy( "\\nstart\\n"); new PdfCanvasProcessor(extractionStrategy).processPageContent(pdfDocument.getPage(1)); List<IPdfTextLocation> locations = new ArrayList<>(extractionStrategy.getResultantLocations()); pdfDocument.close(); Assert.assertEquals(1, locations.size()); Assert.assertEquals("\nstart\n", locations.get(0).getText()); Assert.assertTrue( new Rectangle(56.8f, 729.5970f, 20.6159f, 13.2839f).equalsWithEpsilon(locations.get(0).getRectangle())); }
@Test public void regexWithNewLineBetweenWordsTest() throws IOException { PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "regexWithNewLineBetweenWordsTest.pdf")); RegexBasedLocationExtractionStrategy extractionStrategy = new RegexBasedLocationExtractionStrategy( "hello\\nworld"); new PdfCanvasProcessor(extractionStrategy).processPageContent(pdfDocument.getPage(1)); List<IPdfTextLocation> locations = new ArrayList<>(extractionStrategy.getResultantLocations()); pdfDocument.close(); Assert.assertEquals(2, locations.size()); Assert.assertEquals("hello\nworld", locations.get(0).getText()); Assert.assertEquals("hello\nworld", locations.get(1).getText()); Assert.assertTrue( new Rectangle(56.8f, 729.5970f, 27.8999f, 13.2839f).equalsWithEpsilon(locations.get(0).getRectangle())); Assert.assertTrue( new Rectangle(56.8f, 743.3970f, 23.9039f, 13.2839f).equalsWithEpsilon(locations.get(1).getRectangle())); }
@Test public void regexWithOnlyNewLine() throws IOException { PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "regexWithNewLinesTest.pdf")); RegexBasedLocationExtractionStrategy extractionStrategy = new RegexBasedLocationExtractionStrategy("\\n"); new PdfCanvasProcessor(extractionStrategy).processPageContent(pdfDocument.getPage(1)); List<IPdfTextLocation> locations = new ArrayList<>(extractionStrategy.getResultantLocations()); pdfDocument.close(); Assert.assertEquals(0, locations.size()); }
@Test public void regexWithOnlyWhiteSpace() throws IOException { PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "regexWithWhiteSpacesTest.pdf")); RegexBasedLocationExtractionStrategy extractionStrategy = new RegexBasedLocationExtractionStrategy(" "); new PdfCanvasProcessor(extractionStrategy).processPageContent(pdfDocument.getPage(1)); List<IPdfTextLocation> locations = new ArrayList<>(extractionStrategy.getResultantLocations()); pdfDocument.close(); Assert.assertEquals(0, locations.size()); }