Search in sources :

Example 26 with Rectangle

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");
}
Also used : ArrayList(java.util.ArrayList) Rectangle(com.itextpdf.kernel.geom.Rectangle) IntegrationTest(com.itextpdf.test.annotations.type.IntegrationTest) ExtendedITextTest(com.itextpdf.test.ExtendedITextTest) Test(org.junit.Test)

Example 27 with Rectangle

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");
}
Also used : ArrayList(java.util.ArrayList) Rectangle(com.itextpdf.kernel.geom.Rectangle) IntegrationTest(com.itextpdf.test.annotations.type.IntegrationTest) ExtendedITextTest(com.itextpdf.test.ExtendedITextTest) Test(org.junit.Test)

Example 28 with Rectangle

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);
}
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 29 with Rectangle

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();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Rectangle(com.itextpdf.kernel.geom.Rectangle)

Example 30 with Rectangle

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;
}
Also used : Rectangle(com.itextpdf.kernel.geom.Rectangle) PdfNumber(com.itextpdf.kernel.pdf.PdfNumber) Point(com.itextpdf.kernel.geom.Point)

Aggregations

Rectangle (com.itextpdf.kernel.geom.Rectangle)118 ExtendedITextTest (com.itextpdf.test.ExtendedITextTest)76 IntegrationTest (com.itextpdf.test.annotations.type.IntegrationTest)76 Test (org.junit.Test)76 PdfString (com.itextpdf.kernel.pdf.PdfString)42 PdfDocument (com.itextpdf.kernel.pdf.PdfDocument)41 PdfWriter (com.itextpdf.kernel.pdf.PdfWriter)37 ArrayList (java.util.ArrayList)29 PdfReader (com.itextpdf.kernel.pdf.PdfReader)22 PdfCanvas (com.itextpdf.kernel.pdf.canvas.PdfCanvas)16 Paragraph (com.itextpdf.layout.element.Paragraph)15 PdfFont (com.itextpdf.kernel.font.PdfFont)12 Document (com.itextpdf.layout.Document)12 Canvas (com.itextpdf.layout.Canvas)11 PdfPage (com.itextpdf.kernel.pdf.PdfPage)10 ColumnDocumentRenderer (com.itextpdf.layout.ColumnDocumentRenderer)9 LogMessages (com.itextpdf.test.annotations.LogMessages)9 AreaBreak (com.itextpdf.layout.element.AreaBreak)8 PdfCleanUpLocation (com.itextpdf.pdfcleanup.PdfCleanUpLocation)8 HyphenationConfig (com.itextpdf.layout.hyphenation.HyphenationConfig)7