Пример #1
0
 public PDFXRefTableEntry this[PDFObjectRef oref]
 {
     get
     {
         for (int i = this._sections.Count - 1; i >= 0; i--)
         {
             PDFXRefTableSection section = this._sections[i];
             if (section.Start <= oref.Number && section.Start + section.Count > oref.Number)
             {
                 PDFXRefTableEntry entry = section[oref];
                 if (null != entry)
                 {
                     return(entry);
                 }
             }
         }
         if (null != this.Previous)
         {
             return(this.Previous[oref]);
         }
         else
         {
             return(null);
         }
     }
 }
Пример #2
0
        /// <summary>
        /// Gets the content of the indirect object specifed by the object reference.
        /// If the result is not found or does not exist, then an exception is thrown.
        /// </summary>
        /// <param name="oref"></param>
        /// <returns></returns>
        public IFileObject AssertGetContent(PDFObjectRef oref)
        {
            IFileObject result = this.GetContent(oref);

            if (null == result)
            {
                throw new NullReferenceException(String.Format(CommonErrors.AnIndirectObjectWithReferenceCouldNotBeFound, oref));
            }

            return(result);
        }
Пример #3
0
        // not used

        /*
         #region private PDFObjectRef[] InitPageRefs()
         *
         * private PDFObjectRef[] InitPageRefs()
         * {
         *  List<PDFObjectRef> all = new List<PDFObjectRef>();
         *  PDFObjectRef pagetree = this.DocumentCatalog[PagesName] as PDFObjectRef;
         *  if (null != pagetree)
         *      this.InitAPageOrTree(pagetree, all, null);
         *
         *  return all.ToArray();
         * }
         *
         #endregion
         *
         #region private void InitAPageOrTree(PDFObjectRef pagetree, List<PDFObjectRef> all)
         *
         * private void InitAPageOrTree(PDFObjectRef pageref, List<PDFObjectRef> all, PDFDictionary inherited)
         * {
         *  IParsedIndirectObject obj = this._reader.GetObject(pageref);
         *  if (null == obj)
         *      throw new PDFNativeParserException("Cannot find object with reference '" + pageref.ToString());
         *
         *  PDFDictionary dict = obj.GetContents() as PDFDictionary;
         *  if (null == dict)
         *      return;
         *
         *  PDFName type = dict[TypeName] as PDFName;
         *  if (null == type)
         *  {
         *      return;
         *  }
         *  else if (type.Value == "Pages")
         *  {
         *      PDFDictionary newinherited = null;
         *      PDFArray kids = null;
         *      //Type, Parent, Kids, Count are standard
         *      //Others are then inherited in the page.
         *      foreach (KeyValuePair<PDFName, IFileObject> item in dict)
         *      {
         *          switch (item.Key.Value)
         *          {
         *              case ("Type"):
         *              case ("Parent"):
         *              case ("Count"):
         *                  //Do nothing
         *                  break;
         *              case ("Kids"):
         *                  kids = item.Value as PDFArray;
         *                  break;
         *              default:
         *                  if (null == newinherited)
         *                      newinherited = new PDFDictionary();
         *
         *                  newinherited.Add(item.Key, item.Value);
         *                  break;
         *          }
         *
         *      }
         *      if (null != newinherited && newinherited.Count > 0)
         *      {
         *          if (null != inherited && inherited.Count > 0)
         *          {
         *              foreach (KeyValuePair<PDFName, IFileObject> item in inherited)
         *              {
         *                  newinherited.Add(item.Key, item.Value);
         *              }
         *          }
         *      }
         *      else
         *          newinherited = inherited;
         *
         *      if (null != kids)
         *      {
         *          foreach (PDFObjectRef oref in kids)
         *          {
         *              InitAPageOrTree(oref, all, newinherited);
         *          }
         *      }
         *  }
         *  else if (type.Value == "Page")
         *  {
         *      all.Add(pageref);
         *
         *  }
         *
         * }
         *
         #endregion
         */

        //
        // public interface
        //

        #region public IFileObject GetContent(PDFObjectRef oref)

        /// <summary>
        /// Gets the main content of object based on the reference
        /// </summary>
        /// <param name="oref"></param>
        /// <returns></returns>
        public IFileObject GetContent(PDFObjectRef oref)
        {
            IParsedIndirectObject obj = this._reader.GetObject(oref);

            if (null == obj)
            {
                return(null);
            }
            else
            {
                return(obj.GetContents());
            }
        }
        public PDFXRefTableEntry this[PDFObjectRef oref]
        {
            get
            {
                PDFXRefTableEntry entry = this[oref.Number - this.Start];

                //check the generations match
                if (null != entry && entry.Generation != oref.Generation)
                {
                    entry = null;
                }

                return(entry);
            }
        }
