use of com.itextpdf.kernel.geom.Rectangle in project i7j-pdfsweep by itext.
the class CompositeCleanupStrategy method getResultantLocations.
@Override
public Collection<IPdfTextLocation> getResultantLocations() {
locations.clear();
// build return value
Set<IPdfTextLocation> retval = new LinkedHashSet<>();
for (int i = 0; i < strategies.size(); i++) {
ILocationExtractionStrategy s = strategies.get(i);
Collection<IPdfTextLocation> rects = s.getResultantLocations();
retval.addAll(rects);
locations.put(i, new HashSet<>(rects));
}
List<IPdfTextLocation> rectangles = new ArrayList<>(retval);
java.util.Collections.sort(rectangles, new Comparator<IPdfTextLocation>() {
@Override
public int compare(IPdfTextLocation l1, IPdfTextLocation l2) {
Rectangle r1 = l1.getRectangle();
Rectangle r2 = l2.getRectangle();
if (r1.getY() == r2.getY()) {
return r1.getX() == r2.getX() ? 0 : (r1.getX() < r2.getX() ? -1 : 1);
} else {
return r1.getY() < r2.getY() ? -1 : 1;
}
}
});
// return
return rectangles;
}
use of com.itextpdf.kernel.geom.Rectangle in project i7j-pdfsweep by itext.
the class PdfAutoSweepTools method getPdfCleanUpLocations.
/**
* Get all {@link PdfCleanUpLocation} objects from a given {@link PdfDocument}.
*
* @param doc the {@link PdfDocument} to be processed
*
* @return a List of {@link PdfCleanUpLocation} objects
*/
public List<PdfCleanUpLocation> getPdfCleanUpLocations(PdfDocument doc) {
PdfDocumentContentParser parser = new PdfDocumentContentParser(doc);
List<PdfCleanUpLocation> toClean = new ArrayList<>();
for (int pageNr = 1; pageNr <= doc.getNumberOfPages(); pageNr++) {
parser.processContent(pageNr, strategy);
for (IPdfTextLocation rect : strategy.getResultantLocations()) {
if (rect != null) {
toClean.add(new PdfCleanUpLocation(pageNr, rect.getRectangle(), strategy.getRedactionColor(rect)));
}
}
resetStrategy();
}
java.util.Collections.sort(toClean, new Comparator<PdfCleanUpLocation>() {
@Override
public int compare(PdfCleanUpLocation o1, PdfCleanUpLocation o2) {
if (o1.getPage() != o2.getPage()) {
return o1.getPage() < o2.getPage() ? -1 : 1;
}
Rectangle r1 = o1.getRegion();
Rectangle r2 = o2.getRegion();
if (r1.getY() == r2.getY()) {
return r1.getX() == r2.getX() ? 0 : (r1.getX() < r2.getX() ? -1 : 1);
} else {
return r1.getY() < r2.getY() ? -1 : 1;
}
}
});
return toClean;
}
use of com.itextpdf.kernel.geom.Rectangle in project i7j-pdfsweep by itext.
the class PdfCleanUpToolTest method autoSweepCleanUpWithAdditionalLocationTest.
@Test
public void autoSweepCleanUpWithAdditionalLocationTest() throws Exception {
String in = INPUT_PATH + "page229.pdf";
String out = OUTPUT_PATH + "autoSweepCleanUpWithAdditionalLocationTest.pdf";
String cmp = INPUT_PATH + "cmp_autoSweepCleanUpWithAdditionalLocationTest.pdf";
List<PdfCleanUpLocation> cleanUpLocations = new ArrayList<>();
cleanUpLocations.add(new PdfCleanUpLocation(1, new Rectangle(100, 560, 200, 30)));
CompositeCleanupStrategy strategy = new CompositeCleanupStrategy();
strategy.add(new RegexBasedCleanupStrategy(" (T|t)o ").setRedactionColor(ColorConstants.GREEN));
try (PdfReader reader = new PdfReader(in);
PdfWriter writer = new PdfWriter(out);
PdfDocument document = new PdfDocument(reader, writer)) {
PdfCleaner.autoSweepCleanUp(document, strategy, cleanUpLocations);
}
CompareTool cmpTool = new CompareTool();
String errorMessage = cmpTool.compareVisually(out, cmp, OUTPUT_PATH, "diff_autoSweepCleanUpWithAdditionalLocationTest_");
if (errorMessage != null) {
Assert.fail(errorMessage);
}
}
use of com.itextpdf.kernel.geom.Rectangle in project i7j-pdfsweep by itext.
the class PdfCleanUpToolTest method documentWithoutReaderTest.
@Test
public void documentWithoutReaderTest() {
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
List<PdfCleanUpLocation> cleanUpLocations = Arrays.asList(new PdfCleanUpLocation(1, new Rectangle(0, 0, 500, 500)));
Exception e = Assert.assertThrows(PdfException.class, () -> PdfCleaner.cleanUp(pdfDocument, cleanUpLocations));
Assert.assertEquals(CleanupExceptionMessageConstant.PDF_DOCUMENT_MUST_BE_OPENED_IN_STAMPING_MODE, e.getMessage());
}
use of com.itextpdf.kernel.geom.Rectangle in project i7j-pdfsweep by itext.
the class PdfCleanUpToolTest method cleanUpTest39.
@Test
public void cleanUpTest39() throws IOException, InterruptedException {
String input = INPUT_PATH + "corruptJpeg.pdf";
String output = OUTPUT_PATH + "corruptJpeg.pdf";
String cmp = INPUT_PATH + "cmp_corruptJpeg.pdf";
List<PdfCleanUpLocation> cleanUpLocations = Arrays.asList(new PdfCleanUpLocation(1, new Rectangle(100, 350, 100, 200), ColorConstants.ORANGE));
cleanUp(input, output, cleanUpLocations);
CleanUpImagesCompareTool cmpTool = new CleanUpImagesCompareTool();
String errorMessage = cmpTool.extractAndCompareImages(output, cmp, OUTPUT_PATH, "1.2");
String compareByContentResult = cmpTool.compareByContent(output, cmp, OUTPUT_PATH);
if (compareByContentResult != null) {
errorMessage += compareByContentResult;
}
if (!errorMessage.equals("")) {
Assert.fail(errorMessage);
}
}
Aggregations