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;
}
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();
}
}
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);
}
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;
}
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;
}
Aggregations