public static PdfFunction type2(PdfWriter writer, float domain[], float range[], float c0[], float c1[], float n) { PdfFunction func = new PdfFunction(writer); func.dictionary = new PdfDictionary(); func.dictionary.put(PdfName.FUNCTIONTYPE, new PdfNumber(2)); func.dictionary.put(PdfName.DOMAIN, new PdfArray(domain)); if (range != null) func.dictionary.put(PdfName.RANGE, new PdfArray(range)); if (c0 != null) func.dictionary.put(PdfName.C0, new PdfArray(c0)); if (c1 != null) func.dictionary.put(PdfName.C1, new PdfArray(c1)); func.dictionary.put(PdfName.N, new PdfNumber(n)); return func; }
protected void readFields() { catalog = trailer.getAsDict(PdfName.ROOT); PdfDictionary fdf = catalog.getAsDict(PdfName.FDF); if (fdf == null) return; PdfString fs = fdf.getAsString(PdfName.F); if (fs != null) fileSpec = fs.toUnicodeString(); PdfArray fld = fdf.getAsArray(PdfName.FIELDS); if (fld == null) return; encoding = fdf.getAsName(PdfName.ENCODING); PdfDictionary merged = new PdfDictionary(); merged.put(PdfName.KIDS, fld); kidNode(merged, ""); }
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(); }
PdfDictionary pageDict = myReader.getPageN(1); PdfArray annots = pageDict.getAsArray(PdfName.ANNOTS); ArrayList<String> dests = new ArrayList<String>(); if (annots != null) { for (int i = 0; i < annots.size(); ++i) { PdfDictionary annotDict = annots.getAsDict(i); PdfName subType = annotDict.getAsName(PdfName.SUBTYPE); if (subType != null && PdfName.LINK.equals(subType)) { PdfDictionary action = annotDict.getAsDict(PdfName.A); if (action != null && PdfName.URI.equals(action.getAsName(PdfName.S)) { dests.add(action.getAsString(PdfName.URI).toString()); } // else { its an internal link, meh } } } }
protected static PdfDictionary duplicatePdfDictionary(final PdfDictionary original, PdfDictionary copy, final PdfReader newReader) { if (copy == null) copy = new PdfDictionary(original.size()); for (Object element : original.getKeys()) { PdfName key = (PdfName)element; copy.put(key, duplicatePdfObject(original.get(key), newReader)); } return copy; }
for (int page = 1; page <= reader.getNumberOfPages(); ++page) { PdfDictionary pageDic = reader.getPageN(page); PdfArray annots = pageDic.getAsArray(PdfName.ANNOTS); PdfDictionary annDic = (PdfDictionary) annoto; if (flattenFreeTextAnnotations) { if (!(annDic.get(PdfName.SUBTYPE)).equals(PdfName.FREETEXT)) { continue; if ((annDic.get(PdfName.SUBTYPE)).equals(PdfName.WIDGET)) { PdfNumber ff = annDic.getAsNumber(PdfName.F); int flags = ff != null ? ff.intValue() : 0; PdfObject obj1 = annDic.get(PdfName.AP); if (obj1 == null) continue; PdfDictionary appDic = obj1 instanceof PdfIndirectReference ? (PdfDictionary) PdfReader.getPdfObject(obj1) : (PdfDictionary) obj1; PdfObject obj = appDic.get(PdfName.N); PdfDictionary objDict = appDic.getAsStream(PdfName.N); if (obj instanceof PdfIndirectReference && !obj.isIndirect()) { app = new PdfAppearance((PdfIndirectReference) obj); } else if (objReal instanceof PdfStream) { ((PdfDictionary) objReal).put(PdfName.SUBTYPE, PdfName.FORM); app = new PdfAppearance((PdfIndirectReference) obj); } else { if (objReal.isDictionary()) {
private PdfObject propagate(PdfObject obj) throws IOException { if (obj == null) { return new PdfNull(); } else if (obj.isArray()) { PdfArray a = (PdfArray)obj; for (int i = 0; i < a.size(); i++) { a.set(i, propagate(a.getPdfObject(i))); } return a; } else if (obj.isDictionary() || obj.isStream()) { PdfDictionary d = (PdfDictionary)obj; for (PdfName key : d.getKeys()) { d.put(key, propagate(d.get(key))); } return d; } else if (obj.isIndirect()) { obj = PdfReader.getPdfObject(obj); return addToBody(propagate(obj)).getIndirectReference(); } else return obj; }
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; }
private void updateCalculationOrder(PdfReader reader) { PdfDictionary catalog = reader.getCatalog(); PdfDictionary acro = catalog.getAsDict(PdfName.ACROFORM); if (acro == null) return; PdfArray co = acro.getAsArray(PdfName.CO); if (co == null || co.size() == 0) return; AcroFields af = reader.getAcroFields(); for (int k = 0; k < co.size(); ++k) { PdfObject obj = co.getPdfObject(k); if (obj == null || !obj.isIndirect()) continue; String name = getCOName(reader, (PRIndirectReference) obj); if (af.getFieldItem(name) == null) continue; name = "." + name; if (calculationOrder.contains(name)) continue; calculationOrder.add(name); } }
/** * 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 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)); }
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); }
protected void updateNamedDestinations() throws IOException { PdfDictionary dic = reader.getCatalog().getAsDict(PdfName.NAMES); if (dic != null) dic = dic.getAsDict(PdfName.DESTS); if (dic == null) { dic = reader.getCatalog().getAsDict(PdfName.DESTS); } if (dic == null) { dic = new PdfDictionary(); PdfDictionary dests = new PdfDictionary(); dic.put(PdfName.NAMES, new PdfArray()); dests.put(PdfName.DESTS, dic); reader.getCatalog().put(PdfName.NAMES, dests); } PdfArray names = getLastChildInNameTree(dic); for (Object name : namedDestinations.keySet()) { names.add(new PdfString(name.toString())); names.add(addToBody(namedDestinations.get(name), getPdfIndirectReference()).getIndirectReference()); } }
protected void readColorProfile() { PdfObject outputIntents = reader.getCatalog().getAsArray(PdfName.OUTPUTINTENTS); if (outputIntents != null && ((PdfArray) outputIntents).size() > 0) { PdfStream iccProfileStream = null; for (int i = 0; i < ((PdfArray) outputIntents).size(); i++) { PdfDictionary outputIntentDictionary = ((PdfArray) outputIntents).getAsDict(i); if (outputIntentDictionary != null) { PdfName gts = outputIntentDictionary.getAsName(PdfName.S); if (iccProfileStream == null || PdfName.GTS_PDFA1.equals(gts)) { iccProfileStream = outputIntentDictionary.getAsStream(PdfName.DESTOUTPUTPROFILE); if (iccProfileStream != null && PdfName.GTS_PDFA1.equals(gts)) break; } } } if (iccProfileStream instanceof PRStream) { try { colorProfile = ICC_Profile.getInstance(PdfReader.getStreamBytes((PRStream)iccProfileStream)); } catch(IOException exc) { throw new ExceptionConverter(exc); } } } }
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); }
protected void readColorProfile() { PdfObject outputIntents = reader.getCatalog().getAsArray(PdfName.OUTPUTINTENTS); if (outputIntents != null && ((PdfArray) outputIntents).size() > 0) { PdfStream iccProfileStream = null; for (int i = 0; i < ((PdfArray) outputIntents).size(); i++) { PdfDictionary outputIntentDictionary = ((PdfArray) outputIntents).getAsDict(i); if (outputIntentDictionary != null) { iccProfileStream = outputIntentDictionary.getAsStream(PdfName.DESTOUTPUTPROFILE); if (iccProfileStream != null) break; } } if (iccProfileStream instanceof PRStream) { try { colorProfile = ICC_Profile.getInstance(PdfReader.getStreamBytes((PRStream) iccProfileStream)); } catch (IOException exc) { throw new ExceptionConverter(exc); } } } }
/** * Replaces CalRGB and CalGray colorspaces with DeviceRGB and DeviceGray. */ public void simplifyColorspace() { if (additional == null) return; PdfArray value = additional.getAsArray(PdfName.COLORSPACE); if (value == null) return; PdfObject cs = simplifyColorspace(value); PdfObject newValue; if (cs.isName()) newValue = cs; else { newValue = value; PdfName first = value.getAsName(0); if (PdfName.INDEXED.equals(first)) { if (value.size() >= 2) { PdfArray second = value.getAsArray(1); if (second != null) { value.set(1, simplifyColorspace(second)); } } } } additional.put(PdfName.COLORSPACE, newValue); }
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; }