我正在使用 RDLC 报告以动态值打印 POS 小到未定义的高度收据。我在页面中将宽度设置为 4 英寸,将高度设置为 4 英寸,并且在代码中将 Landscape 设置为 false。收据打印完美,但是当打印大量数据时,它会在达到一定高度后进入下一页,并且在每页末尾打印边框也会隐藏一些并在其上打印边距线。我尝试了 WPF 流文档方法,但无法将 IP 发送到打印机。我想在网络中打印时传递打印机名称。我使用下面的代码打印。
string deviceInfo =
$@"<DeviceInfo>
<OutputFormat>EMF</OutputFormat>
<PageWidth>{4}in</PageWidth>
<PageHeight>{4}in</PageHeight>
<MarginTop>{0}in</MarginTop>
<MarginLeft>{0}in</MarginLeft>
<MarginRight>{0}in</MarginRight>
<MarginBottom>{0}in</MarginBottom>
</DeviceInfo>";
Warning[] warnings;
var streams = new List<Stream>();
var currentPageIndex = 0;
report.Render("Image", deviceInfo,
(name, fileNameExtension, encoding, mimeType, willSeek) =>
{
var stream = new MemoryStream();
streams.Add(stream);
return stream;
}, out warnings);
foreach (Stream stream in streams)
stream.Position = 0;
if (streams == null || streams.Count == 0)
throw new Exception("Error: no stream to print.");
var printDocument = new PrintDocument();
printDocument.DefaultPageSettings = pageSettings;
if(printer!="")
{
printDocument.PrinterSettings.PrinterName = "E-PoS printer driver (1)";// "E -PoS printer driver (1)";
}
printDocument.DefaultPageSettings.Landscape = false;
printDocument.DefaultPageSettings.Margins.Bottom = 0;
if (!printDocument.PrinterSettings.IsValid)
throw new Exception("Error: cannot find the default printer.");
else
{
var paperSize = new PaperSize("Custom", 520, 820);
printDocument.DefaultPageSettings.PaperSize = paperSize;
printDocument.PrintPage += (sender, e) =>
{
Metafile pageImage = new Metafile(streams[currentPageIndex]);
Rectangle adjustedRect = new Rectangle(
e.PageBounds.Left - (int)e.PageSettings.HardMarginX,
e.PageBounds.Top - (int)e.PageSettings.HardMarginY,
e.PageBounds.Width,
e.PageBounds.Height);
e.Graphics.FillRectangle(Brushes.White, adjustedRect);
e.Graphics.DrawImage(pageImage, adjustedRect);
currentPageIndex++;
e.HasMorePages = (currentPageIndex < streams.Count);
e.Graphics.DrawRectangle(Pens.Red, adjustedRect);
};
printDocument.EndPrint += (Sender, e) =>
{
if (streams != null)
{
foreach (Stream stream in streams)
stream.Close();
streams = null;
}
};
printDocument.Print();
}