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;
}
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);
}
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;
}
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);
}
}
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);
}
}
Aggregations