c# - C# RDLC 报告 - 直接到打印机 - 格式折叠
问题描述
我在我们的 WPF 应用程序中使用 ssrs 客户端 rdlc 报告(3 英寸打印机)。当报告呈现为 .PDF 并将其保存到临时路径,然后打印 PDF 意味着它可以完美运行。但是当我尝试直接将报表打印到打印机时,报表呈现为图像(MSDN 网站建议的打印代码),但只打印打印机中报表的一半,而且看起来太难看,因为它看起来像拖。
报告宽度:3.5 英寸报告高度:7 英寸
请查看我使用的以下代码并提出您的意见。
// RDLC InvoicePrintReport - Report Ready with Data Source and Parameters
Export(InvoicePrintReport);
Print();
//Report Printing Section
private int m_currentPageIndex;
private IList<Stream> m_streams;
private Stream CreateStream(string name, string fileNameExtension, System.Text.Encoding encoding,
string mimeType, bool willSeek)
{
Stream stream = new MemoryStream();
m_streams.Add(stream);
return stream;
}
private void Export(LocalReport report)
{
string deviceInfo = @"<DeviceInfo><OutputFormat>EMF</OutputFormat><PageWidth>3.5in</PageWidth><MarginTop>0.01in</MarginTop><MarginLeft>0.01in</MarginLeft><MarginRight>0.1in</MarginRight><MarginBottom>0.01in</MarginBottom></DeviceInfo>";
Microsoft.Reporting.WinForms.Warning[] warnings;
m_streams = new List<Stream>();
report.Render("Image", deviceInfo, CreateStream, out warnings);
foreach (Stream stream in m_streams) stream.Position = 0;
}
private void Print()
{
if (m_streams == null || m_streams.Count == 0) throw new Exception("Error: no stream to print.");
PrintDocument printDoc = new PrintDocument();
if (!printDoc.PrinterSettings.IsValid)
{
throw new Exception("Error: cannot find the default printer.");
}
else
{
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
m_currentPageIndex = 0;
//PaperSize pkCustomSize1 = new PaperSize("First custom size", 100, 200);
//printDoc.DefaultPageSettings.PaperSize = pkCustomSize1;
printDoc.Print();
}
}
private void PrintPage(object sender, PrintPageEventArgs ev)
{
System.Drawing.Imaging.Metafile pageImage =
new System.Drawing.Imaging.Metafile(m_streams[m_currentPageIndex]);
// Adjust rectangular area with printer margins.
System.Drawing.Rectangle adjustedRect = new System.Drawing.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(System.Drawing.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);
}
解决方案
几乎在我的问题中就有了答案。问题在于纸张尺寸属性。现在更改了如下所述的纸张尺寸,用于 3 英寸打印就可以了。
private void Print()
{
if (m_streams == null || m_streams.Count == 0) throw new Exception("Error: no stream to print.");
PrintDocument printDoc = new PrintDocument();
if (!printDoc.PrinterSettings.IsValid)
{
throw new Exception("Error: cannot find the default printer.");
}
else
{
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
m_currentPageIndex = 0;
PaperSize pkCustomSize1 = new PaperSize("First custom size", 350, 700);
printDoc.DefaultPageSettings.PaperSize = pkCustomSize1;
printDoc.Print();
}
}
注意:350 - 宽度 - 百分之一英寸 700 - 高度 - 百分之一英寸(AutoCut 将在热敏打印机中完成其余部分,因此不需要完全 700)
推荐阅读
- reactjs - 使用效果不返回两个功能
- kql - KQL如何根据列表查找表中的行
- c# - 如何在 C# 中合并位图(Alpha 位图和主位图)?
- python - Pandas 应用函数抛出 NotImplementedError
- r - 线性规划中的组/投资组合约束
- c - 是否有一种算法可以找到一周中的某一天(周日、周一等)?
- postgresql - 通过 CLI 重置 Azure PostgreSQL 服务器用户密码
- python - 在python中查找恰好具有两个具有相同值的嵌套元素的列表元素
- javascript - React.js:应用 CSS 过滤器后下载图像
- f# - 避免重复映射到 Union 类型的情况