|
| 1 | +package com.itextpdf.kernel.pdf.canvas.parser.listener; |
| 2 | + |
| 3 | +import com.itextpdf.kernel.pdf.PdfDocument; |
| 4 | +import com.itextpdf.kernel.pdf.PdfReader; |
| 5 | +import com.itextpdf.kernel.pdf.canvas.parser.PdfCanvasProcessor; |
| 6 | +import com.itextpdf.test.ExtendedITextTest; |
| 7 | +import org.junit.Assert; |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +import java.io.File; |
| 11 | +import java.io.IOException; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.Collection; |
| 14 | +import java.util.Iterator; |
| 15 | +import java.util.List; |
| 16 | +import java.util.regex.Pattern; |
| 17 | + |
| 18 | +public class RegexBasedLocationExtractionStrategyTest extends ExtendedITextTest { |
| 19 | + |
| 20 | + private static final String sourceFolder = "./src/test/resources/com/itextpdf/kernel/parser/RegexBasedLocationExtractionStrategyTest/"; |
| 21 | + |
| 22 | + @Test |
| 23 | + public void test01() throws IOException { |
| 24 | + System.out.println(new File(sourceFolder).getAbsolutePath()); |
| 25 | + |
| 26 | + PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "in01.pdf")); |
| 27 | + |
| 28 | + // build strategy |
| 29 | + RegexBasedLocationExtractionStrategy extractionStrategy = new RegexBasedLocationExtractionStrategy(Pattern.compile("\\{\\{Signature\\}\\}")); |
| 30 | + |
| 31 | + // get locations |
| 32 | + List<IPdfTextLocation> locationList = new ArrayList<>(); |
| 33 | + for (int x = 1; x <= pdfDocument.getNumberOfPages(); x++) { |
| 34 | + new PdfCanvasProcessor(extractionStrategy).processPageContent(pdfDocument.getPage(x)); |
| 35 | + for(IPdfTextLocation location : extractionStrategy.getResultantLocations()) { |
| 36 | + if(location != null) { |
| 37 | + locationList.add(location); |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + // compare |
| 43 | + Assert.assertEquals(locationList.size(), 1); |
| 44 | + |
| 45 | + IPdfTextLocation loc = locationList.get(0); |
| 46 | + |
| 47 | + Assert.assertEquals(loc.getText(), "{{Signature}}"); |
| 48 | + Assert.assertEquals(23, (int) loc.getRectangle().getX()); |
| 49 | + Assert.assertEquals(375, (int) loc.getRectangle().getY()); |
| 50 | + Assert.assertEquals(52, (int) loc.getRectangle().getWidth()); |
| 51 | + Assert.assertEquals(11, (int) loc.getRectangle().getHeight()); |
| 52 | + |
| 53 | + // close |
| 54 | + pdfDocument.close(); |
| 55 | + } |
| 56 | +} |
0 commit comments