/** * Tells you if the node contains an array. * * @return true if the object is a PdfArray */ public boolean isArray() { return object.isArray(); }
private void fillOrderRecursively(PdfArray orderArray, Set<PdfObject> order) { for (PdfObject orderItem : orderArray) { if (!orderItem.isArray()) { order.add(orderItem); } else { fillOrderRecursively((PdfArray) orderItem, order); } } }
private void fillOrderRecursively(PdfArray orderArray, Set<PdfObject> order) { for (PdfObject orderItem : orderArray) { if (!orderItem.isArray()) { order.add(orderItem); } else { fillOrderRecursively((PdfArray) orderItem, order); } } }
private PdfArray getDestArray(PdfObject obj) { if (obj == null) return null; if (obj.isArray()) return (PdfArray) obj; else if (obj.isDictionary()) { PdfArray arr = ((PdfDictionary) obj).getAsArray(PdfName.D); return arr; } return null; } }
private static String optionsArrayToString(PdfArray options) { StringBuffer stringBuffer = new StringBuffer(); for (PdfObject obj : options) { if (obj.isString()) { stringBuffer.append(((PdfString) obj).toUnicodeString()).append('\n'); } else if (obj.isArray()) { PdfObject element = ((PdfArray) obj).get(1); if (element.isString()) { stringBuffer.append(((PdfString) element).toUnicodeString()).append('\n'); } } } stringBuffer.deleteCharAt(stringBuffer.length() - 1); // last '\n' return stringBuffer.toString(); }
private static String optionsArrayToString(PdfArray options) { StringBuffer stringBuffer = new StringBuffer(); for (PdfObject obj : options) { if (obj.isString()) { stringBuffer.append(((PdfString) obj).toUnicodeString()).append('\n'); } else if (obj.isArray()) { PdfObject element = ((PdfArray) obj).get(1); if (element.isString()) { stringBuffer.append(((PdfString) element).toUnicodeString()).append('\n'); } } } stringBuffer.deleteCharAt(stringBuffer.length() - 1); // last '\n' return stringBuffer.toString(); }
private int removeKidObject(PdfObject kid) { PdfObject k = getK(); if (k == null || !k.isArray() && k != kid && k != kid.getIndirectReference()) { return -1; } int removedIndex = -1; if (k.isArray()) { PdfArray kidsArray = (PdfArray) k; removedIndex = removeObjectFromArray(kidsArray, kid); } if (!k.isArray() || k.isArray() && ((PdfArray) k).isEmpty()) { getPdfObject().remove(PdfName.K); removedIndex = 0; } setModified(); return removedIndex; }
private void initXfaForm(PdfObject xfa) throws IOException, ParserConfigurationException, SAXException { ByteArrayOutputStream bout = new ByteArrayOutputStream(); if (xfa.isArray()) { PdfArray ar = (PdfArray) xfa; for (int k = 1; k < ar.size(); k += 2) { PdfObject ob = ar.get(k); if (ob instanceof PdfStream) { byte[] b = ((PdfStream) ob).getBytes(); bout.write(b); } } } else if (xfa instanceof PdfStream) { byte[] b = ((PdfStream) xfa).getBytes(); bout.write(b); } bout.close(); initXfaForm(new ByteArrayInputStream(bout.toByteArray())); }
private void initXfaForm(PdfObject xfa) throws IOException, ParserConfigurationException, SAXException { ByteArrayOutputStream bout = new ByteArrayOutputStream(); if (xfa.isArray()) { PdfArray ar = (PdfArray) xfa; for (int k = 1; k < ar.size(); k += 2) { PdfObject ob = ar.get(k); if (ob instanceof PdfStream) { byte[] b = ((PdfStream) ob).getBytes(); bout.write(b); } } } else if (xfa instanceof PdfStream) { byte[] b = ((PdfStream) xfa).getBytes(); bout.write(b); } bout.close(); initXfaForm(new ByteArrayInputStream(bout.toByteArray())); }
/** * This methods adds new name in the Dests NameTree. It throws an exception, if the name already exists. * * @param key Name of the destination. * @param value An object destination refers to. Must be an array or a dictionary with key /D and array. * See ISO 32000-1 12.3.2.3 for more info. */ public void addNamedDestination(String key, PdfObject value) { checkClosingStatus(); if (value.isArray() && ((PdfArray)value).get(0).isNumber()) LoggerFactory.getLogger(PdfDocument.class).warn(LogMessageConstant.INVALID_DESTINATION_TYPE); catalog.addNamedDestination(key, value); }
public PdfLinkAnnotation setDestination(PdfObject destination) { if (getPdfObject().containsKey(PdfName.A)) { getPdfObject().remove(PdfName.A); logger.warn(LogMessageConstant.DESTINATION_NOT_PERMITTED_WHEN_ACTION_IS_SET); } if (destination.isArray() && ((PdfArray)destination).get(0).isNumber()) LoggerFactory.getLogger(PdfLinkAnnotation.class).warn(LogMessageConstant.INVALID_DESTINATION_TYPE); return (PdfLinkAnnotation) put(PdfName.Dest, destination); }
@Override public List<PdfStructureAttributes> getAttributesList() { ArrayList<PdfStructureAttributes> attributesList = new ArrayList<>(); PdfObject elemAttributesObj = getBackingElem().getAttributes(false); if (elemAttributesObj != null) { if (elemAttributesObj.isDictionary()) { attributesList.add(new PdfStructureAttributes((PdfDictionary) elemAttributesObj)); } else if (elemAttributesObj.isArray()) { PdfArray attributesArray = (PdfArray) elemAttributesObj; for (PdfObject attributeObj : attributesArray) { if (attributeObj.isDictionary()) { attributesList.add(new PdfStructureAttributes((PdfDictionary) attributeObj)); } } } } return attributesList; }
/** * Gets list of the direct kids of structure element. * If certain kid is flushed, there will be a {@code null} in the list on it's place. * * @return list of the direct kids of structure element. */ @Override public List<IStructureNode> getKids() { PdfObject k = getK(); List<IStructureNode> kids = new ArrayList<>(); if (k != null) { if (k.isArray()) { PdfArray a = (PdfArray) k; for (int i = 0; i < a.size(); i++) { addKidObjectToStructElemList(a.get(i), kids); } } else { addKidObjectToStructElemList(k, kids); } } return kids; }
/** * Gets list of the direct kids of StructTreeRoot. * If certain kid is flushed, there will be a {@code null} in the list on it's place. * * @return list of the direct kids of StructTreeRoot. */ @Override public List<IStructureNode> getKids() { PdfObject k = getPdfObject().get(PdfName.K); List<IStructureNode> kids = new ArrayList<>(); if (k != null) { if (k.isArray()) { PdfArray a = (PdfArray) k; for (int i = 0; i < a.size(); i++) { ifKidIsStructElementAddToList(a.get(i), kids); } } else { ifKidIsStructElementAddToList(k, kids); } } return kids; }
public IStructureNode removeKid(int index, boolean prepareForReAdding) { PdfObject k = getK(); if (k == null || !k.isArray() && index != 0) { throw new IndexOutOfBoundsException(); } if (k.isArray()) { PdfArray kidsArray = (PdfArray) k; k = kidsArray.get(index); kidsArray.remove(index); if (kidsArray.isEmpty()) { getPdfObject().remove(PdfName.K); } } else { getPdfObject().remove(PdfName.K); } setModified(); IStructureNode removedKid = convertPdfObjectToIPdfStructElem(k); PdfDocument doc = getDocument(); if (removedKid instanceof PdfMcr && doc != null && !prepareForReAdding) { doc.getStructTreeRoot().getParentTreeHandler().unregisterMcr((PdfMcr) removedKid); } return removedKid; }
public PdfArray getKidsObject() { PdfArray k = null; PdfObject kObj = getPdfObject().get(PdfName.K); if (kObj != null && kObj.isArray()) { k = (PdfArray) kObj; } if (k == null) { k = new PdfArray(); getPdfObject().put(PdfName.K, k); setModified(); if (kObj != null) { k.add(kObj); } } return k; }
/** * Reads an array. The tokeniser must be positioned past the "[" token. * @return an array * @throws IOException on error */ public PdfArray readArray() throws IOException { PdfArray array = new PdfArray(); while (true) { PdfObject obj = readObject(); if (!obj.isArray() && tokeniser.getTokenType() == PdfTokenizer.TokenType.EndArray) break; if (tokeniser.getTokenType() == PdfTokenizer.TokenType.EndDic && obj.getType() != PdfObject.DICTIONARY) tokeniser.throwError(PdfException.UnexpectedGtGt); array.add(obj); } return array; }
/** * Defines the sort order of the field (ascending or descending). * * @param ascending an array with every element corresponding with a name of a field. */ public PdfCollectionSort setSortOrder(boolean[] ascending) { PdfObject obj = getPdfObject().get(PdfName.S); if (obj.isArray()) { if (((PdfArray) obj).size() != ascending.length) { throw new PdfException(PdfException.NumberOfBooleansInTheArrayDoesntCorrespondWithTheNumberOfFields); } getPdfObject().put(PdfName.A, new PdfArray(ascending)); return this; } else { throw new PdfException(PdfException.YouNeedASingleBooleanForThisCollectionSortDictionary); } }
/** * Defines index of the current tag in the parent's kids list. * @return returns index of the current tag in the parent's kids list, or -1 * if either current tag is a root tag, parent is flushed or it wasn't possible to define index. */ public int getIndexInParentKidsList() { if (getCurrentStructElem().getPdfObject() == tagStructureContext.getRootTag().getPdfObject()) { return -1; } PdfStructElem parent = (PdfStructElem) getCurrentStructElem().getParent(); if (parent.isFlushed()) { return -1; } PdfObject k = parent.getK(); if (k == getCurrentStructElem().getPdfObject()) { return 0; } if (k.isArray()) { PdfArray kidsArr = (PdfArray) k; return kidsArr.indexOf(getCurrentStructElem().getPdfObject()); } return -1; }
static IntHashtable convertCompositeWidthsArray(PdfArray widthsArray) { IntHashtable res = new IntHashtable(); if (widthsArray == null) { return res; } for (int k = 0; k < widthsArray.size(); ++k) { int c1 = widthsArray.getAsNumber(k).intValue(); PdfObject obj = widthsArray.get(++k); if (obj.isArray()) { PdfArray subWidths = (PdfArray)obj; for (int j = 0; j < subWidths.size(); ++j) { int c2 = subWidths.getAsNumber(j).intValue(); res.put(c1++, c2); } } else { int c2 = ((PdfNumber)obj).intValue(); int w = widthsArray.getAsNumber(++k).intValue(); for (; c1 <= c2; ++c1) { res.put(c1, w); } } } return res; } }