Search in sources :

Example 6 with PDAnnotationLink

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

the class Splitter method processAnnotations.

private void processAnnotations(PDPage imported) throws IOException {
    List<PDAnnotation> annotations = imported.getAnnotations();
    for (PDAnnotation annotation : annotations) {
        if (annotation instanceof PDAnnotationLink) {
            PDAnnotationLink link = (PDAnnotationLink) annotation;
            PDDestination destination = link.getDestination();
            if (destination == null && link.getAction() != null) {
                PDAction action = link.getAction();
                if (action instanceof PDActionGoTo) {
                    destination = ((PDActionGoTo) action).getDestination();
                }
            }
            if (destination instanceof PDPageDestination) {
                // TODO preserve links to pages within the splitted result
                ((PDPageDestination) destination).setPage(null);
            }
        }
        // TODO preserve links to pages within the splitted result
        annotation.setPage(null);
    }
}
Also used : PDAction(org.apache.pdfbox.pdmodel.interactive.action.PDAction) PDAnnotation(org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation) PDPageDestination(org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageDestination) PDDestination(org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDDestination) PDAnnotationLink(org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink) PDActionGoTo(org.apache.pdfbox.pdmodel.interactive.action.PDActionGoTo)

Example 7 with PDAnnotationLink

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

the class PDLinkAppearanceHandler method generateNormalAppearance.

@Override
public void generateNormalAppearance() {
    PDAnnotationLink annotation = (PDAnnotationLink) getAnnotation();
    if (annotation.getRectangle() == null) {
        // 660402-p1-AnnotationEmptyRect.pdf has /Rect entry with 0 elements
        return;
    }
    // Adobe doesn't generate an appearance for a link annotation
    float lineWidth = getLineWidth();
    try {
        try (PDAppearanceContentStream contentStream = getNormalAppearanceAsContentStream()) {
            PDColor color = annotation.getColor();
            if (color == null) {
                // spec is unclear, but black is what Adobe does
                color = new PDColor(new float[] { 0 }, PDDeviceGray.INSTANCE);
            }
            boolean hasStroke = contentStream.setStrokingColorOnDemand(color);
            contentStream.setBorderLine(lineWidth, annotation.getBorderStyle());
            // setBorderLine(), or use AnnotationBorder class
            if (annotation.getBorderStyle() == null) {
                COSArray border = annotation.getBorder();
                if (border.size() > 3 && border.getObject(3) instanceof COSArray) {
                    contentStream.setLineDashPattern(((COSArray) border.getObject(3)).toFloatArray(), 0);
                }
            }
            // the differences rectangle
            // TODO: this only works for border effect solid. Cloudy needs a different approach.
            setRectDifference(lineWidth);
            // Acrobat applies a padding to each side of the bbox so the line is completely within
            // the bbox.
            PDRectangle borderEdge = getPaddedRectangle(getRectangle(), lineWidth / 2);
            float[] pathsArray = annotation.getQuadPoints();
            if (pathsArray != null) {
                // QuadPoints shall be ignored if any coordinate in the array lies outside
                // the region specified by Rect.
                PDRectangle rect = annotation.getRectangle();
                for (int i = 0; i < pathsArray.length / 2; ++i) {
                    if (!rect.contains(pathsArray[i * 2], pathsArray[i * 2 + 1])) {
                        LOG.warn("At least one /QuadPoints entry (" + pathsArray[i * 2] + ";" + pathsArray[i * 2 + 1] + ") is outside of rectangle, " + rect + ", /QuadPoints are ignored and /Rect is used instead");
                        pathsArray = null;
                        break;
                    }
                }
            }
            if (pathsArray == null) {
                // Convert rectangle coordinates as if it was a /QuadPoints entry
                pathsArray = new float[8];
                pathsArray[0] = borderEdge.getLowerLeftX();
                pathsArray[1] = borderEdge.getLowerLeftY();
                pathsArray[2] = borderEdge.getUpperRightX();
                pathsArray[3] = borderEdge.getLowerLeftY();
                pathsArray[4] = borderEdge.getUpperRightX();
                pathsArray[5] = borderEdge.getUpperRightY();
                pathsArray[6] = borderEdge.getLowerLeftX();
                pathsArray[7] = borderEdge.getUpperRightY();
            }
            int of = 0;
            while (of + 7 < pathsArray.length) {
                if (annotation.getBorderStyle() != null && annotation.getBorderStyle().getStyle().equals(PDBorderStyleDictionary.STYLE_UNDERLINE)) {
                    contentStream.moveTo(pathsArray[of], pathsArray[of + 1]);
                    contentStream.lineTo(pathsArray[of + 2], pathsArray[of + 3]);
                } else {
                    contentStream.moveTo(pathsArray[of], pathsArray[of + 1]);
                    contentStream.lineTo(pathsArray[of + 2], pathsArray[of + 3]);
                    contentStream.lineTo(pathsArray[of + 4], pathsArray[of + 5]);
                    contentStream.lineTo(pathsArray[of + 6], pathsArray[of + 7]);
                    contentStream.closePath();
                }
                of += 8;
            }
            contentStream.drawShape(lineWidth, hasStroke, false);
        }
    } catch (IOException e) {
        LOG.error(e);
    }
}
Also used : COSArray(org.apache.pdfbox.cos.COSArray) PDAppearanceContentStream(org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceContentStream) PDRectangle(org.apache.pdfbox.pdmodel.common.PDRectangle) IOException(java.io.IOException) PDAnnotationLink(org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink) PDColor(org.apache.pdfbox.pdmodel.graphics.color.PDColor)

