Search in sources :

Example 11 with PDAnnotation

use of com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotation in project PdfBox-Android by TomRoush.

the class PDAcroForm method buildPagesWidgetsMap.

private Map<COSDictionary, Set<COSDictionary>> buildPagesWidgetsMap(List<PDField> fields) throws IOException {
    Map<COSDictionary, Set<COSDictionary>> pagesAnnotationsMap = new HashMap<COSDictionary, Set<COSDictionary>>();
    boolean hasMissingPageRef = false;
    for (PDField field : fields) {
        List<PDAnnotationWidget> widgets = field.getWidgets();
        for (PDAnnotationWidget widget : widgets) {
            PDPage page = widget.getPage();
            if (page != null) {
                fillPagesAnnotationMap(pagesAnnotationsMap, page, widget);
            } else {
                hasMissingPageRef = true;
            }
        }
    }
    if (!hasMissingPageRef) {
        return pagesAnnotationsMap;
    }
    // If there is a widget with a missing page reference we need to build the map reverse i.e.
    // from the annotations to the widget.
    Log.w("PdfBox-Android", "There has been a widget with a missing page reference, will check all page annotations");
    for (PDPage page : document.getPages()) {
        for (PDAnnotation annotation : page.getAnnotations()) {
            if (annotation instanceof PDAnnotationWidget) {
                fillPagesAnnotationMap(pagesAnnotationsMap, page, (PDAnnotationWidget) annotation);
            }
        }
    }
    return pagesAnnotationsMap;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) HashMap(java.util.HashMap) PDAnnotation(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotation) PDAnnotationWidget(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget)

Example 12 with PDAnnotation

use of com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotation in project PdfBox-Android by TomRoush.

the class PDPage method getAnnotations.

/**
 * This will return a list of the annotations for this page.
 *
 * @param annotationFilter the annotation filter provided allowing to filter out specific annotations
 * @return List of the PDAnnotation objects, never null. The returned list is backed by the
 * annotations COSArray, so any adding or deleting in this list will change the document too.
 *
 * @throws IOException If there is an error while creating the annotation list.
 */
public List<PDAnnotation> getAnnotations(AnnotationFilter annotationFilter) throws IOException {
    COSBase base = page.getDictionaryObject(COSName.ANNOTS);
    if (base instanceof COSArray) {
        COSArray annots = (COSArray) base;
        List<PDAnnotation> actuals = new ArrayList<PDAnnotation>();
        for (int i = 0; i < annots.size(); i++) {
            COSBase item = annots.getObject(i);
            if (item == null) {
                continue;
            }
            PDAnnotation createdAnnotation = PDAnnotation.createAnnotation(item);
            if (annotationFilter.accept(createdAnnotation)) {
                actuals.add(createdAnnotation);
            }
        }
        return new COSArrayList<PDAnnotation>(actuals, annots);
    }
    return new COSArrayList<PDAnnotation>(page, COSName.ANNOTS);
}
Also used : COSArrayList(com.tom_roush.pdfbox.pdmodel.common.COSArrayList) COSArray(com.tom_roush.pdfbox.cos.COSArray) PDAnnotation(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotation) ArrayList(java.util.ArrayList) COSArrayList(com.tom_roush.pdfbox.pdmodel.common.COSArrayList) COSBase(com.tom_roush.pdfbox.cos.COSBase)

Example 13 with PDAnnotation

use of com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotation in project PdfBox-Android by TomRoush.

the class PDObjectReference method getReferencedObject.

/**
 * Gets a higher-level object for the referenced object.
 * Currently this method may return a {@link PDAnnotation},
 * a {@link PDXObject} or <code>null</code>.
 *
 * @return a higher-level object for the referenced object
 */
public COSObjectable getReferencedObject() {
    COSBase obj = this.getCOSObject().getDictionaryObject(COSName.OBJ);
    if (!(obj instanceof COSDictionary)) {
        return null;
    }
    try {
        if (obj instanceof COSStream) {
            // <-- TODO: valid?
            PDXObject xobject = PDXObject.createXObject(obj, null);
            if (xobject != null) {
                return xobject;
            }
        }
        COSDictionary objDictionary = (COSDictionary) obj;
        PDAnnotation annotation = PDAnnotation.createAnnotation(obj);
        /*
             * COSName.TYPE is optional, so if annotation is of type unknown and
             * COSName.TYPE is not COSName.ANNOT it still may be an annotation.
             * TODO shall we return the annotation object instead of null?
             * what else can be the target of the object reference?
             */
        if (!(annotation instanceof PDAnnotationUnknown) || COSName.ANNOT.equals(objDictionary.getDictionaryObject(COSName.TYPE))) {
            return annotation;
        }
    } catch (IOException exception) {
    // this can only happen if the target is an XObject.
    }
    return null;
}
Also used : COSStream(com.tom_roush.pdfbox.cos.COSStream) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) PDAnnotation(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotation) COSBase(com.tom_roush.pdfbox.cos.COSBase) IOException(java.io.IOException) PDAnnotationUnknown(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationUnknown) PDXObject(com.tom_roush.pdfbox.pdmodel.graphics.PDXObject)

Example 14 with PDAnnotation

use of com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotation in project PdfBox-Android by TomRoush.

the class PageDrawer method drawPage.

/**
 * Draws the page to the requested Canvas.
 *
 * @param p The paint.
 * @param c The canvas to draw onto.
 * @param pageSize The size of the page to draw.
 * @throws IOException If there is an IO error while drawing the page.
 */
