Search in sources :

Example 1 with IPdfTextLocation

use of com.itextpdf.kernel.pdf.canvas.parser.listener.IPdfTextLocation 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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ILocationExtractionStrategy(com.itextpdf.kernel.pdf.canvas.parser.listener.ILocationExtractionStrategy) IPdfTextLocation(com.itextpdf.kernel.pdf.canvas.parser.listener.IPdfTextLocation) ArrayList(java.util.ArrayList) Rectangle(com.itextpdf.kernel.geom.Rectangle)

Example 2 with IPdfTextLocation

use of com.itextpdf.kernel.pdf.canvas.parser.listener.IPdfTextLocation in project i7j-pdfsweep by itext.

the class PdfAutoSweepTools method getPdfCleanUpLocations.

/**
 * Get all {@link PdfCleanUpLocation} objects from a given {@link PdfPage}.
 *
 * @param page the {@link PdfPage} to be processed
 *
 * @return a List of {@link PdfCleanUpLocation} objects
 */
public List<PdfCleanUpLocation> getPdfCleanUpLocations(PdfPage page) {
    // get document
    PdfDocument doc = page.getDocument();
    // create parser
    PdfDocumentContentParser parser = new PdfDocumentContentParser(doc);
    // get page number
    int pageNr = doc.getPageNumber(page);
    // process document
    List<PdfCleanUpLocation> toClean = new ArrayList<>();
    parser.processContent(pageNr, strategy);
    for (IPdfTextLocation rect : strategy.getResultantLocations()) {
        if (rect != null) {
            toClean.add(new PdfCleanUpLocation(pageNr, rect.getRectangle(), strategy.getRedactionColor(rect)));
        }
    }
    // reset strategy for next iteration
    resetStrategy();
    // return
    return toClean;
}
Also used : PdfDocumentContentParser(com.itextpdf.kernel.pdf.canvas.parser.PdfDocumentContentParser) IPdfTextLocation(com.itextpdf.kernel.pdf.canvas.parser.listener.IPdfTextLocation) PdfCleanUpLocation(com.itextpdf.pdfcleanup.PdfCleanUpLocation) ArrayList(java.util.ArrayList) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument)

Example 3 with IPdfTextLocation

use of com.itextpdf.kernel.pdf.canvas.parser.listener.IPdfTextLocation 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;
}
Also used : PdfDocumentContentParser(com.itextpdf.kernel.pdf.canvas.parser.PdfDocumentContentParser) IPdfTextLocation(com.itextpdf.kernel.pdf.canvas.parser.listener.IPdfTextLocation) PdfCleanUpLocation(com.itextpdf.pdfcleanup.PdfCleanUpLocation) ArrayList(java.util.ArrayList) Rectangle(com.itextpdf.kernel.geom.Rectangle)

Aggregations

IPdfTextLocation (com.itextpdf.kernel.pdf.canvas.parser.listener.IPdfTextLocation)3 ArrayList (java.util.ArrayList)3 Rectangle (com.itextpdf.kernel.geom.Rectangle)2 PdfDocumentContentParser (com.itextpdf.kernel.pdf.canvas.parser.PdfDocumentContentParser)2 PdfCleanUpLocation (com.itextpdf.pdfcleanup.PdfCleanUpLocation)2 PdfDocument (com.itextpdf.kernel.pdf.PdfDocument)1 ILocationExtractionStrategy (com.itextpdf.kernel.pdf.canvas.parser.listener.ILocationExtractionStrategy)1 LinkedHashSet (java.util.LinkedHashSet)1