public static PDFStream MakeStream(PDFDictionary StreamDictionary, byte[] Data, int StartingIndex, out int EndingIndex) { int TokEnd; int StreamLength = 0; if (StreamDictionary.ContainsKey("Length")) { StreamLength = (int)(Decimal.Parse(StreamDictionary["Length"].Description)); } else { StreamLength = PDF.FirstOccurance(Data, Encoding.UTF8.GetBytes("endstream"), StartingIndex); } EndingIndex = StartingIndex; if ("stream".Equals(PDFObjectParser.GetTokenString(Data, EndingIndex, out _, out TokEnd))) { if ((StreamLength > 0) && (Data.Length >= TokEnd + 1 + StreamLength)) { byte[] StreamBytes = new byte[StreamLength]; Array.Copy(Data, TokEnd + 1, StreamBytes, 0, StreamLength); EndingIndex = TokEnd + 1 + StreamLength; if (!"endstream".Equals(PDFObjectParser.GetTokenString(Data, EndingIndex, out _, out EndingIndex))) { } // Error - missing "endstream" return(new PDFStream(StreamDictionary, StreamBytes)); } } // Unable to make a stream from this data return(null); }
public PDFTrailer(PDFDictionary TrailerDictionary, PDFCrossReference CrossReference) : base(TrailerDictionary) { if (CrossReference == null) { this.CrossReference = new PDFCrossReference(); } else { this.CrossReference = CrossReference; } }
/// <summary> /// Read the trailer from a PDF data file /// </summary> /// <param name="Data">Data to read</param> /// <param name="StartIndex">Starting index of where to look for trailer, or -1 to look from end of file (default: -1)</param> /// <returns>TRUE if a trailer was successfully read, FALSE otherwise</returns> public static PDFTrailer ReadTrailer(byte[] Data, int StartIndex = -1) { int EndIndex = StartIndex; if (EndIndex < 0) { EndIndex = PDF.FindEOF(Data, Data.Length - 1); } if (EndIndex < 0) { return(null); } int EndOfLineIndex; byte[] LineData = PDF.ExtractPreviousPDFLine(Data, EndIndex, out EndIndex, out EndOfLineIndex); while (LineData != null) { if ("trailer".Equals(Encoding.UTF8.GetString(LineData).Trim())) { int TokenStartIndex = 0; string Token = PDFObjectParser.GetTokenString(Data, EndOfLineIndex, out TokenStartIndex, out EndIndex); PDFDictionary TrailerDictionary = (PDFDictionary)PDFDictionary.TryParse(Token, Data, TokenStartIndex, out EndIndex); if (TrailerDictionary != null) { LineData = PDF.ExtractPDFLine(Data, EndIndex, out EndIndex); if ("startxref".Equals(Encoding.UTF8.GetString(LineData).Trim())) { Token = PDFObjectParser.GetTokenString(Data, EndIndex, out TokenStartIndex, out _); PDFNumber Offset = (PDFNumber)PDFNumber.TryParse(Token, Data, TokenStartIndex, out EndIndex); PDFCrossReference CrossRef = PDFCrossReference.ReadCrossReference(Data, Offset, out _); return(new PDFTrailer(TrailerDictionary, CrossRef)); } else { return(null); } } else { return(null); } } else { LineData = PDF.ExtractPreviousPDFLine(Data, EndIndex, out EndIndex, out EndOfLineIndex); } } return(null); }
/// <summary> /// Reads the PDF catalog dictionary, if it can be found /// </summary> /// <returns>TRUE if catalog dictionary found and loaded successfully, FALSE otherwise</returns> private bool ReadPDFCatalog() { Catalog = null; if ((Trailer != null) && (Trailer.Keys.Contains("Root"))) { IPDFObject catalogObject = GetObject(Trailer["Root"]); if ((catalogObject != null) && (catalogObject.Type == PDFObjectType.Dictionary)) { Catalog = (PDFDictionary)catalogObject; } } return(Catalog != null); }
public PDFStream(PDFDictionary StreamDictionary, byte[] StreamData) { StreamDictionary["Length"] = new PDFNumber(StreamData.Length); this.StreamDictionary = StreamDictionary; this.Data = StreamData; }