private PdfString iterateItems(PdfDictionary dictionary, Map<String, PdfObject> items, PdfString leftOver) { PdfArray names = dictionary.getAsArray(PdfName.Names); if (names != null) { for (int k = 0; k < names.size(); k++) { PdfString name; if (leftOver == null) name = names.getAsString(k++); else { name = leftOver; leftOver = null; } if (k < names.size()) { items.put(name.toUnicodeString(), names.get(k)); } else { return name; } } } else if ((names = dictionary.getAsArray(PdfName.Kids)) != null) { for (int k = 0; k < names.size(); k++) { PdfDictionary kid = names.getAsDictionary(k); leftOver = iterateItems(kid, items, leftOver); } } return null; }
/** * Converts a {@link com.itextpdf.kernel.pdf.PdfArray} to an array of longs * * @param pdfArray PdfArray to be converted * @return long[] containing the PdfArray values * @deprecated Will be removed in 7.2. Use {@link PdfArray#toLongArray()} instead */ @Deprecated public static long[] asLongArray(PdfArray pdfArray) { long[] rslt = new long[pdfArray.size()]; for (int k = 0; k < rslt.length; ++k) { rslt[k] = pdfArray.getAsNumber(k).longValue(); } return rslt; }
public CalRgb(float[] whitePoint, float[] blackPoint, float[] gamma, float[] matrix) { this(whitePoint); PdfDictionary d = ((PdfArray)getPdfObject()).getAsDictionary(1); if (blackPoint != null) d.put(PdfName.BlackPoint, new PdfArray(blackPoint)); if (gamma != null) d.put(PdfName.Gamma, new PdfArray(gamma)); if (matrix != null) d.put(PdfName.Matrix, new PdfArray(matrix)); }
/** * PDF 2.0. Gets halftone origin * * @return an array of two values specifying X and Y values of the halftone origin in the current coordinate system, * respectively, or <code>null</code> if halftone origin is not specified */ public float[] getHalftoneOrigin() { PdfArray hto = getPdfObject().getAsArray(PdfName.HTO); if (hto != null && hto.size() == 2 && hto.get(0).isNumber() && hto.get(1).isNumber()) { return new float[]{hto.getAsNumber(0).floatValue(), hto.getAsNumber(1).floatValue()}; } else { return null; } }
newField.getPdfObject().remove(PdfName.P); formTo.getFields().remove(existingField.getPdfObject()); if (kids != null && !kids.isEmpty()) { existingField.addKid(newField); return existingField; mergedField.put(PdfName.Parent, parent); PdfArray parentKids = parent.getAsArray(PdfName.Kids); for (int i = 0; i < parentKids.size(); i++) { PdfObject obj = parentKids.get(i); if (obj == existingField.getPdfObject()) { parentKids.set(i, mergedField.getPdfObject()); break;
protected Rectangle getRect(PdfDictionary field) { PdfArray rect = field.getAsArray(PdfName.Rect); if (rect == null) { PdfArray kids = field.getAsArray(PdfName.Kids); if (kids == null) { throw new PdfException(PdfException.WrongFormFieldAddAnnotationToTheField); } rect = ((PdfDictionary) kids.get(0)).getAsArray(PdfName.Rect); } return rect != null ? rect.toRectangle() : null; }
private PdfDictionary getPdfAOutputIntent(PdfArray outputIntents) { for (int i = 0; i < outputIntents.size(); ++i) { PdfName outputIntentSubtype = outputIntents.getAsDictionary(i).getAsName(PdfName.S); if (PdfName.GTS_PDFA1.equals(outputIntentSubtype)) { return outputIntents.getAsDictionary(i); } } return null; }
private boolean isAltCSIsTheSame(PdfObject cs1, PdfObject cs2) { boolean altCSIsTheSame = false; if (cs1 instanceof PdfName) { altCSIsTheSame = cs1.equals(cs2); } else if (cs1 instanceof PdfArray && cs2 instanceof PdfArray) { // TODO(DEVSIX-1672) in fact need to check if objects content is equal. ISO 19005-2, 6.2.4.4 "Separation and DeviceN colour spaces": // In evaluating equivalence, the PDF objects shall be compared, rather than the computational // result of the use of those PDF objects. Compression and whether or not an object is direct or indirect shall be ignored. altCSIsTheSame = ((PdfArray) cs1).get(0).equals(((PdfArray) cs1).get(0)); } return altCSIsTheSame; }
/** * Sets the {@code float[]} of numbers specifying how to map vertex coordinates and colour components * into the appropriate ranges of values. The ranges shall be specified as follows: * [x_min x_max y_min y_max c1_min c1_max … cn_min cn_max]. * Only one pair of color values shall be specified if a Function entry is present. * * @param decode the {@code float[]} of Decode object to set. */ public void setDecode(float[] decode) { setDecode(new PdfArray(decode)); }
/** * Gets a list of X509CRL objects from a Document Security Store. * @return a list of CRLs * @throws GeneralSecurityException * @throws IOException */ public List<X509CRL> getCRLsFromDSS() throws GeneralSecurityException, IOException { List<X509CRL> crls = new ArrayList<>(); if (dss == null) return crls; PdfArray crlarray = dss.getAsArray(PdfName.CRLs); if (crlarray == null) return crls; for (int i = 0; i < crlarray.size(); i++) { PdfStream stream = crlarray.getAsStream(i); crls.add((X509CRL) SignUtils.parseCrlFromStream(new ByteArrayInputStream(stream.getBytes()))); } return crls; }
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; }
/** * Create a new PdfArray. The PdfObjects in the iterable object will be added to the PdfArray. * * @param objects List of PdfObjects to be added to this PdfArray * @param initialCapacity Initial capacity of this PdfArray */ public PdfArray(Iterable<? extends PdfObject> objects, int initialCapacity) { list = new ArrayList<>(initialCapacity); for (PdfObject element : objects) add(element); }
/** * Returns the value associated to this key as a Rectangle. If the value isn't a PdfArray of which the * firt four elements are PdfNumbers, null is returned. * * @param key the key of which the associated value needs to be returned * @return PdfArray associated with this key * @see PdfArray#toRectangle() */ public Rectangle getAsRectangle(PdfName key) { PdfArray a = getAsArray(key); return a == null ? null : a.toRectangle(); }
/** * Adds I{@code PdfFormXObject} to the specified position with specified width preserving aspect ratio. * * @param form * @param x * @param y * @param width * @return current canvas. */ private PdfCanvas addForm(PdfFormXObject form, float x, float y, float width) { PdfArray bbox = form.getPdfObject().getAsArray(PdfName.BBox); if (bbox == null) throw new PdfException(PdfException.PdfFormXobjectHasInvalidBbox); float formWidth = Math.abs(bbox.getAsNumber(2).floatValue() - bbox.getAsNumber(0).floatValue()); float formHeight = Math.abs(bbox.getAsNumber(3).floatValue() - bbox.getAsNumber(1).floatValue()); return addForm(form, width, 0, 0, width / formWidth * formHeight, x, y); }
/** * This method removes all widget annotation entries from the form field the given annotation merged with. */ public void releaseFormFieldFromWidgetAnnotation(){ PdfDictionary annotDict = getPdfObject(); for (PdfName entry: widgetEntries) { annotDict.remove(entry); } PdfDictionary parent = annotDict.getAsDictionary(PdfName.Parent); if (parent != null && annotDict.size() == 1) { PdfArray kids = parent.getAsArray(PdfName.Kids); kids.remove(annotDict); if (kids.size() == 0) { parent.remove(PdfName.Kids); } } } /**
protected static byte[] computeGlobalKeyOnReading(PdfDictionary encryptionDictionary, PrivateKey certificateKey, Certificate certificate, String certificateKeyProvider, IExternalDecryptionProcess externalDecryptionProcess, boolean encryptMetadata, String digestAlgorithm) { PdfArray recipients = encryptionDictionary.getAsArray(PdfName.Recipients); if (recipients == null) { recipients = encryptionDictionary.getAsDictionary(PdfName.CF) .getAsDictionary(PdfName.DefaultCryptFilter) .getAsArray(PdfName.Recipients); } byte[] envelopedData = EncryptionUtils.fetchEnvelopedData(certificateKey, certificate, certificateKeyProvider, externalDecryptionProcess, recipients); byte[] encryptionKey; MessageDigest md; try { md = MessageDigest.getInstance(digestAlgorithm); md.update(envelopedData, 0, 20); for (int i = 0; i < recipients.size(); i++) { byte[] encodedRecipient = recipients.getAsString(i).getValueBytes(); md.update(encodedRecipient); } if (!encryptMetadata) { md.update(new byte[]{(byte) 255, (byte) 255, (byte) 255, (byte) 255}); } encryptionKey = md.digest(); } catch (Exception f) { throw new PdfException(PdfException.PdfDecryption, f); } return encryptionKey; }
/** * Constructs a new PdfVisibilityExpression instance by its raw PdfArray. * @param visibilityExpressionArray the array representing the visibility expression */ public PdfVisibilityExpression(PdfArray visibilityExpressionArray) { super(visibilityExpressionArray); PdfName operator = visibilityExpressionArray.getAsName(0); if (visibilityExpressionArray.size() < 1 || !PdfName.Or.equals(operator) && !PdfName.And.equals(operator) && !PdfName.Not.equals(operator)) { throw new IllegalArgumentException("Invalid visibilityExpressionArray"); } }