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