Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
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 http://support.microsoft.com/kb/322091[^] 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.
 
Regards,
Puneet Sharma
Posted 24-Feb-13 20:51pm

1 solution

Rate this: bad
good
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:
http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.aspx[^].
 
You will find a simple code sample in this article.
 
—SA
  Permalink  
Comments
HappySharma at 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.
 
Regards,
Puneet Sharma
Sergey Alexandrovich Kryukov at 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).
 
—SA
HappySharma at 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 time...now 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
 
---PS
Sergey Alexandrovich Kryukov at 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?
—SA
HappySharma at 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...
 
-PS
Sergey Alexandrovich Kryukov at 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:
 
http://en.wikipedia.org/wiki/IText
http://itextpdf.com/
http://sourceforge.net/projects/itextsharp/
http://www.codeproject.com/Articles/12445/Converting-PDF-to-Text-in-C
http://www.sautinsoft.com/products/pdf-focus/index.php
http://www.codeproject.com/Articles/32274/How-To-Convert-PDF-to-Image-Using-Ghostscript-API
http://www.codeproject.com/Answers/288152/Convert-pdf-doc-and-excel-file-to-jpg-in-webform-C.aspx#answer1
...
 
—SA
 

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 385
1 DamithSL 210
2 Kornfeld Eliyahu Peter 210
3 OriginalGriff 180
4 Peter Leow 95
0 OriginalGriff 7,215
1 DamithSL 5,199
2 Maciej Los 4,866
3 Sergey Alexandrovich Kryukov 4,757
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web04 | 2.8.141223.1 | Last Updated 25 Feb 2013
Copyright © CodeProject, 1999-2014
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