public void drawPage(Paint p, Canvas c, PDRectangle pageSize) throws IOException {
    paint = p;
    canvas = c;
    xform = new AffineTransform(canvas.getMatrix());
    // initialClip = graphics.getClip(); TODO: PdfBox-Android
    this.pageSize = pageSize;
    setRenderingHints();
    canvas.translate(0, pageSize.getHeight());
    canvas.scale(1, -1);
    // adjust for non-(0,0) crop box
    canvas.translate(-pageSize.getLowerLeftX(), -pageSize.getLowerLeftY());
    canvas.save();
    processPage(getPage());
    for (PDAnnotation annotation : getPage().getAnnotations(annotationFilter)) {
        showAnnotation(annotation);
    }
}
Also used : PDAnnotation(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotation) AffineTransform(com.tom_roush.harmony.awt.geom.AffineTransform)

Example 15 with PDAnnotation

use of com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotation in project PdfBox-Android by TomRoush.

the class MergeAnnotationsTest method testLinkAnnotations.

/*
     * PDFBOX-1065 Ensure that after merging the PDFs there are all link
     * annotations and they point to the correct page.
     */
@Test
public void testLinkAnnotations() throws IOException {
    // Merge the PDFs from PDFBOX-1065
    PDFMergerUtility merger = new PDFMergerUtility();
    InputStream is1 = null;
    InputStream is2 = null;
    try {
        File file1 = new File(TARGET_PDF_DIR, "PDFBOX-1065-1.pdf");
        assumeTrue(file1.exists());
        is1 = new FileInputStream(file1);
        File file2 = new File(TARGET_PDF_DIR, "PDFBOX-1065-2.pdf");
        assumeTrue(file2.exists());
        is2 = new FileInputStream(file2);
        File pdfOutput = new File(OUT_DIR, "PDFBOX-1065.pdf");
        merger.setDestinationFileName(pdfOutput.getAbsolutePath());
        merger.addSource(is1);
        merger.addSource(is2);
        merger.mergeDocuments(null);
        // Test merge result
        PDDocument mergedPDF = PDDocument.load(pdfOutput);
        assertEquals("There shall be 6 pages", 6, mergedPDF.getNumberOfPages());
        PDDocumentNameDestinationDictionary destinations = mergedPDF.getDocumentCatalog().getDests();
        // Each document has 3 annotations with 2 entries in the /Dests dictionary per annotation. One for the
        // source and one for the target.
        assertEquals("There shall be 12 entries", 12, destinations.getCOSObject().entrySet().size());
        List<PDAnnotation> sourceAnnotations01 = mergedPDF.getPage(0).getAnnotations();
        List<PDAnnotation> sourceAnnotations02 = mergedPDF.getPage(3).getAnnotations();
        List<PDAnnotation> targetAnnotations01 = mergedPDF.getPage(2).getAnnotations();
        List<PDAnnotation> targetAnnotations02 = mergedPDF.getPage(5).getAnnotations();
        // Test for the first set of annotations to be merged an linked correctly
        assertEquals("There shall be 3 source annotations at the first page", 3, sourceAnnotations01.size());
        assertEquals("There shall be 3 source annotations at the third page", 3, targetAnnotations01.size());
        assertTrue("The annotations shall match to each other", testAnnotationsMatch(sourceAnnotations01, targetAnnotations01));
        // Test for the second set of annotations to be merged an linked correctly
        assertEquals("There shall be 3 source annotations at the first page", 3, sourceAnnotations02.size());
        assertEquals("There shall be 3 source annotations at the third page", 3, targetAnnotations02.size());
        assertTrue("The annotations shall match to each other", testAnnotationsMatch(sourceAnnotations02, targetAnnotations02));
        mergedPDF.close();
    } finally {
        IOUtils.closeQuietly(is1);
        IOUtils.closeQuietly(is2);
    }
}
Also used : PDDocumentNameDestinationDictionary(com.tom_roush.pdfbox.pdmodel.PDDocumentNameDestinationDictionary) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDAnnotation(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotation) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Aggregations

PDAnnotation (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotation)21 Test (org.junit.Test)9 COSArray (com.tom_roush.pdfbox.cos.COSArray)6 PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)5 PDAnnotationLink (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink)5 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)4 AnnotationFilter (com.tom_roush.pdfbox.pdmodel.interactive.annotation.AnnotationFilter)4 COSBase (com.tom_roush.pdfbox.cos.COSBase)3 COSArrayList (com.tom_roush.pdfbox.pdmodel.common.COSArrayList)3 PDAnnotationSquareCircle (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationSquareCircle)3 PDAnnotationWidget (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget)3 ArrayList (java.util.ArrayList)3 COSName (com.tom_roush.pdfbox.cos.COSName)2 COSStream (com.tom_roush.pdfbox.cos.COSStream)2 PDDocumentCatalog (com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog)2 PDDocumentNameDestinationDictionary (com.tom_roush.pdfbox.pdmodel.PDDocumentNameDestinationDictionary)2 COSObjectable (com.tom_roush.pdfbox.pdmodel.common.COSObjectable)2 PDNumberTreeNode (com.tom_roush.pdfbox.pdmodel.common.PDNumberTreeNode)2 PDActionGoTo (com.tom_roush.pdfbox.pdmodel.interactive.action.PDActionGoTo)2 PDDestination (com.tom_roush.pdfbox.pdmodel.interactive.documentnavigation.destination.PDDestination)2