Пример #5
0
        /// <summary>
        /// Returns true if the object in the file (with reference oref) has an associated stream,
        /// and if so sets the data value to that stream data
        /// </summary>
        /// <param name="oref"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public bool TryGetStreamData(PDFObjectRef oref, out byte[] data)
        {
            data = null;
            IParsedIndirectObject obj = this._reader.GetObject(oref);

            if (null == obj)
            {
                return(false);
            }
            else
            {
                data = obj.GetStreamData();
                return(null != data);
            }
        }
Пример #6
0
        //
        // public methods
        //

        #region public IIndirectObject GetIndirectObject(PDFObjectRef oref)

        /// <summary>
        /// Reads and returns the object data returned based on the provided reference
        /// </summary>
        /// <param name="oref"></param>
        /// <returns></returns>
        public override IParsedIndirectObject GetObject(PDFObjectRef oref)
        {
            PDFXRefTableEntry entry = this.XRefTable[oref];

            if (null != entry)
            {
                if (entry.Free == false)
                {
                    if (null == entry.Reference)
                    {
                        this.Searcher.Position = entry.Offset;
                        entry.Reference        = PDFFileIndirectObject.Parse(this.Searcher, oref.Number, oref.Generation);
                    }
                    return((IParsedIndirectObject)entry.Reference);
                }
            }
            return(null);
        }
Пример #7
0
        /// <summary>
        /// Initializes the known PDF file data such as trailers, xref tables and catalogs
        /// </summary>
        protected override void InitData(PDFTraceLog log)
        {
            try
            {
                if (log.ShouldLog(TraceLevel.Debug))
                {
                    log.Add(TraceLevel.Debug, "PDFReader", "Finding end of file, startxref and trailer positions");
                }

                this.Searcher.Position = this.Searcher.Length;
                PDFFileRange eofPos       = AssertFoundRange(Searcher.MatchBackwardString(EndOfFileMarker), EndOfFileMarker);
                PDFFileRange startxrefPos = AssertFoundRange(Searcher.MatchBackwardString(StartXRefMarker), StartXRefMarker);

                PDFFileRange trailerPos = AssertFoundRange(Searcher.MatchBackwardString(TrailerMarker), TrailerMarker);

                if (log.ShouldLog(TraceLevel.Debug))
                {
                    log.Add(TraceLevel.Debug, "PDFReader", "Markers found, loading the trailer dictionary");
                }

                PDFDictionary trailer = GetTrailerDictionary(trailerPos, startxrefPos);
                this._trailer = trailer;

                if (log.ShouldLog(TraceLevel.Debug))
                {
                    log.Add(TraceLevel.Debug, "PDFReader", "Markers found, loading the XRef table");
                }

                PDFObjectRef catalogRef = AssertGetObjectRef(trailer, CatalogObjName, "The '" + CatalogObjName + "' entry couldnot be found in the documents trailer dictionary");
                PDFObjectRef infoRef    = AssertGetObjectRef(trailer, InfoObjName, "The '" + InfoObjName + "' entry couldnot be found in the documents trailer dictionary");
                IFileObject  prevXRefObj;
                trailer.TryGetValue(PrevXRefName, out prevXRefObj);
                long prevOffset = -1;
                if (prevXRefObj is PDFNumber)
                {
                    prevOffset = ((PDFNumber)prevXRefObj).Value;
                }
                else if (prevXRefObj is PDFReal)
                {
                    prevOffset = (long)((PDFNumber)prevXRefObj).Value;
                }

                PDFXRefTable xref = GetXRefTable(startxrefPos, eofPos, prevOffset);


                if (log.ShouldLog(TraceLevel.Debug))
                {
                    log.Add(TraceLevel.Debug, "PDFReader", "References for the catalog and document info found");
                }

                this._xreftable = xref;
                this._info      = (PDFFileIndirectObject)this.GetObject(infoRef);

                if (log.ShouldLog(TraceLevel.Debug))
                {
                    log.Add(TraceLevel.Debug, "PDFReader", "Loaded the document Info indirect object");
                }

                this._catalog = (PDFFileIndirectObject)this.GetObject(catalogRef);

                if (log.ShouldLog(TraceLevel.Debug))
                {
                    log.Add(TraceLevel.Debug, "PDFReader", "Loaded the document Catalog indirect object");
                }

                //TODO: Look for more updates and read those in too
            }
            catch (Exception ex)
            {
                throw new PDFNativeParserException(CommonErrors.CouldNotInitializeThePDFReader, ex);
            }
        }
