protected virtual void KidNode(PdfDictionary merged, String name) { PdfArray kids = merged.GetAsArray(PdfName.KIDS); if (kids == null || kids.Size == 0) { if (name.Length > 0) { name = name.Substring(1); } fields[name] = merged; } else { merged.Remove(PdfName.KIDS); for (int k = 0; k < kids.Size; ++k) { PdfDictionary dic = new PdfDictionary(); dic.Merge(merged); PdfDictionary newDic = kids.GetAsDict(k); PdfString t = newDic.GetAsString(PdfName.T); String newName = name; if (t != null) { newName += "." + t.ToUnicodeString(); } dic.Merge(newDic); dic.Remove(PdfName.T); KidNode(dic, newName); } } }
internal void SetOriginalResources(PdfDictionary resources, int[] newNamePtr) { if (newNamePtr != null) { namePtr = newNamePtr; } forbiddenNames = new Hashtable(); usedNames = new Hashtable(); if (resources == null) { return; } originalResources = new PdfDictionary(); originalResources.Merge(resources); foreach (PdfName key in resources.Keys) { PdfObject sub = PdfReader.GetPdfObject(resources.Get(key)); if (sub != null && sub.IsDictionary()) { PdfDictionary dic = (PdfDictionary)sub; foreach (PdfName name in dic.Keys) { forbiddenNames[name] = null; } PdfDictionary dic2 = new PdfDictionary(); dic2.Merge(dic); originalResources.Put(key, dic2); } } }
/** * Gets the content stream of a page as a PdfStream object. * @param pageNumber the page of which you want the stream * @param compressionLevel the compression level you want to apply to the stream * @return a PdfStream object * @since 2.1.3 (the method already existed without param compressionLevel) */ internal PdfStream GetFormXObject(int pageNumber, int compressionLevel) { PdfDictionary page = reader.GetPageNRelease(pageNumber); PdfObject contents = PdfReader.GetPdfObjectRelease(page.Get(PdfName.CONTENTS)); PdfDictionary dic = new PdfDictionary(); byte[] bout = null; if (contents != null) { if (contents.IsStream()) { dic.Merge((PRStream)contents); } else { bout = reader.GetPageContent(pageNumber, file); } } else { bout = new byte[0]; } dic.Put(PdfName.RESOURCES, PdfReader.GetPdfObjectRelease(page.Get(PdfName.RESOURCES))); dic.Put(PdfName.TYPE, PdfName.XOBJECT); dic.Put(PdfName.SUBTYPE, PdfName.FORM); PdfImportedPage impPage = (PdfImportedPage)importedPages[pageNumber]; dic.Put(PdfName.BBOX, new PdfRectangle(impPage.BoundingBox)); PdfArray matrix = impPage.Matrix; if (matrix == null) { dic.Put(PdfName.MATRIX, IDENTITYMATRIX); } else { dic.Put(PdfName.MATRIX, matrix); } dic.Put(PdfName.FORMTYPE, ONE); PRStream stream; if (bout == null) { stream = new PRStream((PRStream)contents, dic); } else { stream = new PRStream(reader, bout); stream.Merge(dic); } return(stream); }
// methods internal void Add(PdfName key, PdfDictionary resource) { if (resource.Size == 0) { return; } PdfDictionary dic = GetAsDict(key); if (dic == null) { Put(key, resource); } else { dic.Merge(resource); } }
/** * merge field attributes from two dictionaries * @param parent one dictionary * @param child the other dictionary * @return a merged dictionary */ protected PdfDictionary MergeAttrib(PdfDictionary parent, PdfDictionary child) { PdfDictionary targ = new PdfDictionary(); if (parent != null) { targ.Merge(parent); } foreach (PdfName key in child.Keys) { if (key.Equals(PdfName.DR) || key.Equals(PdfName.DA) || key.Equals(PdfName.Q) || key.Equals(PdfName.FF) || key.Equals(PdfName.DV) || key.Equals(PdfName.V) || key.Equals(PdfName.FT) || key.Equals(PdfName.F)) { targ.Put(key, child.Get(key)); } } return(targ); }
internal void Fill() { fields = new Hashtable(); PdfDictionary top = (PdfDictionary)PdfReader.GetPdfObjectRelease(reader.Catalog.Get(PdfName.ACROFORM)); if (top == null) return; PdfArray arrfds = (PdfArray)PdfReader.GetPdfObjectRelease(top.Get(PdfName.FIELDS)); if (arrfds == null || arrfds.Size == 0) return; for (int k = 1; k <= reader.NumberOfPages; ++k) { PdfDictionary page = reader.GetPageNRelease(k); PdfArray annots = (PdfArray)PdfReader.GetPdfObjectRelease(page.Get(PdfName.ANNOTS), page); if (annots == null) continue; for (int j = 0; j < annots.Size; ++j) { PdfDictionary annot = annots.GetAsDict(j); if (annot == null) { PdfReader.ReleaseLastXrefPartial(annots.GetAsIndirectObject(j)); continue; } if (!PdfName.WIDGET.Equals(annot.GetAsName(PdfName.SUBTYPE))) { PdfReader.ReleaseLastXrefPartial(annots.GetAsIndirectObject(j)); continue; } PdfDictionary widget = annot; PdfDictionary dic = new PdfDictionary(); dic.Merge(annot); String name = ""; PdfDictionary value = null; PdfObject lastV = null; while (annot != null) { dic.MergeDifferent(annot); PdfString t = annot.GetAsString(PdfName.T); if (t != null) name = t.ToUnicodeString() + "." + name; if (lastV == null && annot.Get(PdfName.V) != null) lastV = PdfReader.GetPdfObjectRelease(annot.Get(PdfName.V)); if (value == null && t != null) { value = annot; if (annot.Get(PdfName.V) == null && lastV != null) value.Put(PdfName.V, lastV); } annot = annot.GetAsDict(PdfName.PARENT); } if (name.Length > 0) name = name.Substring(0, name.Length - 1); Item item = (Item)fields[name]; if (item == null) { item = new Item(); fields[name] = item; } if (value == null) item.AddValue(widget); else item.AddValue(value); item.AddWidget(widget); item.AddWidgetRef(annots.GetAsIndirectObject(j)); // must be a reference if (top != null) dic.MergeDifferent(top); item.AddMerged(dic); item.AddPage(k); item.AddTabOrder(j); } } // some tools produce invisible signatures without an entry in the page annotation array // look for a single level annotation PdfNumber sigFlags = top.GetAsNumber(PdfName.SIGFLAGS); if (sigFlags == null || (sigFlags.IntValue & 1) != 1) return; for (int j = 0; j < arrfds.Size; ++j) { PdfDictionary annot = arrfds.GetAsDict(j); if (annot == null) { PdfReader.ReleaseLastXrefPartial(arrfds.GetAsIndirectObject(j)); continue; } if (!PdfName.WIDGET.Equals(annot.GetAsName(PdfName.SUBTYPE))) { PdfReader.ReleaseLastXrefPartial(arrfds.GetAsIndirectObject(j)); continue; } PdfArray kids = (PdfArray)PdfReader.GetPdfObjectRelease(annot.Get(PdfName.KIDS)); if (kids != null) continue; PdfDictionary dic = new PdfDictionary(); dic.Merge(annot); PdfString t = annot.GetAsString(PdfName.T); if (t == null) continue; String name = t.ToUnicodeString(); if (fields.ContainsKey(name)) continue; Item item = new Item(); fields[name] = item; item.AddValue(dic); item.AddWidget(dic); item.AddWidgetRef(arrfds.GetAsIndirectObject(j)); // must be a reference item.AddMerged(dic); item.AddPage(-1); item.AddTabOrder(-1); } }
/** * Creates a file specification with the file embedded. The file may * come from the file system or from a byte array. * @param writer the <CODE>PdfWriter</CODE> * @param filePath the file path * @param fileDisplay the file information that is presented to the user * @param fileStore the byte array with the file. If it is not <CODE>null</CODE> * it takes precedence over <CODE>filePath</CODE> * @param mimeType the optional mimeType * @param fileParameter the optional extra file parameters such as the creation or modification date * @param compressionLevel the level of compression * @throws IOException on error * @return the file specification * @since 2.1.3 */ public static PdfFileSpecification FileEmbedded(PdfWriter writer, String filePath, String fileDisplay, byte[] fileStore, String mimeType, PdfDictionary fileParameter, int compressionLevel) { PdfFileSpecification fs = new PdfFileSpecification(); fs.writer = writer; fs.Put(PdfName.F, new PdfString(fileDisplay)); PdfEFStream stream; Stream inp = null; PdfIndirectReference refi; PdfIndirectReference refFileLength; try { refFileLength = writer.PdfIndirectReference; if (fileStore == null) { if (File.Exists(filePath)) { inp = new FileStream(filePath, FileMode.Open, FileAccess.Read); } else { if (filePath.StartsWith("file:/") || filePath.StartsWith("http://") || filePath.StartsWith("https://")) { WebRequest w = WebRequest.Create(filePath); inp = w.GetResponse().GetResponseStream(); } else { inp = BaseFont.GetResourceStream(filePath); if (inp == null) throw new IOException(filePath + " not found as file or resource."); } } stream = new PdfEFStream(inp, writer); } else stream = new PdfEFStream(fileStore); stream.Put(PdfName.TYPE, PdfName.EMBEDDEDFILE); stream.FlateCompress(compressionLevel); stream.Put(PdfName.PARAMS, refFileLength); if (mimeType != null) stream.Put(PdfName.SUBTYPE, new PdfName(mimeType)); refi = writer.AddToBody(stream).IndirectReference; if (fileStore == null) { stream.WriteLength(); } PdfDictionary param = new PdfDictionary(); if (fileParameter != null) param.Merge(fileParameter); param.Put(PdfName.SIZE, new PdfNumber(stream.RawLength)); writer.AddToBody(param, refFileLength); } finally { if (inp != null) try{inp.Close();}catch{} } PdfDictionary f = new PdfDictionary(); f.Put(PdfName.F, refi); fs.Put(PdfName.EF, f); return fs; }
protected PdfArray BranchForm(Hashtable level, PdfIndirectReference parent, String fname) { PdfArray arr = new PdfArray(); foreach (DictionaryEntry entry in level) { String name = (String)entry.Key; Object obj = entry.Value; PdfIndirectReference ind = PdfIndirectReference; PdfDictionary dic = new PdfDictionary(); if (parent != null) { dic.Put(PdfName.PARENT, parent); } dic.Put(PdfName.T, new PdfString(name, PdfObject.TEXT_UNICODE)); String fname2 = fname + "." + name; int coidx = calculationOrder.IndexOf(fname2); if (coidx >= 0) { calculationOrderRefs[coidx] = ind; } if (obj is Hashtable) { dic.Put(PdfName.KIDS, BranchForm((Hashtable)obj, ind, fname2)); arr.Add(ind); AddToBody(dic, ind); } else { ArrayList list = (ArrayList)obj; dic.MergeDifferent((PdfDictionary)list[0]); if (list.Count == 3) { dic.MergeDifferent((PdfDictionary)list[2]); int page = (int)list[1]; PdfDictionary pageDic = (PdfDictionary)pageDics[page - 1]; PdfArray annots = pageDic.GetAsArray(PdfName.ANNOTS); if (annots == null) { annots = new PdfArray(); pageDic.Put(PdfName.ANNOTS, annots); } PdfNumber nn = (PdfNumber)dic.Get(iTextTag); dic.Remove(iTextTag); AdjustTabOrder(annots, ind, nn); } else { PdfArray kids = new PdfArray(); for (int k = 1; k < list.Count; k += 2) { int page = (int)list[k]; PdfDictionary pageDic = (PdfDictionary)pageDics[page - 1]; PdfArray annots = pageDic.GetAsArray(PdfName.ANNOTS); if (annots == null) { annots = new PdfArray(); pageDic.Put(PdfName.ANNOTS, annots); } PdfDictionary widget = new PdfDictionary(); widget.Merge((PdfDictionary)list[k + 1]); widget.Put(PdfName.PARENT, ind); PdfNumber nn = (PdfNumber)widget.Get(iTextTag); widget.Remove(iTextTag); PdfIndirectReference wref = AddToBody(widget).IndirectReference; AdjustTabOrder(annots, wref, nn); kids.Add(wref); Propagate(widget, null, false); } dic.Put(PdfName.KIDS, kids); } arr.Add(ind); AddToBody(dic, ind); Propagate(dic, null, false); } } return(arr); }
internal void SetOriginalResources(PdfDictionary resources, int[] newNamePtr) { if (newNamePtr != null) namePtr = newNamePtr; forbiddenNames = new Hashtable(); usedNames = new Hashtable(); if (resources == null) return; originalResources = new PdfDictionary(); originalResources.Merge(resources); foreach (PdfName key in resources.Keys) { PdfObject sub = PdfReader.GetPdfObject(resources.Get(key)); if (sub != null && sub.IsDictionary()) { PdfDictionary dic = (PdfDictionary)sub; foreach (PdfName name in dic.Keys) { forbiddenNames[name] = null; } PdfDictionary dic2 = new PdfDictionary(); dic2.Merge(dic); originalResources.Put(key, dic2); } } }
protected virtual void KidNode(PdfDictionary merged, String name) { PdfArray kids = merged.GetAsArray(PdfName.KIDS); if (kids == null || kids.Size == 0) { if (name.Length > 0) name = name.Substring(1); fields[name] = merged; } else { merged.Remove(PdfName.KIDS); for (int k = 0; k < kids.Size; ++k) { PdfDictionary dic = new PdfDictionary(); dic.Merge(merged); PdfDictionary newDic = kids.GetAsDict(k); PdfString t = newDic.GetAsString(PdfName.T); String newName = name; if (t != null) newName += "." + t.ToUnicodeString(); dic.Merge(newDic); dic.Remove(PdfName.T); KidNode(dic, newName); } } }
protected PdfArray BranchForm(Hashtable level, PdfIndirectReference parent, String fname) { PdfArray arr = new PdfArray(); foreach (DictionaryEntry entry in level) { String name = (String)entry.Key; Object obj = entry.Value; PdfIndirectReference ind = PdfIndirectReference; PdfDictionary dic = new PdfDictionary(); if (parent != null) dic.Put(PdfName.PARENT, parent); dic.Put(PdfName.T, new PdfString(name, PdfObject.TEXT_UNICODE)); String fname2 = fname + "." + name; int coidx = calculationOrder.IndexOf(fname2); if (coidx >= 0) calculationOrderRefs[coidx] = ind; if (obj is Hashtable) { dic.Put(PdfName.KIDS, BranchForm((Hashtable)obj, ind, fname2)); arr.Add(ind); AddToBody(dic, ind); } else { ArrayList list = (ArrayList)obj; dic.MergeDifferent((PdfDictionary)list[0]); if (list.Count == 3) { dic.MergeDifferent((PdfDictionary)list[2]); int page = (int)list[1]; PdfDictionary pageDic = (PdfDictionary)pageDics[page - 1]; PdfArray annots = pageDic.GetAsArray(PdfName.ANNOTS); if (annots == null) { annots = new PdfArray(); pageDic.Put(PdfName.ANNOTS, annots); } PdfNumber nn = (PdfNumber)dic.Get(iTextTag); dic.Remove(iTextTag); AdjustTabOrder(annots, ind, nn); } else { PdfArray kids = new PdfArray(); for (int k = 1; k < list.Count; k += 2) { int page = (int)list[k]; PdfDictionary pageDic = (PdfDictionary)pageDics[page - 1]; PdfArray annots = pageDic.GetAsArray(PdfName.ANNOTS); if (annots == null) { annots = new PdfArray(); pageDic.Put(PdfName.ANNOTS, annots); } PdfDictionary widget = new PdfDictionary(); widget.Merge((PdfDictionary)list[k + 1]); widget.Put(PdfName.PARENT, ind); PdfNumber nn = (PdfNumber)widget.Get(iTextTag); widget.Remove(iTextTag); PdfIndirectReference wref = AddToBody(widget).IndirectReference; AdjustTabOrder(annots, wref, nn); kids.Add(wref); Propagate(widget, null, false); } dic.Put(PdfName.KIDS, kids); } arr.Add(ind); AddToBody(dic, ind); Propagate(dic, null, false); } } return arr; }
internal void AddDefaultColor(PdfDictionary dic) { colorDictionary.Merge(dic); }
/** * @param fdf * @throws IOException */ public void AddComments(FdfReader fdf) { if (readers2intrefs.ContainsKey(fdf)) return; PdfDictionary catalog = fdf.Catalog; catalog = catalog.GetAsDict(PdfName.FDF); if (catalog == null) return; PdfArray annots = catalog.GetAsArray(PdfName.ANNOTS); if (annots == null || annots.Size == 0) return; RegisterReader(fdf, false); IntHashtable hits = new IntHashtable(); Hashtable irt = new Hashtable(); ArrayList an = new ArrayList(); for (int k = 0; k < annots.Size; ++k) { PdfObject obj = annots[k]; PdfDictionary annot = (PdfDictionary)PdfReader.GetPdfObject(obj); PdfNumber page = annot.GetAsNumber(PdfName.PAGE); if (page == null || page.IntValue >= reader.NumberOfPages) continue; FindAllObjects(fdf, obj, hits); an.Add(obj); if (obj.Type == PdfObject.INDIRECT) { PdfObject nm = PdfReader.GetPdfObject(annot.Get(PdfName.NM)); if (nm != null && nm.Type == PdfObject.STRING) irt[nm.ToString()] = obj; } } int[] arhits = hits.GetKeys(); for (int k = 0; k < arhits.Length; ++k) { int n = arhits[k]; PdfObject obj = fdf.GetPdfObject(n); if (obj.Type == PdfObject.DICTIONARY) { PdfObject str = PdfReader.GetPdfObject(((PdfDictionary)obj).Get(PdfName.IRT)); if (str != null && str.Type == PdfObject.STRING) { PdfObject i = (PdfObject)irt[str.ToString()]; if (i != null) { PdfDictionary dic2 = new PdfDictionary(); dic2.Merge((PdfDictionary)obj); dic2.Put(PdfName.IRT, i); obj = dic2; } } } AddToBody(obj, GetNewObjectNumber(fdf, n, 0)); } for (int k = 0; k < an.Count; ++k) { PdfObject obj = (PdfObject)an[k]; PdfDictionary annot = (PdfDictionary)PdfReader.GetPdfObject(obj); PdfNumber page = annot.GetAsNumber(PdfName.PAGE); PdfDictionary dic = reader.GetPageN(page.IntValue + 1); PdfArray annotsp = (PdfArray)PdfReader.GetPdfObject(dic.Get(PdfName.ANNOTS), dic); if (annotsp == null) { annotsp = new PdfArray(); dic.Put(PdfName.ANNOTS, annotsp); MarkUsed(dic); } MarkUsed(annotsp); annotsp.Add(obj); } }
/** * Gets the content stream of a page as a PdfStream object. * @param pageNumber the page of which you want the stream * @param compressionLevel the compression level you want to apply to the stream * @return a PdfStream object * @since 2.1.3 (the method already existed without param compressionLevel) */ internal PdfStream GetFormXObject(int pageNumber, int compressionLevel) { PdfDictionary page = reader.GetPageNRelease(pageNumber); PdfObject contents = PdfReader.GetPdfObjectRelease(page.Get(PdfName.CONTENTS)); PdfDictionary dic = new PdfDictionary(); byte[] bout = null; if (contents != null) { if (contents.IsStream()) dic.Merge((PRStream)contents); else bout = reader.GetPageContent(pageNumber, file); } else bout = new byte[0]; dic.Put(PdfName.RESOURCES, PdfReader.GetPdfObjectRelease(page.Get(PdfName.RESOURCES))); dic.Put(PdfName.TYPE, PdfName.XOBJECT); dic.Put(PdfName.SUBTYPE, PdfName.FORM); PdfImportedPage impPage = (PdfImportedPage)importedPages[pageNumber]; dic.Put(PdfName.BBOX, new PdfRectangle(impPage.BoundingBox)); PdfArray matrix = impPage.Matrix; if (matrix == null) dic.Put(PdfName.MATRIX, IDENTITYMATRIX); else dic.Put(PdfName.MATRIX, matrix); dic.Put(PdfName.FORMTYPE, ONE); PRStream stream; if (bout == null) { stream = new PRStream((PRStream)contents, dic); } else { stream = new PRStream(reader, bout); stream.Merge(dic); } return stream; }
/** * merge field attributes from two dictionaries * @param parent one dictionary * @param child the other dictionary * @return a merged dictionary */ protected PdfDictionary MergeAttrib(PdfDictionary parent, PdfDictionary child) { PdfDictionary targ = new PdfDictionary(); if (parent != null) targ.Merge(parent); foreach (PdfName key in child.Keys) { if (key.Equals(PdfName.DR) || key.Equals(PdfName.DA) || key.Equals(PdfName.Q) || key.Equals(PdfName.FF) || key.Equals(PdfName.DV) || key.Equals(PdfName.V) || key.Equals(PdfName.FT) || key.Equals(PdfName.F)) { targ.Put(key,child.Get(key)); } } return targ; }
/** * Creates a file specification with the file embedded. The file may * come from the file system or from a byte array. * @param writer the <CODE>PdfWriter</CODE> * @param filePath the file path * @param fileDisplay the file information that is presented to the user * @param fileStore the byte array with the file. If it is not <CODE>null</CODE> * it takes precedence over <CODE>filePath</CODE> * @param mimeType the optional mimeType * @param fileParameter the optional extra file parameters such as the creation or modification date * @param compressionLevel the level of compression * @throws IOException on error * @return the file specification * @since 2.1.3 */ public static PdfFileSpecification FileEmbedded(PdfWriter writer, String filePath, String fileDisplay, byte[] fileStore, String mimeType, PdfDictionary fileParameter, int compressionLevel) { PdfFileSpecification fs = new PdfFileSpecification(); fs.writer = writer; fs.Put(PdfName.F, new PdfString(fileDisplay)); PdfEFStream stream; Stream inp = null; PdfIndirectReference refi; PdfIndirectReference refFileLength; try { refFileLength = writer.PdfIndirectReference; if (fileStore == null) { if (File.Exists(filePath)) { inp = new FileStream(filePath, FileMode.Open, FileAccess.Read); } else { if (filePath.StartsWith("file:/") || filePath.StartsWith("http://") || filePath.StartsWith("https://")) { WebRequest w = WebRequest.Create(filePath); inp = w.GetResponse().GetResponseStream(); } else { inp = BaseFont.GetResourceStream(filePath); if (inp == null) { throw new IOException(filePath + " not found as file or resource."); } } } stream = new PdfEFStream(inp, writer); } else { stream = new PdfEFStream(fileStore); } stream.Put(PdfName.TYPE, PdfName.EMBEDDEDFILE); stream.FlateCompress(compressionLevel); stream.Put(PdfName.PARAMS, refFileLength); if (mimeType != null) { stream.Put(PdfName.SUBTYPE, new PdfName(mimeType)); } refi = writer.AddToBody(stream).IndirectReference; if (fileStore == null) { stream.WriteLength(); } PdfDictionary param = new PdfDictionary(); if (fileParameter != null) { param.Merge(fileParameter); } param.Put(PdfName.SIZE, new PdfNumber(stream.RawLength)); writer.AddToBody(param, refFileLength); } finally { if (inp != null) { try{ inp.Close(); }catch {} } } PdfDictionary f = new PdfDictionary(); f.Put(PdfName.F, refi); fs.Put(PdfName.EF, f); return(fs); }