Search in sources :

Example 1 with PDAnnotationHighlight

use of org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationHighlight in project pdfbox by apache.

the class PDHighlightAppearanceHandler method generateNormalAppearance.

@Override
public void generateNormalAppearance() {
    PDAnnotationHighlight annotation = (PDAnnotationHighlight) getAnnotation();
    PDRectangle rect = annotation.getRectangle();
    float[] pathsArray = annotation.getQuadPoints();
    if (pathsArray == null) {
        return;
    }
    AnnotationBorder ab = AnnotationBorder.getAnnotationBorder(annotation, annotation.getBorderStyle());
    PDColor color = annotation.getColor();
    if (color == null || color.getComponents().length == 0) {
        return;
    }
    // Adjust rectangle even if not empty, see PLPDF.com-MarkupAnnotations.pdf
    // TODO in a class structure this should be overridable
    // this is similar to polyline but different data type
    // TODO padding should consider the curves too; needs to know in advance where the curve is
    float minX = Float.MAX_VALUE;
    float minY = Float.MAX_VALUE;
    float maxX = Float.MIN_VALUE;
    float maxY = Float.MIN_VALUE;
    for (int i = 0; i < pathsArray.length / 2; ++i) {
        float x = pathsArray[i * 2];
        float y = pathsArray[i * 2 + 1];
        minX = Math.min(minX, x);
        minY = Math.min(minY, y);
        maxX = Math.max(maxX, x);
        maxY = Math.max(maxY, y);
    }
    // get the delta used for curves and use it for padding
    float maxDelta = 0;
    for (int i = 0; i < pathsArray.length / 8; ++i) {
        // one of the two is 0, depending whether the rectangle is
        // horizontal or vertical
        // if it is diagonal then... uh...
        float delta = Math.max((pathsArray[i + 0] - pathsArray[i + 4]) / 4, (pathsArray[i + 1] - pathsArray[i + 5]) / 4);
        maxDelta = Math.max(delta, maxDelta);
    }
    rect.setLowerLeftX(Math.min(minX - ab.width / 2 - maxDelta, rect.getLowerLeftX()));
    rect.setLowerLeftY(Math.min(minY - ab.width / 2 - maxDelta, rect.getLowerLeftY()));
    rect.setUpperRightX(Math.max(maxX + ab.width + maxDelta, rect.getUpperRightX()));
    rect.setUpperRightY(Math.max(maxY + ab.width + maxDelta, rect.getUpperRightY()));
    annotation.setRectangle(rect);
    try {
        try (PDAppearanceContentStream cs = getNormalAppearanceAsContentStream()) {
            prepareResources();
            PDExtendedGraphicsState r0 = new PDExtendedGraphicsState();
            PDExtendedGraphicsState r1 = new PDExtendedGraphicsState();
            r0.setAlphaSourceFlag(false);
            r0.setStrokingAlphaConstant(annotation.getConstantOpacity());
            r0.setNonStrokingAlphaConstant(annotation.getConstantOpacity());
            r1.setAlphaSourceFlag(false);
            r1.setBlendMode(BlendMode.MULTIPLY);
            cs.setGraphicsStateParameters(r0);
            cs.setGraphicsStateParameters(r1);
            // TODO replace with document.getDocument().createCOSStream()
            // or call new PDFormXObject(document)
            PDFormXObject frm1 = new PDFormXObject(new COSStream());
            PDFormXObject frm2 = new PDFormXObject(new COSStream());
            frm1.setResources(new PDResources());
            try (PDFormContentStream mwfofrmCS = new PDFormContentStream(frm1)) {
                mwfofrmCS.drawForm(frm2);
            }
            frm1.setBBox(annotation.getRectangle());
            COSDictionary groupDict = new COSDictionary();
            groupDict.setItem(COSName.S, COSName.TRANSPARENCY);
            // TODO PDFormXObject.setGroup() is missing
            frm1.getCOSObject().setItem(COSName.GROUP, groupDict);
            cs.drawForm(frm1);
            frm2.setBBox(annotation.getRectangle());
            try (PDFormContentStream frm2CS = new PDFormContentStream(frm2)) {
                frm2CS.setNonStrokingColor(color);
                int of = 0;
                while (of + 7 < pathsArray.length) {
                    // quadpoints spec sequence is incorrect, correct one is (4,5 0,1 2,3 6,7)
                    // https://stackoverflow.com/questions/9855814/pdf-spec-vs-acrobat-creation-quadpoints
                    // for "curvy" highlighting, two Bézier control points are used that seem to have a
                    // distance of about 1/4 of the height.
                    // note that curves won't appear if outside of the rectangle
                    float delta = 0;
                    if (Float.compare(pathsArray[of + 0], pathsArray[of + 4]) == 0 && Float.compare(pathsArray[of + 1], pathsArray[of + 3]) == 0 && Float.compare(pathsArray[of + 2], pathsArray[of + 6]) == 0 && Float.compare(pathsArray[of + 5], pathsArray[of + 7]) == 0) {
                        // horizontal highlight
                        delta = (pathsArray[of + 1] - pathsArray[of + 5]) / 4;
                    } else if (Float.compare(pathsArray[of + 1], pathsArray[of + 5]) == 0 && Float.compare(pathsArray[of + 0], pathsArray[of + 2]) == 0 && Float.compare(pathsArray[of + 3], pathsArray[of + 7]) == 0 && Float.compare(pathsArray[of + 4], pathsArray[of + 6]) == 0) {
                        // vertical highlight
                        delta = (pathsArray[of + 0] - pathsArray[of + 4]) / 4;
                    }
                    frm2CS.moveTo(pathsArray[of + 4], pathsArray[of + 5]);
                    if (Float.compare(pathsArray[of + 0], pathsArray[of + 4]) == 0) {
                        // horizontal highlight
                        frm2CS.curveTo(pathsArray[of + 4] - delta, pathsArray[of + 5] + delta, pathsArray[of + 0] - delta, pathsArray[of + 1] - delta, pathsArray[of + 0], pathsArray[of + 1]);
                    } else if (Float.compare(pathsArray[of + 5], pathsArray[of + 1]) == 0) {
                        // vertical highlight
                        frm2CS.curveTo(pathsArray[of + 4] + delta, pathsArray[of + 5] + delta, pathsArray[of + 0] - delta, pathsArray[of + 1] + delta, pathsArray[of + 0], pathsArray[of + 1]);
                    } else {
                        frm2CS.lineTo(pathsArray[of + 0], pathsArray[of + 1]);
                    }
                    frm2CS.lineTo(pathsArray[of + 2], pathsArray[of + 3]);
                    if (Float.compare(pathsArray[of + 2], pathsArray[of + 6]) == 0) {
                        // horizontal highlight
                        frm2CS.curveTo(pathsArray[of + 2] + delta, pathsArray[of + 3] - delta, pathsArray[of + 6] + delta, pathsArray[of + 7] + delta, pathsArray[of + 6], pathsArray[of + 7]);
                    } else if (Float.compare(pathsArray[of + 3], pathsArray[of + 7]) == 0) {
                        // vertical highlight
                        frm2CS.curveTo(pathsArray[of + 2] - delta, pathsArray[of + 3] - delta, pathsArray[of + 6] + delta, pathsArray[of + 7] - delta, pathsArray[of + 6], pathsArray[of + 7]);
                    } else {
                        frm2CS.lineTo(pathsArray[of + 6], pathsArray[of + 7]);
                    }
                    frm2CS.fill();
                    of += 8;
                }
            }
        }
    } catch (IOException ex) {
        LOG.error(ex);
    }
}
Also used : PDFormContentStream(org.apache.pdfbox.pdmodel.PDFormContentStream) COSStream(org.apache.pdfbox.cos.COSStream) COSDictionary(org.apache.pdfbox.cos.COSDictionary) PDExtendedGraphicsState(org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState) PDAnnotationHighlight(org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationHighlight) PDResources(org.apache.pdfbox.pdmodel.PDResources) IOException(java.io.IOException) PDColor(org.apache.pdfbox.pdmodel.graphics.color.PDColor) PDAppearanceContentStream(org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceContentStream) PDFormXObject(org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject) PDRectangle(org.apache.pdfbox.pdmodel.common.PDRectangle)

