virtual public void TestConstructionForType0WithoutToUnicodeMap() { int pageNum = 2; PdfName fontIdName = new PdfName("TT9"); string testFile = TestResourceUtils.GetResourceAsTempFile(TEST_RESOURCES_PATH, "type0FontWithoutToUnicodeMap.pdf"); RandomAccessFileOrArray f = new RandomAccessFileOrArray(testFile); PdfReader reader = new PdfReader(f, null); try { PdfDictionary fontsDic = reader.GetPageN(pageNum).GetAsDict(PdfName.RESOURCES).GetAsDict(PdfName.FONT); PdfDictionary fontDicDirect = fontsDic.GetAsDict(fontIdName); PRIndirectReference fontDicIndirect = (PRIndirectReference)fontsDic.Get(fontIdName); Assert.AreEqual(PdfName.TYPE0, fontDicDirect.GetAsName(PdfName.SUBTYPE)); Assert.AreEqual("/Identity-H", fontDicDirect.GetAsName(PdfName.ENCODING).ToString()); Assert.IsNull(fontDicDirect.Get(PdfName.TOUNICODE), "This font should not have a ToUnicode map"); new DocumentFont(fontDicIndirect); // this used to throw an NPE } finally { reader.Close(); } }
private static void DeleteOldReferences(PdfArray all, PdfArray toDelete) { if (all == null || toDelete == null) { return; } foreach (PdfObject pi in toDelete) { if (!pi.IsIndirect()) { continue; } PRIndirectReference pir = (PRIndirectReference)pi; for (int k = 0; k < all.Size; ++k) { PdfObject po = all[k]; if (!po.IsIndirect()) { continue; } PRIndirectReference pod = (PRIndirectReference)po; if (pir.Number == pod.Number) { all.Remove(k); --k; } } } }
/** * Gets the font pointed to by the indirect reference. The font may have been cached. * @param ind the indirect reference ponting to the font * @return the font * @since 5.0.6 */ private CMapAwareDocumentFont GetFont(PRIndirectReference ind) { CMapAwareDocumentFont font; cachedFonts.TryGetValue(ind.Number, out font); if (font == null) { font = new CMapAwareDocumentFont(ind); cachedFonts[ind.Number] = font; } return(font); }
virtual public void TestWidths() { PdfReader pdfReader = TestResourceUtils.GetResourceAsPdfReader(TEST_RESOURCES_PATH, "fontwithwidthissue.pdf"); try { PdfDictionary fontsDic = pdfReader.GetPageN(1).GetAsDict(PdfName.RESOURCES).GetAsDict(PdfName.FONT); PRIndirectReference fontDicIndirect = (PRIndirectReference)fontsDic.Get(new PdfName("F1")); CMapAwareDocumentFont f = new CMapAwareDocumentFont(fontDicIndirect); Assert.IsTrue(f.GetWidth('h') != 0, "Width should not be 0"); } finally { pdfReader.Close(); } }
/** * Processes an object. If the object is indirect, it is added to the * list of resources. If not, it is just processed. * @param object the object to process */ protected void Process(PdfObject @object) { PRIndirectReference @ref = @object.IndRef; if (@ref == null) { LoopOver(@object); } else { bool containsKey = resources.ContainsKey(@ref.Number); resources[@ref.Number] = @object; if (!containsKey) { LoopOver(@object); } } }
/** * Gets the content bytes from a content object, which may be a reference * a stream or an array. * @param contentObject the object to read bytes from * @return the content bytes * @throws IOException */ public static byte[] GetContentBytesFromContentObject(PdfObject contentObject) { byte[] result; switch (contentObject.Type) { case PdfObject.INDIRECT: PRIndirectReference refi = (PRIndirectReference)contentObject; PdfObject directObject = PdfReader.GetPdfObject(refi); result = GetContentBytesFromContentObject(directObject); break; case PdfObject.STREAM: PRStream stream = (PRStream)PdfReader.GetPdfObject(contentObject); result = PdfReader.GetStreamBytes(stream); break; case PdfObject.ARRAY: // Stitch together all content before calling ProcessContent(), because // ProcessContent() resets state. MemoryStream allBytes = new MemoryStream(); PdfArray contentArray = (PdfArray)contentObject; ListIterator <PdfObject> iter = contentArray.GetListIterator(); while (iter.HasNext()) { PdfObject element = iter.Next(); byte[] b; allBytes.Write(b = GetContentBytesFromContentObject(element), 0, b.Length); allBytes.WriteByte((byte)' '); } result = allBytes.ToArray(); break; default: String msg = "Unable to handle Content of type " + contentObject.GetType(); throw new InvalidOperationException(msg); } return(result); }
/// <summary> /// Replace texts. /// TODO: Los textos con formato creado en word introducen caracteres entre las palabras y no lo localiza. /// </summary> public void Replace() { using (FileStream stream = new FileStream(_pathSource, FileMode.Open)) { using (PdfReader pdfReader = new PdfReader(stream)) { for (int x = 1; x <= pdfReader.NumberOfPages; x++) { using (FileStream streamDest = new FileStream(_pathDest, FileMode.Create)) { PdfDictionary dict = pdfReader.GetPageN(x); PdfObject obj = dict.GetDirectObject(PdfName.CONTENTS); if (obj.GetType() == typeof(PRStream)) { ReplacePRStream(obj); } if (obj.GetType() == typeof(PdfArray)) { foreach (var r in (PdfArray)obj) { PRIndirectReference ir = (PRIndirectReference)r; PdfObject refdObj = pdfReader.GetPdfObject(ir.Number); if (refdObj.IsStream()) { ReplacePRStream(refdObj); } } } using (PdfStamper stamper = new PdfStamper(pdfReader, streamDest)) { } } } } } }
/** * Gets the font pointed to by the indirect reference. The font may have been cached. * @param ind the indirect reference ponting to the font * @return the font * @since 5.0.6 */ private CMapAwareDocumentFont GetFont(PRIndirectReference ind) { CMapAwareDocumentFont font; cachedFonts.TryGetValue(ind.Number, out font); if (font == null) { font = new CMapAwareDocumentFont(ind); cachedFonts[ind.Number] = font; } return font; }
protected override PdfIndirectReference CopyIndirect(PRIndirectReference @in) { PdfObject srcObj = PdfReader.GetPdfObjectRelease(@in); PdfSmartCopy.ByteStore streamKey = null; bool validStream = false; if (srcObj.IsStream()) { streamKey = new PdfSmartCopy.ByteStore((PRStream)srcObj, serialized); validStream = true; PdfIndirectReference streamRef; if (streamMap.TryGetValue(streamKey, out streamRef)) { return(streamRef); } } else if (srcObj.IsDictionary()) { streamKey = new PdfSmartCopy.ByteStore((PdfDictionary)srcObj, serialized); validStream = true; PdfIndirectReference streamRef; if (streamMap.TryGetValue(streamKey, out streamRef)) { return(streamRef); } } PdfIndirectReference theRef; RefKey key = new RefKey(@in); IndirectReferences iRef; if (indirects.TryGetValue(key, out iRef)) { theRef = iRef.Ref; if (iRef.Copied) { return(theRef); } } else { theRef = body.PdfIndirectReference; iRef = new IndirectReferences(theRef); indirects[key] = iRef; } if (srcObj.IsDictionary()) { PdfObject type = PdfReader.GetPdfObjectRelease(((PdfDictionary)srcObj).Get(PdfName.TYPE)); if (type != null) { if ((PdfName.PAGE.Equals(type))) { return(theRef); } if ((PdfName.CATALOG.Equals(type))) { LOGGER.Warn(MessageLocalization.GetComposedMessage("make.copy.of.catalog.dictionary.is.forbidden")); return(null); } } } iRef.SetCopied(); if (validStream) { streamMap[streamKey] = theRef; } PdfObject obj = CopyObject(srcObj); AddToBody(obj, theRef); return(theRef); }
internal RefKey(PRIndirectReference reference) { num = reference.Number; gen = reference.Generation; }
private void GetFormFields(Stream source) { PdfReader reader = null; try { reader = new PdfReader(source); PRAcroForm form = reader.AcroForm; if (form == null) { //ac.debugText("This document has no fields."); return; } //PdfLister list = new PdfLister(System.out); Hashtable refToField = new Hashtable(); ArrayList fields = form.Fields; foreach (PRAcroForm.FieldInformation field in fields) { refToField.Add(field.Ref.Number, field); } for (int page = 1; page <= reader.NumberOfPages; page++) { PdfDictionary dPage = reader.GetPageN(page); PdfArray annots = (PdfArray)PdfReader.GetPdfObject((PdfObject)dPage.Get(PdfName.ANNOTS)); if (annots == null) { break; } ArrayList ali = annots.ArrayList; PRIndirectReference iRef = null; foreach (PdfObject refObj in ali) { PdfDictionary an = (PdfDictionary)PdfReader.GetPdfObject(refObj); PdfName name = (PdfName)an.Get(PdfName.SUBTYPE); if (name == null || !name.Equals(PdfName.WIDGET)) { break; } PdfArray rect = (PdfArray)PdfReader.GetPdfObject(an.Get(PdfName.RECT)); string fName = ""; PRAcroForm.FieldInformation field = null; while ((an != null)) { PdfString tName = (PdfString)an.Get(PdfName.T); if ((tName != null)) { fName = tName.ToString() + "." + fName; } if (refObj.IsIndirect() && field == null) { iRef = (PRIndirectReference)refObj; field = (PRAcroForm.FieldInformation)refToField[iRef.Number]; } //refObj = (PdfObject)an.Get(PdfName.PARENT); an = (PdfDictionary)PdfReader.GetPdfObject((PdfObject)an.Get(PdfName.PARENT)); } if (fName.EndsWith(".")) { fName = fName.Substring(0, fName.Length - 1); } PDFFieldLocation tempLoc = new PDFFieldLocation(); ArrayList arr = rect.ArrayList; tempLoc.fieldName = fName; tempLoc.page = page; tempLoc.x1 = ((PdfNumber)PdfReader.GetPdfObject((PdfObject)arr[0])).FloatValue; tempLoc.y1 = ((PdfNumber)PdfReader.GetPdfObject((PdfObject)arr[1])).FloatValue; tempLoc.x2 = ((PdfNumber)PdfReader.GetPdfObject((PdfObject)arr[2])).FloatValue; tempLoc.y2 = ((PdfNumber)PdfReader.GetPdfObject((PdfObject)arr[3])).FloatValue; this.PFDlocs.Add(tempLoc); } } } catch (Exception e) { throw new Exception("Critical Exception in GetFormFields", e); } finally { if ((reader != null)) { reader.Close(); } } }