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

1 Answer 1

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());
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your reply ,print document would only take file to print ,we can not send raw text to printer .
i need to send string value to printer ,instead of file like image or document
I added code to illustrate the answer :D Is this the answer you waiting for?
my exact requirement is defined in this question stackoverflow.com/questions/7008575/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.