Search in sources :

Example 1 with PdfException

use of com.itextpdf.kernel.exceptions.PdfException in project i7j-pdfsweep by itext.

the class PdfCleanUpProcessor method annotationIsToBeRedacted.

private boolean annotationIsToBeRedacted(PdfAnnotation annotation, Rectangle redactRegion) {
    // TODO(DEVSIX-1605,DEVSIX-1606,DEVSIX-1607,DEVSIX-1608,DEVSIX-1609)
    removeAnnotIfPartOverlap = true;
    PdfName annotationType = annotation.getPdfObject().getAsName(PdfName.Subtype);
    if (annotationType.equals(PdfName.Watermark)) {
        // TODO /FixedPrint entry effect is not fully investigated: DEVSIX-2471
        Logger logger = LoggerFactory.getLogger(PdfCleanUpProcessor.class);
        logger.warn(CleanUpLogMessageConstant.REDACTION_OF_ANNOTATION_TYPE_WATERMARK_IS_NOT_SUPPORTED);
    }
    PdfArray rectAsArray = annotation.getRectangle();
    Rectangle rect = null;
    if (rectAsArray != null) {
        rect = rectAsArray.toRectangle();
    }
    boolean annotationIsToBeRedacted = processAnnotationRectangle(redactRegion, rect);
    // Special processing for some types of annotations.
    if (PdfName.Link.equals(annotationType)) {
        PdfArray quadPoints = ((PdfLinkAnnotation) annotation).getQuadPoints();
        if (quadPointsForLinkAnnotationAreValid(rect, quadPoints)) {
            annotationIsToBeRedacted = processAnnotationQuadPoints(redactRegion, quadPoints);
        }
    } else if (annotationType.equals(PdfName.Highlight) || annotationType.equals(PdfName.Underline) || annotationType.equals(PdfName.Squiggly) || annotationType.equals(PdfName.StrikeOut)) {
        PdfArray quadPoints = ((PdfTextMarkupAnnotation) annotation).getQuadPoints();
        // The annotation dictionary’s AP entry, if present, shall take precedence over QuadPoints.
        if (quadPoints != null && annotation.getAppearanceDictionary() == null) {
            try {
                annotationIsToBeRedacted = processAnnotationQuadPoints(redactRegion, quadPoints);
            } catch (PdfException ignored) {
            // if quad points array cannot be processed, simply ignore it
            }
        }
    } else if (annotationType.equals(PdfName.Line)) {
        PdfArray line = ((PdfLineAnnotation) annotation).getLine();
        if (line != null) {
            Rectangle drawnLineRectangle = line.toRectangle();
            // Line annotation might contain line leaders, so let's double check overlapping with /Rect area, for simplicity.
            // TODO DEVSIX-1607
            annotationIsToBeRedacted = annotationIsToBeRedacted || processAnnotationRectangle(redactRegion, drawnLineRectangle);
        }
    }
    return annotationIsToBeRedacted;
}
Also used : PdfArray(com.itextpdf.kernel.pdf.PdfArray) PdfException(com.itextpdf.kernel.exceptions.PdfException) PdfName(com.itextpdf.kernel.pdf.PdfName) PdfLinkAnnotation(com.itextpdf.kernel.pdf.annot.PdfLinkAnnotation) PdfLineAnnotation(com.itextpdf.kernel.pdf.annot.PdfLineAnnotation) Rectangle(com.itextpdf.kernel.geom.Rectangle) Logger(org.slf4j.Logger)

Example 2 with PdfException

use of com.itextpdf.kernel.exceptions.PdfException in project i7j-pdfsweep by itext.

the class PdfCleanUpTool method drawOverlayText.

