0

Currently i am working with silverlight 4, we are converting windows based application into silverlight. In windows based application to print string is easy using COM, but client side application like Silverlight is it possible to print string value ?

I have to convert this to Silverlight :

Dim PD As New PrintDialog
PD.UseEXDialog = True
PD.PrinterSettings = New System.Drawing.Printing.PrinterSettings
If (Windows.Forms.DialogResult.OK = PD.ShowDialog()) Then
    ZPLZebra.RawPrinterHelper.SendStringToPrinter(PD.PrinterSettings.PrinterName, *******.ToString())
End If

Edit 1:

     public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, int dwCount)
    {
        IntPtr hPrinter = new IntPtr(0);
        DOCINFOA di = new DOCINFOA();
        bool flag = false;
        di.pDocName = "My C#.NET RAW Document";
        di.pDataType = "RAW";
        if (OpenPrinter(szPrinterName, out hPrinter, 0L))
        {
            if (StartDocPrinter(hPrinter, 1, di))
            {
                if (StartPagePrinter(hPrinter))
                {
                    int dwWritten = 0;
                    flag = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
                    EndPagePrinter(hPrinter);
                }
                EndDocPrinter(hPrinter);
            }
            ClosePrinter(hPrinter);
        }
        if (!flag)
        {
            Marshal.GetLastWin32Error();
        }
        return flag;
    }

    public static bool SendStringToPrinter(string szPrinterName, string szString)
    {
        int length = szString.Length;
        IntPtr pBytes = Marshal.StringToCoTaskMemAnsi(szString);
        SendBytesToPrinter(szPrinterName, pBytes, length);
        Marshal.FreeCoTaskMem(pBytes);
        return true;
    }

Ref:(this is my exact requirement)- Printing "raw text" (ZPL) from Web

Community
  • 1
  • 1
Prabu
  • 1
  • 2

1 Answers1

0

You can use PrintDocument.Print method and in PageVisual send a grid with text block ?

Sample code in C#

/// <summary>
/// Text to print.
/// </summary>
private string _textToPrint;

/// <summary>
/// Method that print text.
/// </summary>
/// <param name="textToPrint">Text to print</param>
/// <param name="documentName">Optional document name.</param>
public void SendStringToPrinter(string textToPrint, string documentName = "My Document")
{
    // Set print content in private property
    _textToPrint = textToPrint;
    // Create a new print dialog
    PrintDocument pd = new PrintDocument();
    // Catch print event
    pd.PrintPage += onPrintPage;
    // Start print request
    pd.Print(documentName);
}
/// <summary>
/// Called during printing.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void onPrintPage(object sender, PrintPageEventArgs e)
{
    // Create a UI element (simple grid with textblock inside).
    var grid = new Grid();
    grid.Children.Add(new TextBlock()
    {
        Text = _textToPrint
    });
    // Add the grid to print visual.
    e.PageVisual = grid;
}

You can call :

SendStringToPrinter(*******.ToString());
Tonio
  • 743
  • 1
  • 4
  • 18
  • Thanks for your reply ,print document would only take file to print ,we can not send raw text to printer . – Prabu Jun 04 '13 at 04:30
  • i need to send string value to printer ,instead of file like image or document – Prabu Jun 04 '13 at 04:31
  • I added code to illustrate the answer :D Is this the answer you waiting for? – Tonio Jun 04 '13 at 12:16
  • my exact requirement is defined in this question http://stackoverflow.com/questions/7008575/printing-raw-text-zpl-from-web – Prabu Jun 05 '13 at 05:57