use of com.itextpdf.kernel.geom.Rectangle in project i7j-pdfsweep by itext.
the class FilteredImagesCacheTest method filteredImagesCacheTest01.
@Test
public void filteredImagesCacheTest01() throws IOException, InterruptedException {
// basic test with reusing of xobjects
String input = inputPath + "multipleImageXObjectOccurrences.pdf";
String output = outputPath + "filteredImagesCacheTest01.pdf";
String cmp = inputPath + "cmp_filteredImagesCacheTest01.pdf";
PdfDocument pdfDocument = new PdfDocument(new PdfReader(input), new PdfWriter(output));
List<PdfCleanUpLocation> cleanUpLocations = new ArrayList<>();
for (int i = 0; i < pdfDocument.getNumberOfPages(); ++i) {
cleanUpLocations.add(new PdfCleanUpLocation(i + 1, new Rectangle(150, 300, 300, 150)));
}
cleanUp(pdfDocument, cleanUpLocations);
compareByContent(cmp, output, outputPath, "1.2");
assertNumberXObjects(output, 1);
}
use of com.itextpdf.kernel.geom.Rectangle in project i7j-pdfsweep by itext.
the class FilteredImagesCacheTest method filteredImagesCacheFlushingTest02.
@Test
public void filteredImagesCacheFlushingTest02() throws IOException, InterruptedException {
String input = inputPath + "severalImageXObjectOccurrences.pdf";
String output = outputPath + "filteredImagesCacheFlushingTest02.pdf";
String cmp = inputPath + "cmp_filteredImagesCacheFlushingTest02.pdf";
PdfDocument pdfDocument = new PdfDocument(new PdfReader(input), new PdfWriter(output));
PdfCleanUpTool cleanUpTool = new PdfCleanUpTool(pdfDocument);
cleanUpTool.addCleanupLocation(new PdfCleanUpLocation(1, new Rectangle(150, 300, 300, 150)));
cleanUpTool.cleanUp();
PdfImageXObject img = pdfDocument.getPage(1).getResources().getImage(new PdfName("Im1"));
img.makeIndirect(pdfDocument).flush();
cleanUpTool.addCleanupLocation(new PdfCleanUpLocation(2, new Rectangle(150, 300, 300, 150)));
cleanUpTool.cleanUp();
cleanUpTool.addCleanupLocation(new PdfCleanUpLocation(3, new Rectangle(150, 300, 300, 150)));
cleanUpTool.cleanUp();
pdfDocument.close();
compareByContent(cmp, output, outputPath, "1.2");
assertNumberXObjects(output, 1);
}
use of com.itextpdf.kernel.geom.Rectangle 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.geom.Rectangle in project i7j-pdfsweep by itext.
the class PdfCleanUpTool method cleanUpPage.
/**
* Cleans a page from the document by erasing all the areas which
* are provided or extracted from redaction annotations.
*
* @param pageNumber the page to be cleaned up
* @param cleanUpLocations the locations to be cleaned up
*/
private void cleanUpPage(int pageNumber, List<PdfCleanUpLocation> cleanUpLocations) {
if (cleanUpLocations.size() == 0) {
return;
}
List<Rectangle> regions = new ArrayList<>();
for (PdfCleanUpLocation cleanUpLocation : cleanUpLocations) {
regions.add(cleanUpLocation.getRegion());
}
PdfPage page = pdfDocument.getPage(pageNumber);
PdfCleanUpProcessor cleanUpProcessor = new PdfCleanUpProcessor(regions, pdfDocument);
cleanUpProcessor.setFilteredImagesCache(filteredImagesCache);
cleanUpProcessor.processPageContent(page);
if (processAnnotations) {
cleanUpProcessor.processPageAnnotations(page, regions, redactAnnotations != null);
}
PdfCanvas pageCleanedContents = cleanUpProcessor.popCleanedCanvas();
page.put(PdfName.Contents, pageCleanedContents.getContentStream());
page.setResources(pageCleanedContents.getResources());
colorCleanedLocations(pageCleanedContents, cleanUpLocations);
}
use of com.itextpdf.kernel.geom.Rectangle in project i7j-pdfsweep by itext.
the class PdfCleanUpTool method extractLocationsFromSingleRedactAnnotation.
/**
* Note: annotation can consist not only of one area specified by the RECT entry, but also of multiple areas specified
* by the QuadPoints entry in the annotation dictionary.
*/
private void extractLocationsFromSingleRedactAnnotation(PdfRedactAnnotation redactAnnotation) {
List<Rectangle> regions;
PdfArray quadPoints = redactAnnotation.getQuadPoints();
if (quadPoints != null && !quadPoints.isEmpty()) {
regions = translateQuadPointsToRectangles(quadPoints);
} else {
regions = new ArrayList<>();
regions.add(redactAnnotation.getRectangle().toRectangle());
}
redactAnnotations.put(redactAnnotation, regions);
int page = pdfDocument.getPageNumber(redactAnnotation.getPage());
Color cleanUpColor = redactAnnotation.getInteriorColor();
PdfDictionary ro = redactAnnotation.getRedactRolloverAppearance();
if (ro != null) {
cleanUpColor = null;
}
for (Rectangle region : regions) {
addCleanupLocation(new PdfCleanUpLocation(page, region, cleanUpColor));
}
}
Aggregations