Printing text directly to a printer (NOT as a graphic)

I can print text if i do a
e.Graphics.DrawString(......)

My problem is that I am trying to print to a card printer, and usually it checks what text it is printing, and if it hits a '~' it then encodes the magnetic stripe.
When i print from Microsoft word, or Access, it works fine because text is sent to the printer.
But using "PrintDocument" object and drawing to a bitmap, it is unable to parse the text.

Any ideas on how to print text directly to the printer at locations, instead of rendering them onto a bitmap first


Answer this question

Printing text directly to a printer (NOT as a graphic)

  • RenX99

    Hi,

    Since the .NET Framework cannot send preformatted data to a printer directly, you may need to use the Win32 spooler API functions to send raw data to a printer.

    I suggest you can refer to the following MSDN KB article for the detailed information and code sample:

    How to send raw data to a printer by using Visual C# .NET
    http://support.microsoft.com/ kbid=322091


    Thanks!

    Best Regards!

    Gary Chang



  • Jerry Koh

    I've tried using the WebBrowser.Print mechanism. I encountered a problem.
    If my application exits immediatly after invoking print, nothing gets sent to the printer.
    Anyone have any ideas on how to safely (reliably) resolve this
    There is no "PrintDone" event associated with the WebBrowser object and the IsBusy mechanism is only used when the browser object is loading.

    I've submitted this as a problem to microsoft, its been over a week with no feedback on the topic.
    It really pains me that I might have to do my own rendering using GDI+ when I can simply use XSL/XML to create my print items.


  • Aaron_Marten

    You can use the winspool.drv to print directly to your printer, here is a little article about it: Printing Directly to the Printer


  • MPE

    I actually figured out an easier way to do this:

    I write out what i need to a text file (formatted HTML in this case), and i have a "WebBrowser" control embedded in the form.

    I make the webBrowser control go to C:\thefile.html then do a WebBrowser1.print() because it is all sent to the printer as text, it works well.
    Now i just need to figure out how to setup the margins and header/footer in code without having to resort to opening up the pagesetup dialog...........


  • Jeff Parker

    Ice Romeo, could I get my hands on that class as well
  • David8756

    if u give me ur email address i will send u a class that me and my teacher complied so that u can print just aout anything from C#. we had to gather tons of info to make this. as long as u understanf the basic u could use it i can alos send u a program sample of how to use the printer classes we designed as well.


  • Jeff Barker

    // ICE ROMEO

    // ibknowbodi@yahoo.com

    // CLASS FOR PRINTING MADE EASY

    using System;

    using System.Windows.Forms; // required for Form class

    using System.Drawing; // required for Bitmap class

    using System.Drawing.Printing; // required for printPreviewDocument class

    namespace PrintFormToPrinter

    {

    /// <summary>

    /// FormPrinter is a class that I created to give

    /// users a simple way to print forms. The FormPrinter

    /// class receives a form in its constructor and

    /// prints that form to the default printer (which

    /// can be changed). This code is based on code found

    /// in the .NET help system.

    /// Rick Bird, DeVry University, June 6, 2004

    /// </summary>

    public class FormPrinter

    {

    // attributes

    private Form formToPrint;

    // create necessary printing objects

    PrintDocument printDoc = new PrintDocument(); // required PrintDocument object

    PrintDialog printDialog = new PrintDialog(); // required PrintDialog object

    PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog(); // required PrintPreviewDialog object

    // bring in the BitBlt windows API method and necessary references

    [System.Runtime.InteropServices.DllImport("gdi32.dll")]

    public static extern long BitBlt (IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);

    private Bitmap memoryImage;

    // constructors

    public FormPrinter()

    {

    formToPrint = null;

    // create an PrintPage event for the printPreviewDoc object

    this.printDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDoc_PrintPage);

    }

    public FormPrinter( Form formToPrint )

    {

    this.formToPrint = formToPrint;

    // create an PrintPage event for the printPreviewDoc object

    this.printDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDoc_PrintPage);

    }

    // behaviors

    public void printWithDialog()

    {

    bool formWasOpened = false;

    // set the printer defaults

    bool printerSet = setPrinterDefaults();

    printPreviewDialog.Document = printDoc;

    // check to see if the form is open

    if(formToPrint.Visible == false)

    {

    formToPrint.Show();

    formWasOpened = true;

    }

    formToPrint.Refresh();

    // print the form unless user hits cancel

    if(printerSet == true)

    {

    captureScreen();

    // use printPreviewDialog.ShowDialog() to show a preview dialog of form

    //printPreviewDialog.ShowDialog();

    // use printDoc.Print() to print immediately after choosing printer

    printDoc.Print();

    }

    // if we had to open the form, close it now

    if(formWasOpened == true)

    {

    formToPrint.Dispose();

    }

    }

    public void printWithOutDialog( Form formToPrint )

    {

    // set form to object level

    this.formToPrint = formToPrint;

    bool formWasOpened = false;

    // set the printer defaults

    printPreviewDialog.Document = printDoc;

    // check to see if the form is open

    if(formToPrint.Visible == false)

    {

    formToPrint.Show();

    formWasOpened = true;

    }

    formToPrint.Refresh();

    formToPrint.Refresh();

    // print the form unless user hits cancel

    captureScreen();

    // use printPreviewDialog.ShowDialog() to show a preview dialog of form

    //printPreviewDialog.ShowDialog();

    // use printDoc.Print() to print immediately after choosing printer

    printDoc.Print();

    // if we had to open the form, close it now

    if(formWasOpened == true)

    {

    formToPrint.Dispose();

    }

    }

    private bool setPrinterDefaults()

    {

    printDialog.Document = printDoc;

    DialogResult dialogRslt = printDialog.ShowDialog();

    if(dialogRslt == DialogResult.OK)

    {

    printDoc.PrinterSettings = printDialog.PrinterSettings;

    // set printer to high resolution

    printDoc.DefaultPageSettings.PrinterResolution =

    printDoc.PrinterSettings.PrinterResolutions[0]; // 0=high, 1=med, 2=low, 3=draft (may change depending on your printer)

    return true;

    }

    return false; // return false if user cancels printer dialog

    }

    private void captureScreen()

    {

    // get the current screen

    Graphics mygraphics = formToPrint.CreateGraphics();

    Size s = formToPrint.Size;

    memoryImage = new Bitmap(s.Width, s.Height, mygraphics);

    Graphics memoryGraphics = Graphics.FromImage(memoryImage);

    IntPtr dc1 = mygraphics.GetHdc();

    IntPtr dc2 = memoryGraphics.GetHdc();

    BitBlt(dc2, 0, 0, formToPrint.ClientRectangle.Width, formToPrint.ClientRectangle.Height, dc1, 0, 0, 13369376);

    mygraphics.ReleaseHdc(dc1);

    memoryGraphics.ReleaseHdc(dc2);

    }

    private void printDoc_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)

    {

    // this event fires when page is printing

    e.Graphics.DrawImage(memoryImage, 0, 0);

    }

    // accessors and modifiers

    public Form getFormToPrint()

    {

    return formToPrint;

    }

    public void setFormToPrint( Form formToPrint )

    {

    this.formToPrint = formToPrint;

    }

    }

    }


  • Printing text directly to a printer (NOT as a graphic)