private void drawOverlayText(PdfCanvas canvas, String overlayText, Rectangle annotRect, PdfBoolean repeat, PdfString defaultAppearance, int justification) throws IOException {
    Map<String, List> parsedDA;
    try {
        parsedDA = parseDAParam(defaultAppearance);
    } catch (NullPointerException npe) {
        throw new PdfException(CleanupExceptionMessageConstant.DEFAULT_APPEARANCE_NOT_FOUND);
    }
    PdfFont font;
    float fontSize = 12;
    List fontArgs = parsedDA.get("Tf");
    PdfDictionary formDictionary = pdfDocument.getCatalog().getPdfObject().getAsDictionary(PdfName.AcroForm);
    if (fontArgs != null && formDictionary != null) {
        font = getFontFromAcroForm((PdfName) fontArgs.get(0));
        fontSize = ((PdfNumber) fontArgs.get(1)).floatValue();
    } else {
        font = PdfFontFactory.createFont();
    }
    if (pdfDocument.isTagged()) {
        canvas.openTag(new CanvasArtifact());
    }
    Canvas modelCanvas = new Canvas(canvas, annotRect, false);
    Paragraph p = new Paragraph(overlayText).setFont(font).setFontSize(fontSize).setMargin(0);
    TextAlignment textAlignment = TextAlignment.LEFT;
    switch(justification) {
        case 1:
            textAlignment = TextAlignment.CENTER;
            break;
        case 2:
            textAlignment = TextAlignment.RIGHT;
            break;
        default:
    }
    p.setTextAlignment(textAlignment);
    List strokeColorArgs = parsedDA.get("StrokeColor");
    if (strokeColorArgs != null) {
        p.setStrokeColor(getColor(strokeColorArgs));
    }
    List fillColorArgs = parsedDA.get("FillColor");
    if (fillColorArgs != null) {
        p.setFontColor(getColor(fillColorArgs));
    }
    modelCanvas.add(p);
    if (repeat != null && repeat.getValue()) {
        boolean hasFull = modelCanvas.getRenderer().hasProperty(Property.FULL);
        boolean isFull = hasFull ? (boolean) modelCanvas.getRenderer().getPropertyAsBoolean(Property.FULL) : false;
        while (!isFull) {
            p.add(overlayText);
            LayoutArea previousArea = modelCanvas.getRenderer().getCurrentArea().clone();
            modelCanvas.relayout();
            if (modelCanvas.getRenderer().getCurrentArea().equals(previousArea)) {
                // Avoid infinite loop. This might be caused by the fact that the font does not support the text we want to show
                break;
            }
            hasFull = modelCanvas.getRenderer().hasProperty(Property.FULL);
            isFull = hasFull ? (boolean) modelCanvas.getRenderer().getPropertyAsBoolean(Property.FULL) : false;
        }
    }
    modelCanvas.getRenderer().flush();
    if (pdfDocument.isTagged()) {
        canvas.closeTag();
    }
}
Also used : PdfException(com.itextpdf.kernel.exceptions.PdfException) PdfName(com.itextpdf.kernel.pdf.PdfName) PdfDictionary(com.itextpdf.kernel.pdf.PdfDictionary) Canvas(com.itextpdf.layout.Canvas) PdfCanvas(com.itextpdf.kernel.pdf.canvas.PdfCanvas) PdfString(com.itextpdf.kernel.pdf.PdfString) Paragraph(com.itextpdf.layout.element.Paragraph) LayoutArea(com.itextpdf.layout.layout.LayoutArea) CanvasArtifact(com.itextpdf.kernel.pdf.canvas.CanvasArtifact) PdfFont(com.itextpdf.kernel.font.PdfFont) TextAlignment(com.itextpdf.layout.properties.TextAlignment) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with PdfException

use of com.itextpdf.kernel.exceptions.PdfException in project i7j-pdfsweep by itext.

the class PdfCleanUpFilter method filterFillPath.

/**
 * Note: this method will close all unclosed subpaths of the passed path.
 *
 * @param path        the PathRenderInfo object to be filtered.
 * @param ctm         a {@link com.itextpdf.kernel.geom.Path} transformation matrix.
 * @param fillingRule If the subpath is contour, pass any value.
 * @return a filtered {@link com.itextpdf.kernel.geom.Path} object.
 */
private com.itextpdf.kernel.geom.Path filterFillPath(com.itextpdf.kernel.geom.Path path, Matrix ctm, int fillingRule) {
    path.closeAllSubpaths();
    IClipper clipper = new DefaultClipper();
    ClipperBridge.addPath(clipper, path, PolyType.SUBJECT);
    for (Rectangle rectangle : regions) {
        try {
            Point[] transfRectVertices = transformPoints(ctm, true, getRectangleVertices(rectangle));
            ClipperBridge.addPolygonToClipper(clipper, transfRectVertices, PolyType.CLIP);
        } catch (PdfException e) {
            if (!(e.getCause() instanceof NoninvertibleTransformException)) {
                throw e;
            } else {
                logger.error(MessageFormatUtil.format(CleanUpLogMessageConstant.FAILED_TO_PROCESS_A_TRANSFORMATION_MATRIX));
            }
        }
    }
    PolyFillType fillType = PolyFillType.NON_ZERO;
    if (fillingRule == PdfCanvasConstants.FillingRule.EVEN_ODD) {
        fillType = PolyFillType.EVEN_ODD;
    }
    PolyTree resultTree = new PolyTree();
    clipper.execute(ClipType.DIFFERENCE, resultTree, fillType, PolyFillType.NON_ZERO);
    return ClipperBridge.convertToPath(resultTree);
}
Also used : NoninvertibleTransformException(com.itextpdf.kernel.geom.NoninvertibleTransformException) PdfException(com.itextpdf.kernel.exceptions.PdfException) PolyTree(com.itextpdf.kernel.pdf.canvas.parser.clipper.PolyTree) DefaultClipper(com.itextpdf.kernel.pdf.canvas.parser.clipper.DefaultClipper) Rectangle(com.itextpdf.kernel.geom.Rectangle) IClipper(com.itextpdf.kernel.pdf.canvas.parser.clipper.IClipper) Point(com.itextpdf.kernel.geom.Point) PolyFillType(com.itextpdf.kernel.pdf.canvas.parser.clipper.IClipper.PolyFillType)