Example 2 with PDAnnotationHighlight

use of org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationHighlight in project pdfbox by apache.

the class AddAnnotations method main.

public static void main(String[] args) throws IOException {
    if (args.length != 1) {
        System.err.println("Usage: " + AddAnnotations.class.getName() + " <output-pdf>");
        System.exit(1);
    }
    try (PDDocument document = new PDDocument()) {
        PDPage page1 = new PDPage();
        PDPage page2 = new PDPage();
        PDPage page3 = new PDPage();
        document.addPage(page1);
        document.addPage(page2);
        document.addPage(page3);
        List<PDAnnotation> annotations = page1.getAnnotations();
        // Some basic reusable objects/constants
        // Annotations themselves can only be used once!
        PDColor red = new PDColor(new float[] { 1, 0, 0 }, PDDeviceRGB.INSTANCE);
        PDColor blue = new PDColor(new float[] { 0, 0, 1 }, PDDeviceRGB.INSTANCE);
        PDColor black = new PDColor(new float[] { 0, 0, 0 }, PDDeviceRGB.INSTANCE);
        PDBorderStyleDictionary borderThick = new PDBorderStyleDictionary();
        // 12th inch
        borderThick.setWidth(INCH / 12);
        PDBorderStyleDictionary borderThin = new PDBorderStyleDictionary();
        // 1 point
        borderThin.setWidth(INCH / 72);
        PDBorderStyleDictionary borderULine = new PDBorderStyleDictionary();
        borderULine.setStyle(PDBorderStyleDictionary.STYLE_UNDERLINE);
        // 1 point
        borderULine.setWidth(INCH / 72);
        float pw = page1.getMediaBox().getUpperRightX();
        float ph = page1.getMediaBox().getUpperRightY();
        // First add some text, two lines we'll add some annotations to this later
        PDFont font = PDType1Font.HELVETICA_BOLD;
        try (PDPageContentStream contents = new PDPageContentStream(document, page1)) {
            contents.beginText();
            contents.setFont(font, 18);
            contents.newLineAtOffset(INCH, ph - INCH - 18);
            contents.showText("PDFBox");
            contents.newLineAtOffset(0, -(INCH / 2));
            contents.showText("External URL");
            contents.newLineAtOffset(0, -(INCH / 2));
            contents.showText("Jump to page three");
            contents.endText();
        }
        // Now add the markup annotation, a highlight to PDFBox text
        PDAnnotationHighlight txtHighlight = new PDAnnotationHighlight();
        txtHighlight.setColor(new PDColor(new float[] { 0, 1, 1 }, PDDeviceRGB.INSTANCE));
        // 20% transparent
        txtHighlight.setConstantOpacity((float) 0.2);
        // Set the rectangle containing the markup
        float textWidth = font.getStringWidth("PDFBox") / 1000 * 18;
        PDRectangle position = new PDRectangle();
        position.setLowerLeftX(INCH);
        position.setLowerLeftY(ph - INCH - 18);
        position.setUpperRightX(INCH + textWidth);
        position.setUpperRightY(ph - INCH);
        txtHighlight.setRectangle(position);
        // work out the points forming the four corners of the annotations
        // set out in anti clockwise form (Completely wraps the text)
        // OK, the below doesn't match that description.
        // It's what acrobat 7 does and displays properly!
        float[] quads = new float[8];
        // x1
        quads[0] = position.getLowerLeftX();
        // y1
        quads[1] = position.getUpperRightY() - 2;
        // x2
        quads[2] = position.getUpperRightX();
        // y2
        quads[3] = quads[1];
        // x3
        quads[4] = quads[0];
        // y3
        quads[5] = position.getLowerLeftY() - 2;
        // x4
        quads[6] = quads[2];
        // y5
        quads[7] = quads[5];
        txtHighlight.setQuadPoints(quads);
        txtHighlight.setContents("Highlighted since it's important");
        annotations.add(txtHighlight);
        // Now add the link annotation, so the click on "External URL" works
        PDAnnotationLink txtLink = new PDAnnotationLink();
        txtLink.setBorderStyle(borderULine);
        // Set the rectangle containing the link
        textWidth = font.getStringWidth("External URL") / 1000 * 18;
        position = new PDRectangle();
        position.setLowerLeftX(INCH);
        // down a couple of points
        position.setLowerLeftY(ph - 1.5f * INCH - 20);
        position.setUpperRightX(INCH + textWidth);
        position.setUpperRightY(ph - 1.5f * INCH);
        txtLink.setRectangle(position);
        // add an action
        PDActionURI action = new PDActionURI();
        action.setURI("http://pdfbox.apache.org");
        txtLink.setAction(action);
        annotations.add(txtLink);
        // Now draw a few more annotations
        PDAnnotationCircle aCircle = new PDAnnotationCircle();
        aCircle.setContents("Circle Annotation");
        // Fill in circle in red
        aCircle.setInteriorColor(red);
        // The border itself will be blue
        aCircle.setColor(blue);
        aCircle.setBorderStyle(borderThin);
        // Place the annotation on the page, we'll make this 1" round
        // 3" down, 1" in on the page
        position = new PDRectangle();
        position.setLowerLeftX(INCH);
        // 1" height, 3" down
        position.setLowerLeftY(ph - 3 * INCH - INCH);
        // 1" in, 1" width
        position.setUpperRightX(2 * INCH);
        // 3" down
        position.setUpperRightY(ph - 3 * INCH);
        aCircle.setRectangle(position);
        annotations.add(aCircle);
        // Now a square annotation
        PDAnnotationSquare aSquare = new PDAnnotationSquare();
        aSquare.setContents("Square Annotation");
        // Outline in red, not setting a fill
        aSquare.setColor(red);
        aSquare.setBorderStyle(borderThick);
        // Place the annotation on the page, we'll make this 1" (72 points) square
        // 3.5" down, 1" in from the right on the page
        // Reuse the variable, but note it's a new object!
        position = new PDRectangle();
        // 1" in from right, 1" wide
        position.setLowerLeftX(pw - 2 * INCH);
        // 1" height, 3.5" down
        position.setLowerLeftY(ph - 3.5f * INCH - INCH);
        // 1" in from right
        position.setUpperRightX(pw - INCH);
        // 3.5" down
        position.setUpperRightY(ph - 3.5f * INCH);
        aSquare.setRectangle(position);
        annotations.add(aSquare);
        // Now we want to draw a line between the two, one end with an open arrow
        PDAnnotationLine aLine = new PDAnnotationLine();
        aLine.setEndPointEndingStyle(PDAnnotationLine.LE_OPEN_ARROW);
        aLine.setContents("Circle->Square");
        // Make the contents a caption on the line
        aLine.setCaption(true);
        // Set the rectangle containing the line
        // Reuse the variable, but note it's a new object!
        position = new PDRectangle();
        // 1" in + width of circle
        position.setLowerLeftX(2 * INCH);
        // 1" height, 3.5" down
        position.setLowerLeftY(ph - 3.5f * INCH - INCH);
        // 1" in from right, and width of square
        position.setUpperRightX(pw - INCH - INCH);
        // 3" down (top of circle)
        position.setUpperRightY(ph - 3 * INCH);
        aLine.setRectangle(position);
        // Now set the line position itself
        float[] linepos = new float[4];
        // x1 = rhs of circle
        linepos[0] = 2 * INCH;
        // y1 halfway down circle
        linepos[1] = ph - 3.5f * INCH;
        // x2 = lhs of square
        linepos[2] = pw - 2 * INCH;
        // y2 halfway down square
        linepos[3] = ph - 4 * INCH;
        aLine.setLine(linepos);
        aLine.setBorderStyle(borderThick);
        aLine.setColor(black);
        annotations.add(aLine);
        // Now add the link annotation, so the click on "Jump to page three" works
        PDAnnotationLink pageLink = new PDAnnotationLink();
        pageLink.setBorderStyle(borderULine);
        // Set the rectangle containing the link
        textWidth = font.getStringWidth("Jump to page three") / 1000 * 18;
        position = new PDRectangle();
        position.setLowerLeftX(INCH);
        // down a couple of points
        position.setLowerLeftY(ph - 2 * INCH - 20);
        position.setUpperRightX(INCH + textWidth);
        position.setUpperRightY(ph - 2 * INCH);
        pageLink.setRectangle(position);
        // add the GoTo action
        PDActionGoTo actionGoto = new PDActionGoTo();
        // see javadoc for other types of PDPageDestination
        PDPageDestination dest = new PDPageFitWidthDestination();
        // do not use setPageNumber(), this is for external destinations only
        dest.setPage(page3);
        actionGoto.setDestination(dest);
        pageLink.setAction(actionGoto);
        annotations.add(pageLink);
        PDAnnotationFreeText freeTextAnnotation = new PDAnnotationFreeText();
        PDColor yellow = new PDColor(new float[] { 1, 1, 0 }, PDDeviceRGB.INSTANCE);
        // this sets background only (contradicts PDF specification)
        freeTextAnnotation.setColor(yellow);
        position = new PDRectangle();
        position.setLowerLeftX(1 * INCH);
        position.setLowerLeftY(ph - 5f * INCH - 3 * INCH);
        position.setUpperRightX(pw - INCH);
        position.setUpperRightY(ph - 5f * INCH);
        freeTextAnnotation.setRectangle(position);
        freeTextAnnotation.setTitlePopup("Sophia Lorem");
        freeTextAnnotation.setSubject("Lorem ipsum");
        freeTextAnnotation.setContents("Lorem ipsum dolor sit amet, consetetur sadipscing elitr," + " sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam " + "erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea " + "rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum " + "dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, " + "sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam " + "erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea " + "rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum " + "dolor sit amet.");
        // Text and border in blue RGB color, "Helv" font, 20 point
        freeTextAnnotation.setDefaultAppearance("0 0 1 rg /Helv 20 Tf");
        // Quadding does not have any effect?!
        freeTextAnnotation.setQ(PDVariableText.QUADDING_RIGHT);
        annotations.add(freeTextAnnotation);
        // add the "Helv" font to the default resources
        PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm();
        if (acroForm == null) {
            acroForm = new PDAcroForm(document);
            document.getDocumentCatalog().setAcroForm(acroForm);
        }
        PDResources dr = acroForm.getDefaultResources();
        if (dr == null) {
            dr = new PDResources();
            acroForm.setDefaultResources(dr);
        }
        dr.put(COSName.getPDFName("Helv"), PDType1Font.HELVETICA);
        // but other applications may not.
        for (PDAnnotation ann : annotations) {
            ann.constructAppearances();
        }
        showPageNo(document, page1, "Page 1");
        showPageNo(document, page2, "Page 2");
        showPageNo(document, page3, "Page 3");
        // save the PDF
        document.save(args[0]);
    }
}
Also used : PDFont(org.apache.pdfbox.pdmodel.font.PDFont) PDAnnotationSquare(org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationSquare) PDPage(org.apache.pdfbox.pdmodel.PDPage) PDAnnotation(org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation) PDPageDestination(org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageDestination) PDAnnotationHighlight(org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationHighlight) PDResources(org.apache.pdfbox.pdmodel.PDResources) PDAcroForm(org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm) PDBorderStyleDictionary(org.apache.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary) PDColor(org.apache.pdfbox.pdmodel.graphics.color.PDColor) PDPageFitWidthDestination(org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageFitWidthDestination) PDAnnotationLine(org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLine) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) PDAnnotationCircle(org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationCircle) PDPageContentStream(org.apache.pdfbox.pdmodel.PDPageContentStream) PDRectangle(org.apache.pdfbox.pdmodel.common.PDRectangle) PDAnnotationFreeText(org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationFreeText) PDAnnotationLink(org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink) PDActionGoTo(org.apache.pdfbox.pdmodel.interactive.action.PDActionGoTo) PDActionURI(org.apache.pdfbox.pdmodel.interactive.action.PDActionURI)

