locked
printing byte array RRS feed

  • Question

  • Hi,

    I want to perform two actions with a byte array:

    1) write it to a file

    2) print it on a printer

    Results:

    1) Writing to a file is succesfull using "File.WriteAllBytes("c://temp//test.pdf", pByteArray);". The file is stored correctly and readable.

    2) To print the same information, I've used the 'PrintQueue'. I can see the job appears in the queue, but nothing comes out of the printer.

    Sample code:

                LocalPrintServer srv = new LocalPrintServer(PrintSystemDesiredAccess.AdministrateServer);


                // Get the printqueue
                PrintQueue printQueue = srv.GetPrintQueue(PrinterPath);  // PrinterPath = windows printer name
                if (printQueue == null)
                {
                    throw new Exception(String.Format("PrintQueue for printer {0} not found.", PrinterPath));
                } 

                // Call AddJob
                PrintSystemJobInfo myPrintJob = printQueue.AddJob("Test Print Job");

                // Write a Byte buffer to the JobStream and close the stream
                Stream myStream = myPrintJob.JobStream;
                myStream.Write(pByteArray, 0, pByteArray.Length);
                myStream.Close();

    For information: I did a test that reads the written (pdf) file into a byte array, and if I use that array to print => this works correct...

    What can cause this?

    Best regards,

    Stijn.

    Saturday, October 20, 2012 4:38 PM

Answers

  • A byte[] array is binary so you can't print the results directly.  See code below for solution

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    namespace ConsoleApplication1
    {
        class Program
        {
            
            static void Main(string[] args)
            {
                byte[] mybytes = new byte[256];
                for (int index = 0; index < 256; index++)
                {
                    mybytes[index] = (byte)(index & 0xff) ;
                }
                string stringbytes = BytesToString(mybytes);
            }
            static string BytesToString(byte[] mybytes)
            {
                string[] HexChar = new string[]{"0","1","2","3","4","5","6","7",
                    "8","9","A","B","C","D","E","F"};
                string results = "";
                for (int index = 0; index < mybytes.Count(); index++)
                {
                    results += HexChar[(mybytes[index] >> 4) & 0xf];
                    results += HexChar[(mybytes[index] & 0xf)];
                }
                return results;
            }
        }
    }


    jdweng

    • Proposed as answer by Mike Feng Wednesday, October 24, 2012 12:48 PM
    • Marked as answer by Mike Feng Tuesday, October 30, 2012 10:57 AM
    Saturday, October 20, 2012 9:37 PM

All replies

  • A byte[] array is binary so you can't print the results directly.  See code below for solution

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    namespace ConsoleApplication1
    {
        class Program
        {
            
            static void Main(string[] args)
            {
                byte[] mybytes = new byte[256];
                for (int index = 0; index < 256; index++)
                {
                    mybytes[index] = (byte)(index & 0xff) ;
                }
                string stringbytes = BytesToString(mybytes);
            }
            static string BytesToString(byte[] mybytes)
            {
                string[] HexChar = new string[]{"0","1","2","3","4","5","6","7",
                    "8","9","A","B","C","D","E","F"};
                string results = "";
                for (int index = 0; index < mybytes.Count(); index++)
                {
                    results += HexChar[(mybytes[index] >> 4) & 0xf];
                    results += HexChar[(mybytes[index] & 0xf)];
                }
                return results;
            }
        }
    }


    jdweng

    • Proposed as answer by Mike Feng Wednesday, October 24, 2012 12:48 PM
    • Marked as answer by Mike Feng Tuesday, October 30, 2012 10:57 AM
    Saturday, October 20, 2012 9:37 PM
  • Hi Stijn,

    How about Joel's code?

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, October 24, 2012 12:49 PM
  • The easiest way to print a pdf file is to shell it with the print verb.  It will print with the program associated with the pdf extension, usually Adobe Acrobat.  If you must print the byte array, use a 3rd party library.
    Wednesday, October 24, 2012 2:01 PM