Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I want to send a byte array to printer containing arabic and english letters and print it through printer.

I am using a text file containing arabic and english letters. I am providing my code below:

C#
private void button1_Click(object sender, EventArgs e)
    {
        string content = "";
        using (StreamReader reader = new StreamReader(@"E:\arabic.txt", System.Text.Encoding.UTF32, true))
        {
            content = reader.ReadToEnd();
        }
        byte[] b1 = System.Text.Encoding.UTF32.GetBytes(content);//Encoding.UTF8.GetBytes(content);
        printdoc(b1);

    }
    public void printdoc(byte[] document)
    {

        string printer = "GP-U80300 Series";

        ////A pointer to a value that receives the number of bytes of data that were written to the printer.
        int pcWritten = 0;
        IntPtr pUnmanagedBytes = new IntPtr(0);
        pcWritten = document.Length;
        pUnmanagedBytes = Marshal.AllocCoTaskMem(pcWritten);
        // Copy the managed byte array into the unmanaged array.
        Marshal.Copy(document, 0, pUnmanagedBytes, pcWritten);
        PrintDirect.SendBytesToPrinter(printer, pUnmanagedBytes, pcWritten);


    }


I am using a PrintDirect class which is mentioned below (PrintDirect.cs) :

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows;
using System.IO; 

public class PrintDirect
{

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public class DOCINFOA
    {
        [MarshalAs(UnmanagedType.LPStr)]
        public string pDocName;
        [MarshalAs(UnmanagedType.LPStr)]
        public string pOutputFile;
        [MarshalAs(UnmanagedType.LPStr)]
        public string pDataType;
    }
    [DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);

    [DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool ClosePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);

    [DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool EndDocPrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool StartPagePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool EndPagePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten);

    // SendBytesToPrinter()
    // When the function is given a printer name and an unmanaged array
    // of bytes, the function sends those bytes to the print queue.
    // Returns true on success, false on failure.
    public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
    {
        Int32 dwError = 0, dwWritten = 0;
        IntPtr hPrinter = new IntPtr(0);
        DOCINFOA di = new DOCINFOA();
        bool bSuccess = false; // Assume failure unless you specifically succeed.

        di.pDocName = "My C#.NET RAW Document";
        di.pDataType = "RAW";

        // Open the printer.
        if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
        {
            // Start a document.
            if (StartDocPrinter(hPrinter, 1, di))
            {
                // Start a page.
                if (StartPagePrinter(hPrinter))
                {
                    // Write your bytes.
                    bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
                    EndPagePrinter(hPrinter);
                }
                EndDocPrinter(hPrinter);
            }
            ClosePrinter(hPrinter);
        }
        // If you did not succeed, GetLastError may give more information
        // about why not.
        if (bSuccess == false)
        {
            dwError = Marshal.GetLastWin32Error();
        }
        return bSuccess;
    }

    public static bool SendFileToPrinter(string szPrinterName, string szFileName)
    {
        // Open the file.
        FileStream fs = new FileStream(szFileName, FileMode.Open);
        // Create a BinaryReader on the file.
        BinaryReader br = new BinaryReader(fs);
        // Dim an array of bytes big enough to hold the file's contents.
        Byte[] bytes = new Byte[fs.Length];
        bool bSuccess = false;
        // Your unmanaged pointer.
        IntPtr pUnmanagedBytes = new IntPtr(0);
        int nLength;

        nLength = Convert.ToInt32(fs.Length);
        // Read the contents of the file into the array.
        bytes = br.ReadBytes(nLength);
        // Allocate some unmanaged memory for those bytes.
        pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
        // Copy the managed byte array into the unmanaged array.
        Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
        // Send the unmanaged bytes to the printer.
        bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength);
        // Free the unmanaged memory that you allocated earlier.
        Marshal.FreeCoTaskMem(pUnmanagedBytes);
        return bSuccess;
    }
    public static bool SendStringToPrinter(string szPrinterName, string szString)
    {
        IntPtr pBytes;
        Int32 dwCount;
        // How many characters are in the string?
        dwCount = szString.Length;
        // Assume that the printer is expecting ANSI text, and then convert
        // the string to ANSI text.
        pBytes = Marshal.StringToCoTaskMemAnsi(szString);
        // Send the converted ANSI string to the printer.
        SendBytesToPrinter(szPrinterName, pBytes, dwCount);
        Marshal.FreeCoTaskMem(pBytes);
        return true;
    }

}


But I am not getting the exact result, english letters are ok but arabic letters are not showing as it looks like, so please provide a solution if possible.
Posted
Comments
Sergey Alexandrovich Kryukov 24-Apr-15 9:52am    
What happens to Arabic letters?
—SA
Member 11557291 24-Apr-15 10:00am    
it changes to ??? when encoded in ASCII, for UTF32 it gives mix of specialcharacter and alphabet
Sergey Alexandrovich Kryukov 24-Apr-15 10:04am    
Of course. How else? How ASCII can support wider Unicode? :-)
—SA
virusstorm 24-Apr-15 9:54am    
What kind of printer are you using? The printer itself may not be able to handle the raw Arabic characters. In which case, you will most likely need to generate either a file that is rendered into PostScript or generate PostScript data so the printer knows how to work with the Arabic characters.
Member 11557291 24-Apr-15 10:04am    
GP-U80300i theraml printer, it prints the text file when I am using graphics.DrawString() to print, but when I send byte-array then problem occurs

1 solution

What you do could not work in principle — please see my comments to the question and the link. Instead of doing this very questionable stuff, you can use Microsoft.PointOfService.PosPrinter class which supports Unicode:
https://msdn.microsoft.com/en-us/library/dd125967%28v=winembedded.10%29.aspx[^],
https://msdn.microsoft.com/en-us/library/dd125593%28v=winembedded.10%29.aspx[^].

—SA
 
Share this answer
 
Comments
Member 11557291 27-Apr-15 7:31am    
Sir,
Its not working unicode also printing in an unknown format, the arabic letters not printed as they are.

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