0

目前我正在使用 silverlight 4,我们正在将基于 windows 的应用程序转换为 silverlight。在基于 Windows 的应用程序中使用 COM 打印字符串很容易,但是像 Silverlight 这样的客户端应用程序是否可以打印字符串值?

我必须将其转换为 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

编辑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;
    }

参考:(这是我的确切要求)-从 Web 打印“原始文本”(ZPL)

4

1 回答 1

0

您可以使用PrintDocument.Print方法并在 PageVisual 中发送带有文本块的网格?

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;
}

您可以致电:

SendStringToPrinter(*******.ToString());
于 2013-06-03T12:10:48.017 回答