目前我正在使用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;
}
参考:(这是我的确切要求) - Printing "raw text" (ZPL) from Web
答案 0 :(得分: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());