use of com.itextpdf.kernel.geom.Rectangle in project i7j-pdfsweep by itext.
the class CleanUpAnnotationTest method cleanLinkAnnotation01.
@Test
public void cleanLinkAnnotation01() throws IOException, InterruptedException {
String input = inputPath + "cleanAnnotation.pdf";
String output = outputPath + "cleanAnnotation_Link01.pdf";
String cmp = inputPath + "cmp_cleanAnnotation_Link01.pdf";
List<PdfCleanUpLocation> cleanUpLocations = new ArrayList<PdfCleanUpLocation>();
PdfCleanUpLocation linkLoc = new PdfCleanUpLocation(1, new Rectangle(235, 740, 30, 16), ColorConstants.BLUE);
cleanUpLocations.add(linkLoc);
cleanUp(input, output, cleanUpLocations);
compareByContent(cmp, output, outputPath, "diff_Annotation_link01");
}
use of com.itextpdf.kernel.geom.Rectangle in project i7j-pdfsweep by itext.
the class CleanUpAnnotationTest method cleanHighlightAnnotation01.
@Test
public void cleanHighlightAnnotation01() throws IOException, InterruptedException {
String input = inputPath + "cleanAnnotation.pdf";
String output = outputPath + "cleanAnnotation_highlight01.pdf";
String cmp = inputPath + "cmp_cleanAnnotation_highlight01.pdf";
List<PdfCleanUpLocation> cleanUpLocations = new ArrayList<PdfCleanUpLocation>();
PdfCleanUpLocation highLightLoc = new PdfCleanUpLocation(1, new Rectangle(105, 500, 70, 10), ColorConstants.BLACK);
cleanUpLocations.add(highLightLoc);
cleanUp(input, output, cleanUpLocations);
compareByContent(cmp, output, outputPath, "diff_text_highlight01");
}
use of com.itextpdf.kernel.geom.Rectangle 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.geom.Rectangle in project i7j-pdfsweep by itext.
the class FilteredImagesCache method rectanglesEqualWithEps.
private boolean rectanglesEqualWithEps(List<Rectangle> cacheRects, List<Rectangle> keyRects) {
if (keyRects == null || cacheRects.size() != keyRects.size()) {
return false;
}
Set<Rectangle> cacheRectsSet = new LinkedHashSet<>(cacheRects);
for (Rectangle keyArea : keyRects) {
boolean found = false;
for (Rectangle cacheArea : cacheRectsSet) {
if (keyArea.equalsWithEpsilon(cacheArea)) {
found = true;
cacheRectsSet.remove(cacheArea);
break;
}
}
if (!found) {
break;
}
}
return cacheRectsSet.isEmpty();
}
use of com.itextpdf.kernel.geom.Rectangle in project i7j-pdfsweep by itext.
the class PdfCleanUpFilter method processImageDirectly.
/**
* Filters image content using direct manipulation over PDF image samples stream. Implemented according to ISO 32000-2,
* "8.9.3 Sample representation".
*
* @param image image XObject which will be filtered
* @param imageAreasToBeCleaned list of rectangle areas for clean up with coordinates in (0,1)x(0,1) space
* @return raw bytes of the PDF image samples stream which is already cleaned.
*/
private static byte[] processImageDirectly(PdfImageXObject image, List<Rectangle> imageAreasToBeCleaned) {
int X = 0;
int Y = 1;
int W = 2;
int H = 3;
byte[] originalImageBytes = image.getPdfObject().getBytes();
PdfNumber bpcVal = image.getPdfObject().getAsNumber(PdfName.BitsPerComponent);
if (bpcVal == null) {
throw new IllegalArgumentException("/BitsPerComponent entry is required for image dictionaries.");
}
int bpc = bpcVal.intValue();
if (bpc != 1 && bpc != 2 && bpc != 4 && bpc != 8 && bpc != 16) {
throw new IllegalArgumentException("/BitsPerComponent only allowed values are: 1, 2, 4, 8 and 16.");
}
double bytesInComponent = (double) bpc / 8;
int firstComponentInByte = 0;
if (bpc < 16) {
for (int i = 0; i < bpc; ++i) {
firstComponentInByte += (int) Math.pow(2, 7 - i);
}
}
double width = image.getWidth();
double height = image.getHeight();
int rowPadding = 0;
if ((width * bpc) % 8 > 0) {
rowPadding = (int) (8 - (width * bpc) % 8);
}
for (Rectangle rect : imageAreasToBeCleaned) {
int[] cleanImgRect = CleanUpHelperUtil.getImageRectToClean(rect, (int) width, (int) height);
for (int j = cleanImgRect[Y]; j < cleanImgRect[Y] + cleanImgRect[H]; ++j) {
for (int i = cleanImgRect[X]; i < cleanImgRect[X] + cleanImgRect[W]; ++i) {
// based on assumption that numOfComponents always equals 1, because this method is only for monochrome and grayscale images
double pixelPos = j * ((width * bpc + rowPadding) / 8) + i * bytesInComponent;
int pixelByteInd = (int) pixelPos;
byte byteWithSample = originalImageBytes[pixelByteInd];
if (bpc == 16) {
originalImageBytes[pixelByteInd] = 0;
originalImageBytes[pixelByteInd + 1] = 0;
} else {
int reset = ~(firstComponentInByte >> (int) ((pixelPos - pixelByteInd) * 8)) & 0xFF;
originalImageBytes[pixelByteInd] = (byte) (byteWithSample & reset);
}
}
}
}
return originalImageBytes;
}
Aggregations