Example 8 with PDAnnotationLink

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

the class PDLinkAppearanceHandler method getLineWidth.

/**
 * Get the line with of the border.
 *
 * Get the width of the line used to draw a border around the annotation.
 * This may either be specified by the annotation dictionaries Border
 * setting or by the W entry in the BS border style dictionary. If both are
 * missing the default width is 1.
 *
 * @return the line width
 */
// TODO: according to the PDF spec the use of the BS entry is annotation
// specific
// so we will leave that to be implemented by individual handlers.
// If at the end all annotations support the BS entry this can be handled
// here and removed from the individual handlers.
float getLineWidth() {
    PDAnnotationLink annotation = (PDAnnotationLink) getAnnotation();
    PDBorderStyleDictionary bs = annotation.getBorderStyle();
    if (bs != null) {
        return bs.getWidth();
    } else {
        COSArray borderCharacteristics = annotation.getBorder();
        if (borderCharacteristics.size() >= 3) {
            COSBase base = borderCharacteristics.getObject(2);
            if (base instanceof COSNumber) {
                return ((COSNumber) base).floatValue();
            }
        }
    }
    return 1;
}
Also used : COSArray(org.apache.pdfbox.cos.COSArray) COSNumber(org.apache.pdfbox.cos.COSNumber) COSBase(org.apache.pdfbox.cos.COSBase) PDAnnotationLink(org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink) PDBorderStyleDictionary(org.apache.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary)

Example 9 with PDAnnotationLink

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

the class TestPDPageAnnotationsFiltering method validateSelectedFew.

@Test
public void validateSelectedFew() throws IOException {
    List<PDAnnotation> annotations = page.getAnnotations(new AnnotationFilter() {

        @Override
        public boolean accept(PDAnnotation annotation) {
            return (annotation instanceof PDAnnotationLink || annotation instanceof PDAnnotationSquare);
        }
    });
    Assert.assertEquals(2, annotations.size());
    Assert.assertTrue(annotations.get(0) instanceof PDAnnotationSquare);
    Assert.assertTrue(annotations.get(1) instanceof PDAnnotationLink);
}
Also used : PDAnnotationSquare(org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationSquare) PDAnnotation(org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation) PDAnnotationLink(org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink) AnnotationFilter(org.apache.pdfbox.pdmodel.interactive.annotation.AnnotationFilter) Test(org.junit.Test)

Aggregations

PDAnnotationLink (org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink)9 PDAnnotation (org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation)5 PDRectangle (org.apache.pdfbox.pdmodel.common.PDRectangle)4 COSArray (org.apache.pdfbox.cos.COSArray)3 PDAction (org.apache.pdfbox.pdmodel.interactive.action.PDAction)3 PDActionURI (org.apache.pdfbox.pdmodel.interactive.action.PDActionURI)3 PDAnnotationSquare (org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationSquare)3 PDBorderStyleDictionary (org.apache.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary)3 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)2 PDPage (org.apache.pdfbox.pdmodel.PDPage)2 PDColor (org.apache.pdfbox.pdmodel.graphics.color.PDColor)2 PDActionGoTo (org.apache.pdfbox.pdmodel.interactive.action.PDActionGoTo)2 PDPageDestination (org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageDestination)2 Rectangle2D (java.awt.geom.Rectangle2D)1 File (java.io.File)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 COSBase (org.apache.pdfbox.cos.COSBase)1 COSDictionary (org.apache.pdfbox.cos.COSDictionary)1 COSNumber (org.apache.pdfbox.cos.COSNumber)1