/// <summary> /// Write PDF document object summary to byte array /// </summary> /// <returns>Text string</returns> public static string PdfFileSummary ( PdfReader Reader ) { OutputCtrl Ctrl = new OutputCtrl(); Ctrl.AppendMessage(string.Format("PDF file name: {0}", Reader.SafeFileName)); Ctrl.AddEol(); Ctrl.AppendMessage("Trailer Dictionary"); Ctrl.AppendMessage("------------------"); Reader.TrailerDict.ToByteArray(Ctrl); Ctrl.AddEol(); Ctrl.AddEol(); Ctrl.AppendMessage("Indirect Objects"); Ctrl.AppendMessage("----------------"); // output one object at a time foreach (PdfIndirectObject Obj in Reader.ObjectArray) { if (Obj != null) { Obj.ObjectSummary(Ctrl); } } // successful exit return(ByteArrayToString(Ctrl.ToArray())); }
/// <summary> /// Create PDF file's object summary /// </summary> /// <returns>ASCII byte array</returns> public static string ObjectSummary ( PdfIndirectObject ReaderObject ) { OutputCtrl Ctrl = new OutputCtrl(); ReaderObject.ObjectSummary(Ctrl); return(ByteArrayToString(Ctrl.ToArray())); }
/// <summary> /// Page contents to text /// </summary> /// <param name="OpArray">Op array</param> /// <returns>Byte array</returns> public static byte[] ContentsToText ( PdfOp[] OpArray ) { OutputCtrl Ctrl = new OutputCtrl(); // output one operator at a time foreach (PdfOp Op in OpArray) { Op.ToByteArray(Ctrl); } // successful exit return(Ctrl.ToArray()); }
/// <summary> /// append object to byte array /// </summary> /// <param name="Ctrl">Output Control</param> public virtual void ToByteArray ( OutputCtrl Ctrl ) { // test for long line Ctrl.TestEol(); // dictionary if (GetType() == typeof(PdfDictionary)) { Ctrl.Add('<'); Ctrl.Add('<'); foreach (PdfKeyValue KeyValue in ((PdfDictionary)this).KeyValueArray) { Ctrl.AppendText(KeyValue.Key); KeyValue.Value.ToByteArray(Ctrl); } Ctrl.Add('>'); Ctrl.Add('>'); } // array else if (GetType() == typeof(PdfArray)) { Ctrl.Add('['); foreach (PdfBase ArrayItem in ((PdfArray)this).Items) { ArrayItem.ToByteArray(Ctrl); } Ctrl.Add(']'); } // PDF string else if (GetType() == typeof(PdfString)) { // convert PDF string to visual display format PdfStringToDisplay(Ctrl, ((PdfString)this).StrValue); } // get text from simple objects else { Ctrl.AppendText(ToString()); } return; }
/// <summary> /// convert PDF string to PDF file format /// </summary> /// <param name="Ctrl">Output Control</param> /// <param name="StrByteArray">PDF string</param> public void PdfStringToPdfFile ( OutputCtrl Ctrl, byte[] StrByteArray ) { // create output string with open and closing parenthesis Ctrl.Add('('); // move string to output if (StrByteArray != null) { foreach (byte TestByte in StrByteArray) { Ctrl.TestEscEol(); // CR and NL must be replaced by \r and \n // Otherwise PDF readers will convert CR or NL or CR-NL to NL if (TestByte == '\r') { Ctrl.Add('\\'); Ctrl.Add('r'); } else if (TestByte == '\n') { Ctrl.Add('\\'); Ctrl.Add('n'); } else { // the three characters \ ( ) must be preceded by \ if (TestByte == (byte)'\\' || TestByte == (byte)'(' || TestByte == (byte)')') { Ctrl.Add('\\'); } Ctrl.Add(TestByte); } } } // final closing parentesis Ctrl.Add(')'); return; }
/// <summary> /// append object to byte array /// </summary> /// <param name="Ctrl">Output control</param> /// <param name="StrByteArray">PDF string</param> public void PdfStringToDisplay ( OutputCtrl Ctrl, byte[] StrByteArray ) { // test for printable characters int Printable = 0; foreach (byte TestByte in StrByteArray) { if (TestByte >= ' ' && TestByte <= '~') { Printable++; } } // mostly printable if (10 * Printable >= 9 * StrByteArray.Length) { // create output string with open and closing parenthesis Ctrl.Add('('); // move string to output foreach (byte TestByte in StrByteArray) { Ctrl.TestEscEol(); // CR and NL must be replaced by \r and \n // Otherwise PDF readers will convert CR or NL or CR-NL to NL if (TestByte == '\r') { Ctrl.Add('\\'); Ctrl.Add('r'); } else if (TestByte == '\n') { Ctrl.Add('\\'); Ctrl.Add('n'); } else if (TestByte < ' ' || TestByte > '~') { Ctrl.Add('\\'); Ctrl.Add('x'); string Hex = string.Format("{0:x2}", TestByte); Ctrl.Add(Hex[0]); Ctrl.Add(Hex[1]); } else { // the three characters \ ( ) must be preceded by \ if (TestByte == (byte)'\\' || TestByte == (byte)'(' || TestByte == (byte)')') { Ctrl.Add('\\'); } Ctrl.Add(TestByte); } } // final closing parentesis Ctrl.Add(')'); return; } // mostly unprintable Ctrl.Add('<'); // move string to output foreach (byte TestByte in StrByteArray) { Ctrl.TestEol(); string Hex = string.Format("{0:x2}", TestByte); Ctrl.Add(Hex[0]); Ctrl.Add(Hex[1]); } // final closing parentesis Ctrl.Add('>'); return; }
//////////////////////////////////////////////////////////////////// // Write indirect object to object analysis file //////////////////////////////////////////////////////////////////// internal void ObjectSummary ( OutputCtrl Ctrl ) { // write object header Ctrl.AppendMessage(string.Format("Object number: {0}", ObjectNumber)); Ctrl.AppendMessage(string.Format("Object Value Type: {0}", ObjectDescription())); Ctrl.AppendMessage(string.Format("File Position: {0} Hex: {0:X}", FilePosition)); if (ParentObjectNo != 0) { Ctrl.AppendMessage(string.Format("Parent object number: {0}", ParentObjectNo)); Ctrl.AppendMessage(string.Format("Parent object index: {0}", ParentObjectIndex)); } if (ObjectType == ObjectType.Stream) { Ctrl.AppendMessage(string.Format("Stream Position: {0} Hex: {0:X}", StreamFilePosition)); Ctrl.AppendMessage(string.Format("Stream Length: {0} Hex: {0:X}", StreamLength)); } // dictionary or stream if (ObjectType == ObjectType.Dictionary || ObjectType == ObjectType.Stream) { string ObjectTypeStr = Dictionary.FindValue("/Type").ToName; if (ObjectTypeStr == null) { ObjectTypeStr = PdfObjectType; } if (ObjectTypeStr != null) { Ctrl.AppendMessage(string.Format("Object Type: {0}", ObjectTypeStr)); } string ObjectSubtypeStr = Dictionary.FindValue("/Subtype").ToName; if (ObjectSubtypeStr != null) { Ctrl.AppendMessage(string.Format("Object Subtype: {0}", ObjectSubtypeStr)); } // write to pdf file Dictionary.ToByteArray(Ctrl); // final terminator Ctrl.AddEol(); } // object has contents that is not stream else if (ObjectType == ObjectType.Other) { // write content to pdf file Value.ToByteArray(Ctrl); // final terminator Ctrl.AddEol(); } // final terminator Ctrl.AddEol(); // return string return; }