Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Attempt to parse the given data stream, returning an indicator of parse progress
        /// </summary>
        /// <param name="StartingToken">The token immediately preceeding the starting index in Data stream</param>
        /// <param name="Data">Raw byte stream to parse</param>
        /// <param name="StartingIndex">0-based starting index into Data where StartingToken appears</param>
        /// <param name="EndingIndex">Index into data stream where parsing ended (either successfully or unsuccessfully)</param>
        /// <returns>Object parsed from data stream, or NULL if unable to parse. If NULL and EndingIndex is equal to Data.Length, parsing may be successful with more data</returns>
        public static IPDFObject TryParse(string StartingToken, byte[] Data, int StartingIndex, out int EndingIndex)
        {
            int Number;

            EndingIndex = StartingIndex;
            if (int.TryParse(StartingToken, out Number))
            {
                string Declaration = PDFObjectParser.GetTokenString(Data, StartingIndex + StartingToken.Length, out _, out EndingIndex, 2);
                if (!String.IsNullOrEmpty(Declaration))
                {
                    Match objMatch = Regex.Match(Declaration, @"([+-]?\d+) obj");
                    if (objMatch.Success)
                    {
                        if (Declaration.Length != objMatch.Length)
                        {
                            EndingIndex = EndingIndex - (Declaration.Length - objMatch.Length) - 1;
                        }
                        int Generation = int.Parse(objMatch.Groups[1].Value);

                        // Parse the indirect object content (should be a single object)
                        IPDFObject PDFObject = PDFObjectParser.Parse(Data, out EndingIndex, EndingIndex);
                        if (PDFObject != null)
                        {
                            int ObjectEndIndex = PDF.FirstOccurance(Data, Encoding.UTF8.GetBytes("endobj"), EndingIndex);
                            if (ObjectEndIndex > 0)
                            {
                                // Format error - tokens after object definition but before "endobj"
                            }

                            EndingIndex = ObjectEndIndex + "endobj".Length;
                            return(new PDFObjectDefinition(Number, Generation, PDFObject));
                        }
                    }
                }
            }

            return(null);
        }