Click here to Skip to main content
13,052,956 members (73,226 online)
Rate this:
Please Sign up or sign in to 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
Posted 24-Feb-13 19:51pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.

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...

Sergey Alexandrovich Kryukov 25-Feb-13 13:46pm
The consideration about "misused" it itself a great abuse. You have a flawed busyness model or at least architecture.
You need to convert PDF to a printable image or text by parsing it. And then follow the way I explained. To work with PDF, please see:


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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 25 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100