Refine search
private byte[] getDocumentIDBytes(COSArray documentIDArray) { //some documents may not have document id, see //test\encryption\encrypted_doc_no_id.pdf byte[] documentIDBytes; if( documentIDArray != null && documentIDArray.size() >= 1 ) { COSString id = (COSString)documentIDArray.getObject( 0 ); documentIDBytes = id.getBytes(); } else { documentIDBytes = new byte[0]; } return documentIDBytes; }
/** * The annotation rectangle, defining the location of the annotation on the page in default user space units. This * is usually required and should not return null on valid PDF documents. But where this is a parent form field with * children, such as radio button collections then the rectangle will be null. * * @return The Rect value of this annotation. */ public PDRectangle getRectangle() { COSArray rectArray = (COSArray) dictionary.getDictionaryObject(COSName.RECT); PDRectangle rectangle = null; if (rectArray != null) { if (rectArray.size() == 4 && rectArray.getObject(0) instanceof COSNumber && rectArray.getObject(1) instanceof COSNumber && rectArray.getObject(2) instanceof COSNumber && rectArray.getObject(3) instanceof COSNumber) { rectangle = new PDRectangle(rectArray); } else { LOG.warn(rectArray + " is not a rectangle array, returning null"); } } return rectangle; }
/** * This will take an COSArray of numbers and convert it to a float[]. * * @return This COSArray as an array of float numbers. */ public float[] toFloatArray() { float[] retval = new float[size()]; for (int i = 0; i < size(); i++) { COSBase base = getObject(i); retval[i] = base instanceof COSNumber ? ((COSNumber) base).floatValue() : 0; } return retval; }
/** * This will get the page for this destination. A page destination can either reference a page * (for a local destination) or a page number (when doing a remote destination to another PDF). * If this object is referencing by page number then this method will return null and * {@link #getPageNumber()} should be used. * * @return The page for this destination. */ public PDPage getPage() { PDPage retval = null; if( array.size() > 0 ) { COSBase page = array.getObject( 0 ); if( page instanceof COSDictionary ) { retval = new PDPage( (COSDictionary)page ); } } return retval; }
/** * This will get the page number for this destination. A page destination can either reference a * page (for a local destination) or a page number (when doing a remote destination to another * PDF). If this object is referencing by page number then this method will return that number, * otherwise -1 will be returned. * * @return The zero-based page number for this destination. */ public int getPageNumber() { int retval = -1; if( array.size() > 0 ) { COSBase page = array.getObject( 0 ); if( page instanceof COSNumber ) { retval = ((COSNumber)page).intValue(); } } return retval; }
/** * Creates a new ICC color space using the PDF array. * * @param iccArray the ICC stream object * @throws IOException if there is an error reading the ICC profile or if the parameter * is invalid. */ public PDICCBased(COSArray iccArray) throws IOException { if (iccArray.size() < 2) { throw new IOException("ICCBased colorspace array must have two elements"); } if (!(iccArray.getObject(1) instanceof COSStream)) { throw new IOException("ICCBased colorspace array must have a stream as second element"); } useOnlyAlternateColorSpace = System .getProperty("org.apache.pdfbox.rendering.UseAlternateInsteadOfICCColorSpace") != null; array = iccArray; stream = new PDStream((COSStream) iccArray.getObject(1)); loadICCProfile(); }
private static byte[] getBytesFromPacket(final COSArray cosArray) throws IOException { try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) { for (int i = 1; i < cosArray.size(); i += 2) { COSBase cosObj = cosArray.getObject(i); if (cosObj instanceof COSStream) { baos.write(getBytesFromStream((COSStream) cosObj.getCOSObject())); } } return baos.toByteArray(); } }
/** * Helper to get kids from malformed PDFs. * @param node page tree node * @return list of kids */ private List<COSDictionary> getKids(COSDictionary node) { List<COSDictionary> result = new ArrayList<>(); COSArray kids = node.getCOSArray(COSName.KIDS); if (kids == null) { // probably a malformed PDF return result; } for (int i = 0, size = kids.size(); i < size; i++) { COSBase base = kids.getObject(i); if (base instanceof COSDictionary) { result.add((COSDictionary) base); } else { LOG.warn("COSDictionary expected, but got " + base.getClass().getSimpleName()); } } return result; }
private void updatePageReferences(PDFCloneUtility cloner, COSArray parentTreeEntry, Map<COSDictionary, COSDictionary> objMapping) throws IOException { for (int i = 0; i < parentTreeEntry.size(); i++) { COSBase subEntry = parentTreeEntry.getObject(i); if (subEntry instanceof COSArray) { updatePageReferences(cloner, (COSArray) subEntry, objMapping); } else if (subEntry instanceof COSDictionary) { updatePageReferences(cloner, (COSDictionary) subEntry, objMapping); } } }
/** * This will get the dash pattern. * * @return null or the D value in the dictionary. */ public PDLineDashPattern getLineDashPattern() { PDLineDashPattern retval = null; COSBase dp = dict.getDictionaryObject( COSName.D ); if( dp instanceof COSArray && ((COSArray)dp).size() == 2) { COSBase dashArray = ((COSArray)dp).getObject(0); COSBase phase = ((COSArray)dp).getObject(1); if (dashArray instanceof COSArray && phase instanceof COSNumber) { retval = new PDLineDashPattern((COSArray) dashArray, ((COSNumber) phase).intValue()); } } return retval; }
/** * This will take an array of COSString and return a COSArrayList of * java.lang.String values. * * @param stringArray The existing name Array. * * @return The list of String objects. */ public static List<String> convertCOSStringCOSArrayToList( COSArray stringArray ) { List<String> retval = null; if( stringArray != null ) { List<String> string = new ArrayList<>(); for( int i=0; i<stringArray.size(); i++ ) { string.add( ((COSString)stringArray.getObject( i )).getString() ); } retval = new COSArrayList<>( string, stringArray ); } return retval; }
/** * Returns the dictionary's "Doc" entry, that is, a map of key value pairs to be added to the document's JavaScript * name tree. * * @return Map of named "JavaScript" dictionaries. */ public Map<String, PDActionJavaScript> getDoc() { Map<String, PDActionJavaScript> map = new LinkedHashMap<>(); COSArray array = (COSArray) dictionary.getDictionaryObject(COSName.DOC); if (array == null) { return null; } for (int i = 0; i < array.size(); i++) { PDActionFactory.createAction((COSDictionary) array.getObject(i)); } return map; }
/** * This will take an array of COSName and return a COSArrayList of * java.lang.String values. * * @param nameArray The existing name Array. * * @return The list of String objects. */ public static List<String> convertCOSNameCOSArrayToList( COSArray nameArray ) { List<String> retval = null; if( nameArray != null ) { List<String>names = new ArrayList<>(); for( int i=0; i<nameArray.size(); i++ ) { names.add( ((COSName)nameArray.getObject( i )).getName() ); } retval = new COSArrayList<>( names, nameArray ); } return retval; }
/** * Returns the user properties. * * @return the user properties */ public List<PDUserProperty> getOwnerUserProperties() { COSArray p = (COSArray) this.getCOSObject() .getDictionaryObject(COSName.P); List<PDUserProperty> properties = new ArrayList<>(p.size()); for (int i = 0; i < p.size(); i++) { properties.add( new PDUserProperty((COSDictionary) p.getObject(i), this)); } return properties; }
/** * Get the viewports. * * @return a list of viewports or null if there is no /VP entry. */ public List<PDViewportDictionary> getViewports() { COSBase base = page.getDictionaryObject(COSName.VP); if (!(base instanceof COSArray)) { return null; } COSArray array = (COSArray) base; List<PDViewportDictionary> viewports = new ArrayList<>(); for (int i = 0; i < array.size(); ++i) { COSBase base2 = array.getObject(i); if (base2 instanceof COSDictionary) { viewports.add(new PDViewportDictionary((COSDictionary) base2)); } else { LOG.warn("Array element " + base2 + " is skipped, must be a (viewport) dictionary"); } } return viewports; }
/** * This will take an array of COSNumbers and return a COSArrayList of * java.lang.Float values. * * @param floatArray The existing float Array. * * @return The list of Float objects. */ public static List<Float> convertFloatCOSArrayToList( COSArray floatArray ) { List<Float> retval = null; if( floatArray != null ) { List<Float> numbers = new ArrayList<>(floatArray.size()); for( int i=0; i<floatArray.size(); i++ ) { COSBase base = floatArray.getObject(i); if (base instanceof COSNumber) { numbers.add(((COSNumber) base).floatValue()); } else { numbers.add(null); } } retval = new COSArrayList<>( numbers, floatArray ); } return retval; }
/** * Get one or more disjoint paths that make this annotation. * * @return An array of arrays, each representing a stroked path. Each array shall be a series of * alternating horizontal and vertical coordinates. */ public float[][] getInkList() { COSBase base = getCOSObject().getDictionaryObject(COSName.INKLIST); if (base instanceof COSArray) { COSArray array = (COSArray) base; float[][] inkList = new float[array.size()][]; for (int i = 0; i < array.size(); ++i) { COSBase base2 = array.getObject(i); if (base2 instanceof COSArray) { inkList[i] = ((COSArray) array.getObject(i)).toFloatArray(); } else { inkList[i] = new float[0]; } } return inkList; } return new float[0][0]; }
/** * Returns the content streams which make up this page. * * @return content stream iterator */ public Iterator<PDStream> getContentStreams() { List<PDStream> streams = new ArrayList<>(); COSBase base = page.getDictionaryObject(COSName.CONTENTS); if (base instanceof COSStream) { streams.add(new PDStream((COSStream) base)); } else if (base instanceof COSArray && ((COSArray) base).size() > 0) { COSArray array = (COSArray)base; for (int i = 0; i < array.size(); i++) { COSStream stream = (COSStream) array.getObject(i); streams.add(new PDStream(stream)); } } return streams.iterator(); }
/** * Return the children of this node. This list will contain PDNameTreeNode objects. * * @return The list of children or null if there are no children. */ public List<PDNameTreeNode<T>> getKids() { List<PDNameTreeNode<T>> retval = null; COSArray kids = (COSArray)node.getDictionaryObject( COSName.KIDS ); if( kids != null ) { List<PDNameTreeNode<T>> pdObjects = new ArrayList<>(); for( int i=0; i<kids.size(); i++ ) { pdObjects.add( createChildNode( (COSDictionary)kids.getObject(i) ) ); } retval = new COSArrayList<>(pdObjects, kids); } return retval; }