Click here to Skip to main content
15,617,231 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear All,

I am stuck in a problem in C# where I want to print a byte array directly to a printer. Means instated of print a file I want to print a byte array. I have gone to a solution[^] but didn't get any help.
With the help of this code I can send bytes to printer but printer gives me error as document can't be printed.

Thanks in advance.

Puneet Sharma

1 solution

Resist the temptation of sending raw data.

Print your string in graphic mode, which will give you guaranteed universal printer-independent solution. All you need it to use the class System.Drawing.Printing.PrintDocument:[^].

You will find a simple code sample in this article.

Share this answer
HappySharma 25-Feb-13 2:56am    
Hi Sergery,

Thanks for your quick reply. The link you provide working fine on txt files. But when we come up with pdf or image files, then it fumbles and dosn't print correctly.
Actually I need to print a byte array of pdf files.

Puneet Sharma
Sergey Alexandrovich Kryukov 25-Feb-13 11:46am    
You asked only about array of bytes without explanation how you want to present it, so I assumed text. Please accept this answer formally (green button).

It will work on image files, use Graphics.DrawImage. PDF? There is no such thing in .NET. Use, some PDF software to render PDF graphically (search CodeProject, there is enough information).

HappySharma 25-Feb-13 12:11pm    
Hi SA,
As you asked to me I pressed the green button...You wrote that you have also read my previous question where I put all my concerns that I need to print bytes of pdf files...any way thank you very much for giving me your valuable I am trying to get some stuffs related to converting pdf to image type without saving it on the disk...
One more thing I want to ask...You wrote "Resist the temptation of sending raw data." in your answer...Is it not a good practice to send raw data on printer...If it is so then
please tell me why

Sergey Alexandrovich Kryukov 25-Feb-13 12:18pm    
It is certainly not a good practice. You have two ways: one is only for certain type of printer, another is for any printers. Which one would you prefer? The answer is obvious.
If you don't understand the way I explained, ask about it, but first: what is in your byte array and how you want to present it?
HappySharma 25-Feb-13 12:35pm    
As I explained in my previous question, I have an encrypted pdf that resides on server. This file may have images, texts...I need to download it, decrypt it, then edit it and then print it...But I am not supposed to make any soft copy of the file on the client machine...because it can be misused...I have done all the above three tasks (Download, Decrypt, Edit) in byte array only...So the only option I have is to print byte array to the printer...Please explain if you have any better idea...


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900