Пример #8
0
        //
        // public methods
        //

        #region public abstract IParsedIndirectObject GetObject(PDFObjectRef oref);

        /// <summary>
        /// Returns the parsed indirect object that corresponds to the reference provided.
        /// </summary>
        /// <param name="oref"></param>
        /// <returns></returns>
        public abstract IParsedIndirectObject GetObject(PDFObjectRef oref);
        public Native.PDFObjectRef OutputToPDF(PDFWriter writer, PDFRenderContext context)
        {
            List <ICategorisedArtefactNamesEntry> entries = new List <ICategorisedArtefactNamesEntry>();

            Native.PDFObjectRef oref = null;
            if (this.InnerEntries.Count == 0)
            {
                return(oref);
            }
            if (this.NameType == PDFCategorisedNameDictionary.DestinationsName)
            {
                oref = writer.BeginObject();
                writer.BeginDictionary();
                writer.BeginDictionaryEntry("Names");


                string first = null;
                string last  = null;
                writer.BeginArray();
                foreach (KeyValuePair <string, IArtefactEntry> kvp in this.InnerEntries)
                {
                    if (string.IsNullOrEmpty(first))
                    {
                        first = kvp.Key;
                    }
                    else
                    {
                        last = kvp.Key;
                    }

                    writer.BeginArrayEntry();
                    writer.WriteStringLiteral(kvp.Key);
                    writer.EndArrayEntry();

                    writer.BeginArrayEntry();
                    ((PDFDestination)kvp.Value).OutputToPDF(context, writer);
                    writer.EndArrayEntry();
                    writer.WriteLine();
                }

                writer.EndArray();
                writer.EndDictionaryEntry();

                if (!string.IsNullOrEmpty(first) && !string.IsNullOrEmpty(last))
                {
                    writer.BeginDictionaryEntry("Limits");
                    writer.WriteArrayStringEntries(true, first, last);
                    writer.EndDictionaryEntry();
                }

                writer.EndDictionary();
                writer.EndObject();
            }
            else //we contain ICategorizedArtefactNameEntry(s)
            {
                oref = writer.BeginObject();
                writer.BeginDictionary();
                writer.BeginDictionaryEntry("Names");


                //string first = null;
                //string last = null;

                writer.BeginArray();
                foreach (KeyValuePair <string, IArtefactEntry> kvp in this.InnerEntries)
                {
                    //if (string.IsNullOrEmpty(first))
                    //    first = kvp.Key;
                    //else
                    //    last = kvp.Key;

                    writer.BeginArrayEntry();
                    writer.WriteStringLiteral(kvp.Key);
                    writer.EndArrayEntry();

                    writer.BeginArrayEntry();
                    ICategorisedArtefactNamesEntry entry = (ICategorisedArtefactNamesEntry)kvp.Value;

                    Native.PDFObjectRef entryOref = entry.OutputToPDF(context, writer);
                    if (null != entryOref)
                    {
                        writer.WriteObjectRef(entryOref);
                    }

                    writer.EndArrayEntry();
                    writer.WriteLine();
                }

                writer.EndArray();
                writer.EndDictionaryEntry();

                //Limits only required on Intermediate and Leaf nodes of a tree, not the root
                //if (!string.IsNullOrEmpty(first) && !string.IsNullOrEmpty(last))
                //{
                //    writer.BeginDictionaryEntry("Limits");
                //    writer.WriteArrayStringEntries(true, first, last);
                //    writer.EndDictionaryEntry();

                //}

                writer.EndDictionary();
                writer.EndObject();
            }

            return(oref);
        }
Пример #10
0
        //
        // initialization
        //

        #region protected virtual void Init()

        /// <summary>
        /// Called from the factory methods once instantiated to initialize the data.
        /// </summary>
        protected virtual void Init()
        {
            this._pageTree = this.DocumentCatalog[PagesName] as PDFObjectRef;
        }