Esempio n. 1
0
        /// <summary>
        /// Attempt to parse a sequence of bytes as a PDF object
        /// </summary>
        /// <param name="Data">Bytes to parse</param>
        /// <param name="EndingIndex">0-based index into data indicating the byte after the last parsed byte</param>
        /// <param name="StartingIndex">0-based index into data at which to start parsing (default: 0)</param>
        /// <returns>PDF object successfully parsed from the data, or NULL if no complete object was parsable</returns>
        public static IPDFObject Parse(byte[] Data, out int EndingIndex, int StartingIndex = 0)
        {
            int StartTokenIndex;
            int EndTokenIndex;

            IPDFObject ParsedObject = null;
            string     Token        = GetTokenString(Data, StartingIndex, out StartTokenIndex, out EndTokenIndex);

            if (!string.IsNullOrEmpty(Token))
            {
                if ((ParsedObject = PDFDictionary.TryParse(Token, Data, StartTokenIndex, out EndingIndex)) != null)
                {
                    return(ParsedObject);
                }

                if ((ParsedObject = PDFArray.TryParse(Token, Data, StartTokenIndex, out EndingIndex)) != null)
                {
                    return(ParsedObject);
                }

                if ((ParsedObject = PDFString.TryParse(Token, Data, StartTokenIndex, out EndingIndex)) != null)
                {
                    return(ParsedObject);
                }

                if ((ParsedObject = PDFHexString.TryParse(Token, Data, StartTokenIndex, out EndingIndex)) != null)
                {
                    return(ParsedObject);
                }

                if ((ParsedObject = PDFObjectDefinition.TryParse(Token, Data, StartTokenIndex, out EndingIndex)) != null)
                {
                    return(ParsedObject);
                }

                if ((ParsedObject = PDFIndirectObject.TryParse(Token, Data, StartTokenIndex, out EndingIndex)) != null)
                {
                    return(ParsedObject);
                }

                if ((ParsedObject = PDFNumber.TryParse(Token, Data, StartTokenIndex, out EndingIndex)) != null)
                {
                    return(ParsedObject);
                }

                if ((ParsedObject = PDFName.TryParse(Token, Data, StartTokenIndex, out EndingIndex)) != null)
                {
                    return(ParsedObject);
                }

                if ((ParsedObject = PDFComment.TryParse(Token, Data, StartTokenIndex, out EndingIndex)) != null)
                {
                    return(ParsedObject);
                }
            }

            // Could not parse next object
            EndingIndex = StartingIndex;
            return(null);
        }
Esempio n. 2
0
File: PDF.cs Progetto: jrhay/PKPDF
        /// <summary>
        /// Determine the maximum PDF version of a PDF file
        /// </summary>
        /// <param name="Pathname">Full pathname to the PDF file</param>
        /// <param name="isBinary">Does the PDF header indicate binary content?</param>
        /// <returns>PDF version number (1.0 - 1.7) as indicated by the PDF header, or 0.0 if file does not appear to be a PDF file</returns>
        private float ReadPDFHeader(out bool isBinary)
        {
            int   CommentEnd;
            float HeaderVersion = 0.0f;

            // Check for PDF version
            byte[] HeaderBytes;
            PDFComment.ExtractPDFComment(RawData, out HeaderBytes, out _, out CommentEnd);
            if (HeaderBytes.Length > 5)
            {
                string HeaderComment = Encoding.UTF8.GetString(HeaderBytes);
                if ("%PDF-".Equals(HeaderComment.Substring(0, 5)))
                {
                    if (!float.TryParse(HeaderComment.Substring(5), out HeaderVersion))
                    {
                        HeaderVersion = 0.0f;
                    }
                }
            }

            // Check for 8-bit-data flag
            isBinary = false;
            byte[] NextBytes = PDF.ExtractPDFLine(RawData, CommentEnd, out CommentEnd);      // Read to next EOL
            PDFComment.ExtractPDFComment(NextBytes, out HeaderBytes, out _, out CommentEnd); // Attempt to find a comment line
            if (HeaderBytes != null)
            {
                int binCount = 0;
                foreach (byte Byte in HeaderBytes)
                {
                    binCount = binCount + (Byte > 128 ? 1 : 0);
                }
                isBinary = (binCount > 3);
            }

            return(HeaderVersion);
        }