【发布时间】:2017-02-03 05:05:41
【问题描述】:
我正在使用带有 Winforms 的 C# 创建应用程序,现在我需要在热敏打印机上打印销售收据。为此,我正在创建一个文本文件并使用PrintDocument 读取它以进行打印,但我不能这样做,因为我不知道如何配置纸张大小、对齐纸张上的文本中心以及其他配置。当我打印时,文本文件被打印出来了,但是很乱,并且在打印结束之后,纸张并没有停止。
我该怎么做?
正在尝试。
private PrintDocument printDocument = new PrintDocument();
private static String RECEIPT = Environment.CurrentDirectory + @"\comprovantes\comprovante.txt";
private String stringToPrint = "";
private void button1_Click(object sender, EventArgs e)
{
generateReceipt();
printReceipt();
}
private void generateReceipt()
{
FileStream fs = new FileStream(COMPROVANTE, FileMode.Create);
StreamWriter writer = new StreamWriter(fs);
writer.WriteLine("==========================================");
writer.WriteLine(" NOME DA EMPRESA AQUI ");
writer.WriteLine("==========================================");
writer.Close();
fs.Close();
}
private void printReceipt()
{
FileStream fs = new FileStream(COMPROVANTE, FileMode.Open);
StreamReader sr = new StreamReader(fs);
stringToPrint = sr.ReadToEnd();
printDocument.PrinterSettings.PrinterName = DefaultPrinter.GetDefaultPrinterName();
printDocument.PrintPage += new PrintPageEventHandler(printPage);
printDocument.Print();
sr.Close();
fs.Close();
}
private void printPage(object sender, PrintPageEventArgs e)
{
int charactersOnPage = 0;
int linesPerPage = 0;
Graphics graphics = e.Graphics;
// Sets the value of charactersOnPage to the number of characters
// of stringToPrint that will fit within the bounds of the page.
graphics.MeasureString(stringToPrint, this.Font,
e.MarginBounds.Size, StringFormat.GenericTypographic,
out charactersOnPage, out linesPerPage);
// Draws the string within the bounds of the page
graphics.DrawString(stringToPrint, this.Font, Brushes.Black,
e.MarginBounds, StringFormat.GenericTypographic);
// Remove the portion of the string that has been printed.
stringToPrint = stringToPrint.Substring(charactersOnPage);
// Check to see if more pages are to be printed.
e.HasMorePages = (stringToPrint.Length > 0);
}
我正在尝试创建这种收据模型。
使用 RDLC,但开始打印很慢。我关注这个example
//works but slow
private void Run() {
LocalReport report = new LocalReport();
report.ReportPath = @"..\..\reports\ReciboDeVenda.rdlc";
Export(report);
Print();
}
private void Export(LocalReport report) {
string deviceInfo =
@"<DeviceInfo>
<OutputFormat>EMF</OutputFormat>
<PageWidth>8.5in</PageWidth>
<PageHeight>11in</PageHeight>
<MarginTop>0.25in</MarginTop>
<MarginLeft>0.25in</MarginLeft>
<MarginRight>0.25in</MarginRight>
<MarginBottom>0.25in</MarginBottom>
</DeviceInfo>";
Warning[] warnings;
m_streams = new List<Stream>();
report.Render("Image", deviceInfo, CreateStream,
out warnings);
foreach (Stream stream in m_streams)
stream.Position = 0;
}
private Stream CreateStream(string name, string fileNameExtension,
Encoding encoding, string mimeType, bool willSeek) {
Stream stream = new MemoryStream();
m_streams.Add(stream);
return stream;
}
private void Print() {
if (m_streams == null || m_streams.Count == 0)
throw new Exception("Error: no stream to print.");
PrintDocument printDoc = new PrintDocument();
printDoc.PrinterSettings.PrinterName = DefaultPrinter.GetDefaultPrinterName();
if (!printDoc.PrinterSettings.IsValid) {
throw new Exception("Error: cannot find the default printer.");
}else {
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
m_currentPageIndex = 0;
printDoc.Print();
}
}
private void PrintPage(object sender, PrintPageEventArgs ev) {
Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);
// Adjust rectangular area with printer margins.
Rectangle adjustedRect = new Rectangle(
ev.PageBounds.Left - (int)ev.PageSettings.HardMarginX,
ev.PageBounds.Top - (int)ev.PageSettings.HardMarginY,
ev.PageBounds.Width,
ev.PageBounds.Height);
// Draw a white background for the report
ev.Graphics.FillRectangle(Brushes.White, adjustedRect);
// Draw the report content
ev.Graphics.DrawImage(pageImage, adjustedRect);
// Prepare for the next page. Make sure we haven't hit the end.
m_currentPageIndex++;
ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
}
【问题讨论】:
-
为什么不使用 RDLC 报告?
-
@RezaAghaei 我试过了,但是使用 RDLC 打印很慢,例如开始打印需要 10 秒,我需要按时开始打印。
-
尝试在没有报告引擎的情况下自己创建报告是在重新发明轮子 IMO,但您更了解自己的要求 :)。也许有些东西使报告/打印如此缓慢。我在使用 RDLC 报告时没有看到这种延迟,但在托管在服务器上的 RDL 报告中看到了这种延迟,并且由于服务器唤醒以及网络和数据库服务器延迟,延迟是第一步。
-
@RezaAghaei 我理解你。我将发布如何使用 RDLC 打印此报告,如果您能给出一个想法,我会很高兴。
-
我没有检查您代码中的性能问题,但我分享了一个有用的选项。
标签: c# winforms report printdocument