/** * <summary>Gets the object equivalent to the given value.</summary> */ public static PdfDirectObject Get( object value ) { if (value == null) { return(null); } if (value is int) { return(PdfInteger.Get((int)value)); } if (value is double || value is float) { return(PdfReal.Get(value)); } if (value is string) { return(PdfTextString.Get((string)value)); } if (value is DateTime) { return(PdfDate.Get((DateTime)value)); } if (value is bool) { return(PdfBoolean.Get((bool)value)); } throw new NotImplementedException(); }
/** * <summary>Parses the current PDF object [PDF:1.6:3.2].</summary> */ public virtual PdfDataObject ParsePdfObject( ) { switch (TokenType) { case TokenTypeEnum.Integer: return(PdfInteger.Get((int)Token)); case TokenTypeEnum.Name: return(new PdfName((string)Token, true)); case TokenTypeEnum.DictionaryBegin: { PdfDictionary dictionary = new PdfDictionary(); dictionary.Updateable = false; while (true) { // Key. MoveNext(); if (TokenType == TokenTypeEnum.DictionaryEnd) { break; } PdfName key = (PdfName)ParsePdfObject(); // Value. MoveNext(); PdfDirectObject value = (PdfDirectObject)ParsePdfObject(); // Add the current entry to the dictionary! dictionary[key] = value; } dictionary.Updateable = true; return(dictionary); } case TokenTypeEnum.ArrayBegin: { PdfArray array = new PdfArray(); array.Updateable = false; while (true) { // Value. MoveNext(); if (TokenType == TokenTypeEnum.ArrayEnd) { break; } // Add the current item to the array! array.Add((PdfDirectObject)ParsePdfObject()); } array.Updateable = true; return(array); } case TokenTypeEnum.Literal: if (Token is DateTime) { return(PdfDate.Get((DateTime)Token)); } else { return(new PdfTextString( Encoding.Pdf.Encode((string)Token) )); } case TokenTypeEnum.Hex: return(new PdfTextString( (string)Token, PdfString.SerializationModeEnum.Hex )); case TokenTypeEnum.Real: return(PdfReal.Get((double)Token)); case TokenTypeEnum.Boolean: return(PdfBoolean.Get((bool)Token)); case TokenTypeEnum.Null: return(null); default: throw new PostScriptParseException(String.Format("Unknown type beginning: '{0}'", Token), this); } }
/** * <remarks> * <para>Require[0]: when this method is invoked, the pointer MUST be at (the end of) the first * token of the object.</para> * <para>Ensure[0]: when this method terminates, the pointer IS at (the end of) the last token of the object.</para> * <para>Invariant[0]: stream data IS kept untouched.</para> * <para>Side effect[0]: see Ensure[0].</para> * </remarks> */ public PdfDirectObject ParsePdfObject( ) { do { switch (tokenType) { case TokenTypeEnum.Integer: return(new PdfInteger((int)token)); case TokenTypeEnum.Name: return(new PdfName((string)token, true)); case TokenTypeEnum.Literal: return(new PdfString( org.pdfclown.tokens.Encoding.Encode((string)token), PdfString.SerializationModeEnum.Literal )); case TokenTypeEnum.DictionaryBegin: PdfDictionary dictionary = new PdfDictionary(); // Populate the dictionary. while (true) { // Key. MoveNext(); if (tokenType == TokenTypeEnum.DictionaryEnd) { break; } PdfName key = (PdfName)ParsePdfObject(); // Value. MoveNext(); PdfDirectObject value = (PdfDirectObject)ParsePdfObject(); // Add the current entry to the dictionary! dictionary[key] = value; } return(dictionary); case TokenTypeEnum.ArrayBegin: PdfArray array = new PdfArray(); // Populate the array. while (true) { // Value. MoveNext(); if (tokenType == TokenTypeEnum.ArrayEnd) { break; } // Add the current item to the array! array.Add((PdfDirectObject)ParsePdfObject()); } return(array); case TokenTypeEnum.Real: return(new PdfReal((float)token)); case TokenTypeEnum.Boolean: return(PdfBoolean.Get((bool)token)); case TokenTypeEnum.Date: return(new PdfDate((DateTime)token)); case TokenTypeEnum.Hex: return(new PdfString( (string)token, PdfString.SerializationModeEnum.Hex )); case TokenTypeEnum.Null: return(null); case TokenTypeEnum.Comment: // NOOP: Comments are simply ignored and skipped. break; default: throw new Exception("Unknown type: " + tokenType); } } while(MoveNext()); return(null); }
/** * <summary>Parses the current PDF object [PDF:1.6:3.2].</summary> */ public PdfDataObject ParsePdfObject( ) { /* * NOTE: Object parsing is intrinsically a sequential operation tied to the stream pointer. * Calls bound towards other classes are potentially disruptive for the predictability of * the position of the stream pointer, so we are forced to carefully keep track of our * current position in order to recover its proper state after any outbound call. */ do { // Which token type? switch (tokenType) { case TokenTypeEnum.Integer: return(new PdfInteger((int)token)); case TokenTypeEnum.Name: return(new PdfName((string)token, true)); case TokenTypeEnum.Reference: /* * NOTE: Curiously, PDF references are the only primitive objects that require * a file reference. That's because they deal with indirect objects, which are strongly * coupled with the current state of the file: so, PDF references are the fundamental * bridge between the token layer and the file layer. */ return(new PdfReference( (Reference)token, file )); case TokenTypeEnum.Literal: return(new PdfTextString( Encoding.Encode((string)token) )); case TokenTypeEnum.DictionaryBegin: PdfDictionary dictionary = new PdfDictionary(); while (true) { // Key. MoveNext(); if (tokenType == TokenTypeEnum.DictionaryEnd) { break; } PdfName key = (PdfName)ParsePdfObject(); // Value. MoveNext(); PdfDirectObject value = (PdfDirectObject)ParsePdfObject(); // Add the current entry to the dictionary! dictionary[key] = value; } int oldOffset = (int)stream.Position; MoveNext(); // Is this dictionary the header of a stream object [PDF:1.6:3.2.7]? if ((tokenType == TokenTypeEnum.Keyword) && token.Equals(Keyword.BeginStream)) { // Keep track of current position! long position = stream.Position; // Get the stream length! /* * NOTE: Indirect reference resolution is an outbound call (stream pointer hazard!), * so we need to recover our current position after it returns. */ int length = ((PdfInteger)files.File.Resolve(dictionary[PdfName.Length])).RawValue; // Move to the stream data beginning! stream.Seek(position); SkipEOL(); // Copy the stream data to the instance! byte[] data = new byte[length]; stream.Read(data); MoveNext(); // Postcondition (last token should be 'endstream' keyword). Object streamType = dictionary[PdfName.Type]; if (PdfName.ObjStm.Equals(streamType)) // Object stream [PDF:1.6:3.4.6]. { return(new ObjectStream( dictionary, new bytes.Buffer(data), file )); } else if (PdfName.XRef.Equals(streamType)) // Cross-reference stream [PDF:1.6:3.4.7]. { return(new XRefStream( dictionary, new bytes.Buffer(data), file )); } else // Generic stream. { return(new PdfStream( dictionary, new bytes.Buffer(data) )); } } else // Stand-alone dictionary. { stream.Seek(oldOffset); // Restores postcondition (last token should be the dictionary end). return(dictionary); } case TokenTypeEnum.ArrayBegin: PdfArray array = new PdfArray(); while (true) { // Value. MoveNext(); if (tokenType == TokenTypeEnum.ArrayEnd) { break; } // Add the current item to the array! array.Add((PdfDirectObject)ParsePdfObject()); } return(array); case TokenTypeEnum.Real: return(new PdfReal((float)token)); case TokenTypeEnum.Boolean: return(PdfBoolean.Get((bool)token)); case TokenTypeEnum.Date: return(new PdfDate((DateTime)token)); case TokenTypeEnum.Hex: return(new PdfTextString( (string)token, PdfString.SerializationModeEnum.Hex )); case TokenTypeEnum.Null: return(null); case TokenTypeEnum.Comment: // NOOP: Comments are simply ignored and skipped. break; default: throw new Exception("Unknown type: " + tokenType); } } while(MoveNext()); return(null); }
public static PdfBoolean NextPdfBoolean(this Random rnd) { return(PdfBoolean.Get(rnd.NextBoolean())); }