/// <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> /// 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; }