Example 4 with PdfException

use of com.itextpdf.kernel.exceptions.PdfException in project i7j-pdfsweep by itext.

the class PdfCleanUpFilter method transformPoints.

private static Point[] transformPoints(Matrix transformationMatrix, boolean inverse, Point... points) {
    AffineTransform t = new AffineTransform(transformationMatrix.get(Matrix.I11), transformationMatrix.get(Matrix.I12), transformationMatrix.get(Matrix.I21), transformationMatrix.get(Matrix.I22), transformationMatrix.get(Matrix.I31), transformationMatrix.get(Matrix.I32));
    Point[] transformed = new Point[points.length];
    if (inverse) {
        try {
            t = t.createInverse();
        } catch (NoninvertibleTransformException e) {
            throw new PdfException(CleanupExceptionMessageConstant.NONINVERTIBLE_MATRIX_CANNOT_BE_PROCESSED, e);
        }
    }
    t.transform(points, 0, transformed, 0, points.length);
    return transformed;
}
Also used : NoninvertibleTransformException(com.itextpdf.kernel.geom.NoninvertibleTransformException) PdfException(com.itextpdf.kernel.exceptions.PdfException) AffineTransform(com.itextpdf.kernel.geom.AffineTransform) Point(com.itextpdf.kernel.geom.Point)

Example 5 with PdfException

use of com.itextpdf.kernel.exceptions.PdfException in project i7j-pdfsweep by itext.

the class PdfCleanUpEventListener method getEncounteredPath.

/**
 * Get the last encountered PathRenderInfo, then clears the internal buffer
 *
 * @return the PathRenderInfo object that was encountered when processing the last path rendering operation
 */
PathRenderInfo getEncounteredPath() {
    if (content.size() == 0) {
        throw new PdfException(pathDataExpected);
    }
    IEventData eventData = content.get(0);
    if (!(eventData instanceof PathRenderInfo)) {
        throw new PdfException(pathDataExpected);
    }
    content.clear();
    return (PathRenderInfo) eventData;
}
Also used : IEventData(com.itextpdf.kernel.pdf.canvas.parser.data.IEventData) PdfException(com.itextpdf.kernel.exceptions.PdfException) PathRenderInfo(com.itextpdf.kernel.pdf.canvas.parser.data.PathRenderInfo)

Aggregations

PdfException (com.itextpdf.kernel.exceptions.PdfException)7 IEventData (com.itextpdf.kernel.pdf.canvas.parser.data.IEventData)3 NoninvertibleTransformException (com.itextpdf.kernel.geom.NoninvertibleTransformException)2 Point (com.itextpdf.kernel.geom.Point)2 Rectangle (com.itextpdf.kernel.geom.Rectangle)2 PdfName (com.itextpdf.kernel.pdf.PdfName)2 ArrayList (java.util.ArrayList)2 PdfFont (com.itextpdf.kernel.font.PdfFont)1 AffineTransform (com.itextpdf.kernel.geom.AffineTransform)1 PdfArray (com.itextpdf.kernel.pdf.PdfArray)1 PdfDictionary (com.itextpdf.kernel.pdf.PdfDictionary)1 PdfString (com.itextpdf.kernel.pdf.PdfString)1 PdfLineAnnotation (com.itextpdf.kernel.pdf.annot.PdfLineAnnotation)1 PdfLinkAnnotation (com.itextpdf.kernel.pdf.annot.PdfLinkAnnotation)1 CanvasArtifact (com.itextpdf.kernel.pdf.canvas.CanvasArtifact)1 PdfCanvas (com.itextpdf.kernel.pdf.canvas.PdfCanvas)1 DefaultClipper (com.itextpdf.kernel.pdf.canvas.parser.clipper.DefaultClipper)1 IClipper (com.itextpdf.kernel.pdf.canvas.parser.clipper.IClipper)1 PolyFillType (com.itextpdf.kernel.pdf.canvas.parser.clipper.IClipper.PolyFillType)1 PolyTree (com.itextpdf.kernel.pdf.canvas.parser.clipper.PolyTree)1