/** * Adds keys to the signature dictionary that define * the field permissions. * This method is only used for signatures that lock fields. * @param crypto the signature dictionary */ private void addFieldMDP(PdfDictionary crypto, PdfDictionary fieldLock) { PdfDictionary reference = new PdfDictionary(); PdfDictionary transformParams = new PdfDictionary(); transformParams.putAll(fieldLock); transformParams.put(PdfName.TYPE, PdfName.TRANSFORMPARAMS); transformParams.put(PdfName.V, new PdfName("1.2")); reference.put(PdfName.TRANSFORMMETHOD, PdfName.FIELDMDP); reference.put(PdfName.TYPE, PdfName.SIGREF); reference.put(PdfName.TRANSFORMPARAMS, transformParams); reference.put(new PdfName("DigestValue"), new PdfString("aa")); PdfArray loc = new PdfArray(); loc.add(new PdfNumber(0)); loc.add(new PdfNumber(0)); reference.put(new PdfName("DigestLocation"), loc); reference.put(new PdfName("DigestMethod"), new PdfName("MD5")); reference.put(PdfName.DATA, writer.reader.getTrailer().get(PdfName.ROOT)); PdfArray types = crypto.getAsArray(PdfName.REFERENCE); if (types == null) types = new PdfArray(); types.add(reference); crypto.put(PdfName.REFERENCE, types); }
/** * Returns the content of the document information dictionary as a <CODE>HashMap</CODE> * of <CODE>String</CODE>. * @return content of the document information dictionary */ public HashMap<String, String> getInfo() { HashMap<String, String> map = new HashMap<String, String>(); PdfDictionary info = trailer.getAsDict(PdfName.INFO); if (info == null) return map; for (Object element : info.getKeys()) { PdfName key = (PdfName)element; PdfObject obj = getPdfObject(info.get(key)); if (obj == null) continue; String value = obj.toString(); switch (obj.type()) { case PdfObject.STRING: { value = ((PdfString)obj).toUnicodeString(); break; } case PdfObject.NAME: { value = PdfName.decodeName(value); break; } } map.put(PdfName.decodeName(key.toString()), value); } return map; }
public void setMKIconFit(PdfName scale, PdfName scalingType, float leftoverLeft, float leftoverBottom, boolean fitInBounds) { PdfDictionary dic = new PdfDictionary(); if (!scale.equals(PdfName.A)) dic.put(PdfName.SW, scale); if (!scalingType.equals(PdfName.P)) dic.put(PdfName.S, scalingType); if (leftoverLeft != 0.5f || leftoverBottom != 0.5f) { PdfArray array = new PdfArray(new PdfNumber(leftoverLeft)); array.add(new PdfNumber(leftoverBottom)); dic.put(PdfName.A, array); } if (fitInBounds) dic.put(PdfName.FB, PdfBoolean.PDFTRUE); getMK().put(PdfName.IF, dic); }
private static void addFont(PRIndirectReference fontRef, IntHashtable hits, ArrayList<Object[]> fonts) { PdfObject obj = PdfReader.getPdfObject(fontRef); if (obj == null || !obj.isDictionary()) return; PdfDictionary font = (PdfDictionary)obj; PdfName subtype = font.getAsName(PdfName.SUBTYPE); if (!PdfName.TYPE1.equals(subtype) && !PdfName.TRUETYPE.equals(subtype) && !PdfName.TYPE0.equals(subtype)) return; PdfName name = font.getAsName(PdfName.BASEFONT); fonts.add(new Object[]{PdfName.decodeName(name.toString()), fontRef}); hits.put(fontRef.getNumber(), 1); }
/** @see com.itextpdf.text.pdf.interfaces.PdfDocumentActions#setAdditionalAction(com.itextpdf.text.pdf.PdfName, com.itextpdf.text.pdf.PdfAction) */ public void setAdditionalAction(final PdfName actionType, final PdfAction action) throws DocumentException { if (!(actionType.equals(DOCUMENT_CLOSE) || actionType.equals(WILL_SAVE) || actionType.equals(DID_SAVE) || actionType.equals(WILL_PRINT) || actionType.equals(DID_PRINT))) { throw new DocumentException(MessageLocalization.getComposedMessage("invalid.additional.action.type.1", actionType.toString())); } pdf.addAdditionalAction(actionType, action); }
private boolean compareNamesExtended(PdfName outName, PdfName cmpName, ObjectPath currentPath, CompareResult compareResult) { if (cmpName.compareTo(outName) == 0) { return true; } else { if (compareResult != null && currentPath != null) compareResult.addError(currentPath, String.format("PdfName. Expected: %s. Found: %s", cmpName.toString(), outName.toString())); return false; } }
/** * Used by the creating application to store application-specific * data associated with this optional content group. * @param creator a text string specifying the application that created the group * @param subtype a string defining the type of content controlled by the group. Suggested * values include but are not limited to <B>Artwork</B>, for graphic-design or publishing * applications, and <B>Technical</B>, for technical designs such as building plans or * schematics */ public void setCreatorInfo(String creator, String subtype) { PdfDictionary usage = getUsage(); PdfDictionary dic = new PdfDictionary(); dic.put(PdfName.CREATOR, new PdfString(creator, PdfObject.TEXT_UNICODE)); dic.put(PdfName.SUBTYPE, new PdfName(subtype)); usage.put(PdfName.CREATORINFO, dic); }
/** * @see com.itextpdf.text.pdf.interfaces.PdfVersion#addDeveloperExtension(com.itextpdf.text.pdf.PdfDeveloperExtension) * @since 2.1.6 */ public void addDeveloperExtension(PdfDeveloperExtension de) { if (extensions == null) { extensions = new PdfDictionary(); } else { PdfDictionary extension = extensions.getAsDict(de.getPrefix()); if (extension != null) { int diff = de.getBaseversion().compareTo(extension.getAsName(PdfName.BASEVERSION)); if (diff < 0) return; diff = de.getExtensionLevel() - extension.getAsNumber(PdfName.EXTENSIONLEVEL).intValue(); if (diff <= 0) return; } } extensions.put(de.getPrefix(), de.getDeveloperExtensions()); }
/** * One of more users for whom this optional content group is primarily intended. * @param type should be "Ind" (Individual), "Ttl" (Title), or "Org" (Organization). * @param names one or more names * @since 5.0.2 */ public void setUser(String type, String... names) { PdfDictionary usage = getUsage(); PdfDictionary dic = new PdfDictionary(); dic.put(PdfName.TYPE, new PdfName(type)); PdfArray arr = new PdfArray(); for (String s : names) arr.add(new PdfString(s, PdfObject.TEXT_UNICODE)); usage.put(PdfName.NAME, arr); usage.put(PdfName.USER, dic); }
/** * Removes all the fields from the document. */ public void removeFields() { pageRefs.resetReleasePage(); for (int k = 1; k <= pageRefs.size(); ++k) { PdfDictionary page = pageRefs.getPageN(k); PdfArray annots = page.getAsArray(PdfName.ANNOTS); if (annots == null) { pageRefs.releasePage(k); continue; } for (int j = 0; j < annots.size(); ++j) { PdfObject obj = getPdfObjectRelease(annots.getPdfObject(j)); if (obj == null || !obj.isDictionary()) continue; PdfDictionary annot = (PdfDictionary)obj; if (PdfName.WIDGET.equals(annot.get(PdfName.SUBTYPE))) annots.remove(j--); } if (annots.isEmpty()) page.remove(PdfName.ANNOTS); else pageRefs.releasePage(k); } catalog.remove(PdfName.ACROFORM); pageRefs.resetReleasePage(); }
private void processType0(PdfDictionary font) { try { PdfObject toUniObject = PdfReader.getPdfObjectRelease(font.get(PdfName.TOUNICODE)); PdfArray df = (PdfArray)PdfReader.getPdfObjectRelease(font.get(PdfName.DESCENDANTFONTS)); PdfDictionary cidft = (PdfDictionary)PdfReader.getPdfObjectRelease(df.getPdfObject(0)); PdfNumber dwo = (PdfNumber)PdfReader.getPdfObjectRelease(cidft.get(PdfName.DW)); int dw = 1000; if (dwo != null) dw = dwo.intValue(); IntHashtable widths = readWidths((PdfArray)PdfReader.getPdfObjectRelease(cidft.get(PdfName.W))); PdfDictionary fontDesc = (PdfDictionary)PdfReader.getPdfObjectRelease(cidft.get(PdfName.FONTDESCRIPTOR)); fillFontDesc(fontDesc); if (toUniObject instanceof PRStream){ fillMetrics(PdfReader.getStreamBytes((PRStream)toUniObject), widths, dw); } else if (new PdfName("Identity-H").equals(toUniObject)) { fillMetricsIdentity(widths, dw); } } catch (Exception e) { throw new ExceptionConverter(e); } }
private PdfName getSignatureHashKey(String signatureName) throws NoSuchAlgorithmException, IOException { PdfDictionary dic = acroFields.getSignatureDictionary(signatureName); PdfString contents = dic.getAsString(PdfName.CONTENTS); byte[] bc = null; if(!reader.isEncrypted()) { bc = contents.getOriginalBytes(); }else{ bc = contents.getBytes(); } byte[] bt = null; if (PdfName.ETSI_RFC3161.equals(PdfReader.getPdfObject(dic.get(PdfName.SUBFILTER)))) { ASN1InputStream din = new ASN1InputStream(new ByteArrayInputStream(bc)); ASN1Primitive pkcs = din.readObject(); bc = pkcs.getEncoded(); } bt = hashBytesSha1(bc); return new PdfName(Utilities.convertToHex(bt)); }
private void fillDiffMap(PdfDictionary encDic, CMapToUnicode toUnicode) { PdfArray diffs = encDic.getAsArray(PdfName.DIFFERENCES); if (diffs != null) { diffmap = new IntHashtable(); int currentNumber = 0; for (int k = 0; k < diffs.size(); ++k) { PdfObject obj = diffs.getPdfObject(k); if (obj.isNumber()) currentNumber = ((PdfNumber)obj).intValue(); else { int c[] = GlyphList.nameToUnicode(PdfName.decodeName(((PdfName)obj).toString())); if (c != null && c.length > 0) { uni2byte.put(c[0], currentNumber);
public void transformDestination(float a, float b, float c, float d, float e, float f) { if (!isInternal()) throw new IllegalArgumentException(MessageLocalization.getComposedMessage("cannot.change.destination.of.external.link")); if (destination.getAsName(1).equals(PdfName.XYZ)) { float x = destination.getAsNumber(2).floatValue(); float y = destination.getAsNumber(3).floatValue(); float xx = x * a + y * c + e; float yy = x * b + y * d + f; destination.set(2, new PdfNumber(xx)); destination.set(3, new PdfNumber(yy)); } }
public void setAppearance(PdfName ap, String state, PdfTemplate template) { PdfDictionary dicAp = (PdfDictionary)get(PdfName.AP); if (dicAp == null) dicAp = new PdfDictionary(); PdfDictionary dic; PdfObject obj = dicAp.get(ap); if (obj != null && obj.isDictionary()) dic = (PdfDictionary)obj; else dic = new PdfDictionary(); dic.put(new PdfName(state), template.getIndirectReference()); dicAp.put(ap, dic); put(PdfName.AP, dicAp); if (!form) return; if (templates == null) templates = new HashSet<PdfTemplate>(); templates.add(template); }
private PdfDictionary getFontBaseType(PdfIndirectReference CIDFont) { PdfDictionary dic = new PdfDictionary(PdfName.FONT); dic.put(PdfName.SUBTYPE, PdfName.TYPE0); String name = fontName; if (style.length() > 0) name += "-" + style.substring(1); name += "-" + CMap; dic.put(PdfName.BASEFONT, new PdfName(name)); dic.put(PdfName.ENCODING, new PdfName(CMap)); dic.put(PdfName.DESCENDANTFONTS, new PdfArray(CIDFont)); return dic; }
protected PdfDictionary readDictionary() throws IOException { PdfDictionary dic = new PdfDictionary(); while (true) { tokens.nextValidToken(); if (tokens.getTokenType() == TokenType.END_DIC) break; if (tokens.getTokenType() != TokenType.NAME) tokens.throwError(MessageLocalization.getComposedMessage("dictionary.key.1.is.not.a.name", tokens.getStringValue())); PdfName name = new PdfName(tokens.getStringValue(), false); PdfObject obj = readPRObject(); int type = obj.type(); if (-type == TokenType.END_DIC.ordinal()) tokens.throwError(MessageLocalization.getComposedMessage("unexpected.gt.gt")); if (-type == TokenType.END_ARRAY.ordinal()) tokens.throwError(MessageLocalization.getComposedMessage("unexpected.close.bracket")); dic.put(name, obj); } return dic; }
/** * Retrieves links for a certain page. * @param page the page to inspect * @return a list of links */ public ArrayList<PdfAnnotation.PdfImportedLink> getLinks(final int page) { pageRefs.resetReleasePage(); ArrayList<PdfAnnotation.PdfImportedLink> result = new ArrayList<PdfAnnotation.PdfImportedLink>(); PdfDictionary pageDic = pageRefs.getPageN(page); if (pageDic.get(PdfName.ANNOTS) != null) { PdfArray annots = pageDic.getAsArray(PdfName.ANNOTS); for (int j = 0; j < annots.size(); ++j) { PdfDictionary annot = (PdfDictionary)getPdfObjectRelease(annots.getPdfObject(j)); if (PdfName.LINK.equals(annot.get(PdfName.SUBTYPE))) { result.add(new PdfAnnotation.PdfImportedLink(annot)); } } } pageRefs.releasePage(page); pageRefs.resetReleasePage(); return result; }
static Integer getFlags(PdfDictionary field) { PdfName type = field.getAsName(PdfName.FT); if (!PdfName.BTN.equals(type)) return null; PdfNumber flags = field.getAsNumber(PdfName.FF); if (flags == null) return null; return flags.intValue(); }
private PdfName getSignatureHashKey(String signatureName) throws NoSuchAlgorithmException, IOException { PdfDictionary dic = acroFields.getSignatureDictionary(signatureName); PdfString contents = dic.getAsString(PdfName.CONTENTS); byte[] bc = contents.getOriginalBytes(); byte[] bt = null; if (PdfName.ETSI_RFC3161.equals(PdfReader.getPdfObject(dic.get(PdfName.SUBFILTER)))) { ASN1InputStream din = new ASN1InputStream(new ByteArrayInputStream(bc)); ASN1Primitive pkcs = din.readObject(); bc = pkcs.getEncoded(); } bt = hashBytesSha1(bc); return new PdfName(Utilities.convertToHex(bt)); }