Aggregations

PDResources (org.apache.pdfbox.pdmodel.PDResources)2 PDRectangle (org.apache.pdfbox.pdmodel.common.PDRectangle)2 PDColor (org.apache.pdfbox.pdmodel.graphics.color.PDColor)2 PDAnnotationHighlight (org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationHighlight)2 IOException (java.io.IOException)1 COSDictionary (org.apache.pdfbox.cos.COSDictionary)1 COSStream (org.apache.pdfbox.cos.COSStream)1 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)1 PDFormContentStream (org.apache.pdfbox.pdmodel.PDFormContentStream)1 PDPage (org.apache.pdfbox.pdmodel.PDPage)1 PDPageContentStream (org.apache.pdfbox.pdmodel.PDPageContentStream)1 PDFont (org.apache.pdfbox.pdmodel.font.PDFont)1 PDFormXObject (org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject)1 PDExtendedGraphicsState (org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState)1 PDActionGoTo (org.apache.pdfbox.pdmodel.interactive.action.PDActionGoTo)1 PDActionURI (org.apache.pdfbox.pdmodel.interactive.action.PDActionURI)1 PDAnnotation (org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation)1 PDAnnotationCircle (org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationCircle)1 PDAnnotationFreeText (org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationFreeText)1 PDAnnotationLine (org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLine)1