private static BaseFont FindFontInFontDict(PdfDictionary fontDict, string sourceFieldFontName) { var fieldFontName = fontDict.Keys .FirstOrDefault(f => sourceFieldFontName.StartsWith(f.ToString())); var iRef = (PRIndirectReference)fontDict .GetAsIndirectObject(fieldFontName); var font = iRef != null?BaseFont.CreateFont(iRef) : null; if (!string.IsNullOrWhiteSpace(font?.Encoding)) { return(font); } foreach (var fontName in fontDict.Keys) { iRef = (PRIndirectReference)fontDict .GetAsIndirectObject(fontName); font = iRef != null?BaseFont.CreateFont(iRef) : null; if (!string.IsNullOrWhiteSpace(font?.Encoding)) { return(font); } } return(null); }
/** * Creates a StructureObject for an OBJR dictionary. * @param structElem the parent structure element * @param ref the reference of the parent structure element * @param dict the object reference dictionary */ public StructureObject(PdfDictionary structElem, PdfIndirectReference refa, PdfDictionary dict) { this.structElem = structElem; this.refa = refa; this.obj = dict.GetDirectObject(PdfName.OBJ); this.objref = dict.GetAsIndirectObject(PdfName.OBJ); this.structParent = ((PdfDictionary) obj).GetAsNumber(PdfName.STRUCTPARENT).IntValue; PdfIndirectReference pg = dict.GetAsIndirectObject(PdfName.PG); if (pg == null) pg = structElem.GetAsIndirectObject(PdfName.PG); this.pageref = pg.Number; }
/** * Creates a StructureObject for an OBJR dictionary. * @param structElem the parent structure element * @param ref the reference of the parent structure element * @param dict the object reference dictionary */ public StructureObject(PdfDictionary structElem, PdfIndirectReference refa, PdfDictionary dict) { this.structElem = structElem; this.refa = refa; this.obj = dict.GetDirectObject(PdfName.OBJ); this.objref = dict.GetAsIndirectObject(PdfName.OBJ); this.structParent = ((PdfDictionary)obj).GetAsNumber(PdfName.STRUCTPARENT).IntValue; PdfIndirectReference pg = dict.GetAsIndirectObject(PdfName.PG); if (pg == null) { pg = structElem.GetAsIndirectObject(PdfName.PG); } this.pageref = pg.Number; }
private void TestFile(String filename, int page, String objectid) { PdfReader pdfReader = TestResourceUtils.GetResourceAsPdfReader(TEST_RESOURCES_PATH, filename); try { PdfDictionary resources = pdfReader.GetPageResources(page); PdfDictionary xobjects = resources.GetAsDict(PdfName.XOBJECT); PdfIndirectReference objRef = xobjects.GetAsIndirectObject(new PdfName(objectid)); if (objRef == null) { throw new NullReferenceException("Reference " + objectid + " not found - Available keys are " + xobjects.Keys); } PRStream stream = (PRStream)PdfReader.GetPdfObject(objRef); PdfDictionary colorSpaceDic = resources != null?resources.GetAsDict(PdfName.COLORSPACE) : null; PdfImageObject img = new PdfImageObject(stream, colorSpaceDic); byte[] result = img.GetImageAsBytes(); Assert.NotNull(result); int zeroCount = 0; foreach (byte b in result) { if (b == 0) { zeroCount++; } } Assert.IsTrue(zeroCount > 0); } finally { pdfReader.Close(); } }
// --------------------------------------------------------------------------- /** * Extracts document level attachments * @param PDF from which document level attachments will be extracted * @param zip the ZipFile object to add the extracted images */ public void ExtractDocLevelAttachments(byte[] pdf, ZipFile zip) { PdfReader reader = new PdfReader(pdf); PdfDictionary root = reader.Catalog; PdfDictionary documentnames = root.GetAsDict(PdfName.NAMES); PdfDictionary embeddedfiles = documentnames.GetAsDict(PdfName.EMBEDDEDFILES); PdfArray filespecs = embeddedfiles.GetAsArray(PdfName.NAMES); for (int i = 0; i < filespecs.Size;) { filespecs.GetAsString(i++); PdfDictionary filespec = filespecs.GetAsDict(i++); PdfDictionary refs = filespec.GetAsDict(PdfName.EF); foreach (PdfName key in refs.Keys) { PRStream stream = (PRStream)PdfReader.GetPdfObject( refs.GetAsIndirectObject(key) ); zip.AddEntry( filespec.GetAsString(key).ToString(), PdfReader.GetStreamBytes(stream) ); } } }
public Boolean PDFPortfolio(byte[] pdf, string filename, Boolean weitergabe, SPFolder tempfolder, string tempuploadurl, string DocID) { Boolean portfolio; using (PdfReader reader = new PdfReader(pdf)) { PdfReader.unethicalreading = true; PdfDictionary documentNames = null; PdfDictionary embeddedFiles = null; PdfDictionary fileArray = null; PdfDictionary file = null; PRStream stream = null; PdfDictionary catalog = reader.Catalog; documentNames = (PdfDictionary)PdfReader.GetPdfObject(catalog.Get(PdfName.NAMES)); if (documentNames != null) { //Erster Check: Portfolio ist vorhanden embeddedFiles = (PdfDictionary)PdfReader.GetPdfObject(documentNames.Get(PdfName.EMBEDDEDFILES)); if (embeddedFiles != null) { //Zweiter Check: Portfolio ist wirklich vorhanden portfolio = true; PdfArray filespecs = embeddedFiles.GetAsArray(PdfName.NAMES); for (int i = 0; i < filespecs.Size; i++) { i++; fileArray = filespecs.GetAsDict(i); file = fileArray.GetAsDict(PdfName.EF); int filecount = 0; foreach (PdfName key in file.Keys) { stream = (PRStream)PdfReader.GetPdfObject(file.GetAsIndirectObject(key)); //string attachedFileName = folderName + fileArray.GetAsString(key).ToString(); filecount++; string attachedFileName = DocID + "_" + filename + "_ExportFromPortfolio_" + i + ".pdf"; byte[] attachedFileBytes = PdfReader.GetStreamBytes(stream); SPFile attachmentuploadfile = tempfolder.Files.Add(tempuploadurl + attachedFileName, attachedFileBytes, true); SPListItem attachmentitem = attachmentuploadfile.Item; attachmentitem["Weitergabe"] = weitergabe; attachmentitem.SystemUpdate(); } } } else { //Zweiter Check: Doch kein Portfolio vorhanden portfolio = false; } } else { //Erster check: Kein Portfolio vorhanden portfolio = false; } } return(portfolio); }
/** * Creates a StructureObject for an OBJR dictionary. * @param structElem the parent structure element * @param ref the reference of the parent structure element * @param dict the object reference dictionary */ public StructureObject(PdfDictionary structElem, PdfIndirectReference refa, PdfDictionary dict) { this.structElem = structElem; this.refa = refa; this.obj = dict.GetDirectObject(PdfName.OBJ); this.objref = dict.GetAsIndirectObject(PdfName.OBJ); PdfNumber sp = ((PdfDictionary)obj).GetAsNumber(PdfName.STRUCTPARENT); if (sp == null) { throw new InvalidPdfException("structparentid.not.found"); } this.structParent = sp.IntValue; PdfIndirectReference pg = dict.GetAsIndirectObject(PdfName.PG); if (pg == null) { pg = structElem.GetAsIndirectObject(PdfName.PG); } this.pageref = pg.Number; }
/** * Processes the kids object of a StructElem dictionary. * This kids object can be a number (MCID), another StructElem dictionary, * an MCR dictionary, an OBJR dictionary, or an array of the above. * @param structElem the StructElem dictionary * @param ref the reference to the StructElem dictionary * @param object the kids object */ protected virtual void ProcessStructElemKids(PdfDictionary structElem, PdfIndirectReference refa, PdfObject objecta) { if (LOGGER.IsLogging(Level.INFO)) { LOGGER.Info(String.Format("addStructureItem({0}, {1}, {2})", structElem, refa, objecta)); } if (objecta == null) { return; } StructureItem item; switch (objecta.Type) { case PdfObject.NUMBER: item = new StructureMCID(structElem.GetAsIndirectObject(PdfName.PG), (PdfNumber)objecta); Add(item); LOGGER.Info("Added " + item); break; case PdfObject.ARRAY: PdfArray array = (PdfArray)objecta; for (int i = 0; i < array.Size; i++) { ProcessStructElemKids(structElem, array.GetAsIndirectObject(i), array.GetDirectObject(i)); } break; case PdfObject.DICTIONARY: PdfDictionary dict = (PdfDictionary)objecta; if (dict.CheckType(PdfName.MCR)) { item = new StructureMCID(dict); Add(item); LOGGER.Info("Added " + item); } else if (dict.CheckType(PdfName.OBJR)) { item = new StructureObject(structElem, refa, dict); Add(item); LOGGER.Info("Added " + item); } else { ProcessStructElems(dict, refa); } break; } }
public static string ExtractEdiconData(string srcEdiconPdfFileName) { PdfReader reader = null; // try { reader = new PdfReader(srcEdiconPdfFileName); // PdfDictionary root = reader.Catalog; PdfDictionary documentnames = root.GetAsDict(PdfName.NAMES); PdfDictionary embeddedfiles = documentnames.GetAsDict(PdfName.EMBEDDEDFILES); PdfArray filespecs = embeddedfiles.GetAsArray(PdfName.NAMES); // for (int i = 0; i < filespecs.Size;) { filespecs.GetAsString(i++); PdfDictionary filespec = filespecs.GetAsDict(i++); PdfDictionary refs = filespec.GetAsDict(PdfName.EF); // foreach (PdfName key in refs.Keys) { PRStream stream = (PRStream)PdfReader.GetPdfObject(refs.GetAsIndirectObject(key)); // var outName = filespec.GetAsString(key).ToString(); // if (outName.Equals(EDICON_EMBED_FILENAME, StringComparison.OrdinalIgnoreCase)) { byte[] outByteArray = PdfReader.GetStreamBytes(stream); string ediconData = Encoding.UTF8.GetString(outByteArray); return(ediconData); } } } } catch (Exception ex) { //Console.WriteLine("ExtractEdiconData error: " + ex.Message); throw new EdiconException("Soubor neobsahuje platnou přílohu Edicon"); } finally { if (reader != null) { reader.Close(); } } return(null); }
internal void ExtractAttachments(string file_name, string folderName, PdfWriter write) { PdfDictionary documentNames = null; PdfDictionary embeddedFiles = null; PdfDictionary fileArray = null; PdfDictionary file = null; PRStream stream = null; PdfReader reader = new PdfReader(file_name); PdfDictionary catalog = reader.Catalog; documentNames = (PdfDictionary)PdfReader.GetPdfObject(catalog.Get(PdfName.NAMES)); if (documentNames != null) { embeddedFiles = (PdfDictionary)PdfReader.GetPdfObject(documentNames.Get(PdfName.EMBEDDEDFILES)); if (embeddedFiles != null) { PdfArray filespecs = embeddedFiles.GetAsArray(PdfName.NAMES); for (int i = 0; i < filespecs.Size; i++) { i++; fileArray = filespecs.GetAsDict(i); file = fileArray.GetAsDict(PdfName.EF); foreach (PdfName key in file.Keys) { stream = (PRStream)PdfReader.GetPdfObject(file.GetAsIndirectObject(key)); string attachedFileName = folderName + fileArray.GetAsString(key).ToString(); byte[] attachedFileBytes = PdfReader.GetStreamBytes(stream); //graba el anexo extraido System.IO.File.WriteAllBytes(attachedFileName, attachedFileBytes); //adjunta los anexos PdfFileSpecification pfs = PdfFileSpecification.FileEmbedded(write, attachedFileName, fileArray.GetAsString(key).ToString(), null); write.AddFileAttachment(pfs); //borramos los archivos extraidos System.IO.File.Delete(attachedFileName); } } } } }
public void ManipulatePdf(string src, string dest) { PdfReader reader = new PdfReader(src); // We assume that there's a single large picture on the first page PdfDictionary page = reader.GetPageN(1); PdfDictionary resources = page.GetAsDict(PdfName.RESOURCES); PdfDictionary xobjects = resources.GetAsDict(PdfName.XOBJECT); Dictionary <PdfName, PdfObject> .KeyCollection.Enumerator enumerator = xobjects.Keys.GetEnumerator(); enumerator.MoveNext(); PdfName imgName = enumerator.Current; Image img = Image.GetInstance((PRIndirectReference)xobjects.GetAsIndirectObject(imgName)); img.SetAbsolutePosition(0, 0); img.ScaleAbsolute(reader.GetPageSize(1)); PdfStamper stamper = new PdfStamper(reader, new FileStream(dest, FileMode.Create)); stamper.GetOverContent(1).AddImage(img); stamper.Close(); reader.Close(); }
/** * Displays an XObject using the registered handler for this XObject's subtype * @param xobjectName the name of the XObject to retrieve from the resource dictionary */ private void DisplayXObject(PdfName xobjectName) { PdfDictionary xobjects = resources.GetAsDict(PdfName.XOBJECT); PdfObject xobject = xobjects.GetDirectObject(xobjectName); PdfStream xobjectStream = (PdfStream)xobject; PdfName subType = xobjectStream.GetAsName(PdfName.SUBTYPE); if (xobject.IsStream()) { IXObjectDoHandler handler; xobjectDoHandlers.TryGetValue(subType, out handler); if (handler == null) { handler = xobjectDoHandlers[PdfName.DEFAULT]; } handler.HandleXObject(this, xobjectStream, xobjects.GetAsIndirectObject(xobjectName)); } else { throw new InvalidOperationException(MessageLocalization.GetComposedMessage("XObject.1.is.not.a.stream", xobjectName)); } }
/** * Creates a list of StructuredItem objects. * @param reader the reader holding the PDF to examine */ public StructureItems(PdfReader reader) { PdfDictionary catalog = reader.Catalog; structTreeRoot = catalog.GetAsDict(PdfName.STRUCTTREEROOT); if (structTreeRoot == null) { throw new DocumentException(MessageLocalization.GetComposedMessage("can.t.read.document.structure")); } // Storing the parent tree parentTree = PdfNumberTree.ReadTree(structTreeRoot.GetAsDict(PdfName.PARENTTREE)); structTreeRoot.Remove(PdfName.STRUCTPARENTS); // Examining the StructTreeRoot PdfObject objecta = structTreeRoot.GetDirectObject(PdfName.K); if (objecta == null) { return; } switch (objecta.Type) { case PdfObject.DICTIONARY: LOGGER.Info("StructTreeRoot refers to dictionary"); ProcessStructElems((PdfDictionary)objecta, structTreeRoot.GetAsIndirectObject(PdfName.K)); break; case PdfObject.ARRAY: LOGGER.Info("StructTreeRoot refers to array"); PdfArray array = (PdfArray)objecta; for (int i = 0; i < array.Size; i++) { ProcessStructElems(array.GetAsDict(i), array.GetAsIndirectObject(i)); } break; } }
/** * Creates a StructurItem using an MCR dictionary. * @param dict an MCR dictionary */ public StructureMCID(PdfDictionary mcr) { mcid = mcr.GetAsNumber(PdfName.MCID).IntValue; pageref = mcr.GetAsIndirectObject(PdfName.PG).Number; }
private String GetParentIndRefStr(PdfDictionary dict) { return(dict.GetAsIndirectObject(PdfName.PARENT).ToString()); }
/** * Converts an annotation structure item to a Form XObject annotation. * @param item the structure item * @throws IOException */ protected void ConvertToXObject(StructureObject item) { PdfDictionary structElem = item.GetStructElem(); if (structElem == null) { return; } PdfDictionary dict = item.GetObjAsDict(); if (dict == null || !dict.CheckType(PdfName.ANNOT)) { return; } PdfDictionary ap = dict.GetAsDict(PdfName.AP); if (ap == null) { return; } PdfNumber structParent = dict.GetAsNumber(PdfName.STRUCTPARENT); if (structParent == null) { return; } PdfStream stream = ap.GetAsStream(PdfName.N); if (stream == null) { return; } stream.Put(PdfName.STRUCTPARENT, structParent); PdfIndirectReference xobjr = ap.GetAsIndirectObject(PdfName.N); if (xobjr == null) { return; } // remove the annotation from the page for (int i = 0; i < annots.Length; i++) { PdfIndirectReference annotref = annots.GetAsIndirectObject(i); if (item.GetObjRef().Number == annotref.Number) { annots.Remove(i); break; } } // replace the existing attributes by a PrintField attribute PdfDictionary attribute = new PdfDictionary(); attribute.Put(PdfName.O, PdfName.PRINTFIELD); PdfString description = dict.GetAsString(PdfName.TU); if (description == null) { description = dict.GetAsString(PdfName.T); } if (PdfName.BTN.Equals(dict.Get(PdfName.FT))) { PdfNumber fflags = dict.GetAsNumber(PdfName.FF); if (fflags != null) { int ff = fflags.IntValue; if ((ff & PdfFormField.FF_PUSHBUTTON) != 0) { attribute.Put(PdfName.ROLE, PdfName.PB); } // I don't think the condition below will ever be true if ((ff & PdfFormField.FF_RADIO) != 0) { attribute.Put(PdfName.ROLE, PdfName.rb); } else { attribute.Put(PdfName.ROLE, PdfName.CB); } } } else { attribute.Put(PdfName.ROLE, PdfName.TV); } attribute.Put(PdfName.DESC, description); // Updating the values of the StructElem dictionary PdfString t = structElem.GetAsString(PdfName.T); if (t == null || t.ToString().Trim().Length == 0) { structElem.Put(PdfName.T, dict.GetAsString(PdfName.T)); } structElem.Put(PdfName.A, attribute); structElem.Put(PdfName.S, PdfName.P); structElem.Put(PdfName.PG, pageref); // Defining a new MCID int mcid = items.ProcessMCID(structParents, item.GetRef()); LOGGER.Info("Using MCID " + mcid); structElem.Put(PdfName.K, new PdfNumber(mcid)); // removing the annotation from the parent tree items.RemoveFromParentTree(structParent); // Adding the XObject to the page PdfName xobj = new PdfName("XObj" + structParent.IntValue); LOGGER.Info("Creating XObject with name " + xobj); xobjects.Put(xobj, xobjr); PdfArray array = dict.GetAsArray(PdfName.RECT); // Getting the position of the annotation Rectangle rect = new Rectangle( array.GetAsNumber(0).FloatValue, array.GetAsNumber(1).FloatValue, array.GetAsNumber(2).FloatValue, array.GetAsNumber(3).FloatValue); rect.Normalize(); // A Do operator is forbidden inside a text block if (inText && !btWrite) { LOGGER.Debug("Introducing extra ET"); byte[] bytes = Encoding.ASCII.GetBytes("ET\n"); baos.Write(bytes, 0, bytes.Length); etExtra = true; } // Writing the marked-content sequence with the Do operator // Note that the position assumes that the CTM wasn't changed in the graphics state // TODO: do the math if the CTM did change! ByteBuffer buf = new ByteBuffer(); buf.Append("/P <</MCID "); buf.Append(mcid); buf.Append(">> BDC\n"); buf.Append("q 1 0 0 1 "); buf.Append(rect.Left.ToString(CultureInfo.InvariantCulture)); buf.Append(" "); buf.Append(rect.Bottom.ToString(CultureInfo.InvariantCulture)); buf.Append(" cm "); buf.Append(xobj.GetBytes()); buf.Append(" Do Q\n"); buf.Append("EMC\n"); buf.Flush(); buf.WriteTo(baos); // if we were inside a text block, we've introduced an ET, so we'll need to write a BT if (inText) { btWrite = true; } }
public static List <Attachment> GetAttachments(Stream stream, string password = null) { PdfReader reader = null; if (!string.IsNullOrWhiteSpace(password)) { reader = new PdfReader(stream, Encoding.ASCII.GetBytes(password)); } else { reader = new PdfReader(stream); } #region Variables PdfDictionary catalog = null; PdfDictionary documentNames = null; PdfDictionary embeddedFiles = null; PdfDictionary fileArray = null; PdfDictionary file = null; PRStream prstream = null; Attachment fContent = null; List <Attachment> lstAtt = null; #endregion Variables catalog = reader.Catalog; lstAtt = new List <Attachment>(); documentNames = (PdfDictionary)PdfReader.GetPdfObject(catalog.Get(PdfName.NAMES)); if (documentNames != null) { embeddedFiles = (PdfDictionary)PdfReader.GetPdfObject(documentNames.Get(PdfName.EMBEDDEDFILES)); if (embeddedFiles != null) { PdfArray filespecs = embeddedFiles.GetAsArray(PdfName.NAMES); for (int i = 0; i < filespecs.Size; i++) { i++; fileArray = filespecs.GetAsDict(i); file = fileArray.GetAsDict(PdfName.EF); foreach (PdfName key in file.Keys) { prstream = (PRStream)PdfReader.GetPdfObject(file.GetAsIndirectObject(key)); fContent = new Attachment(); fContent.Name = fileArray.GetAsString(key).ToString(); fContent.Content = PdfReader.GetStreamBytes(prstream); lstAtt.Add(fContent); } } } } return(lstAtt); }