我有一个Windows窗体(C#、. NET 4.6.1),该窗体在没有报表查看器的情况下打印rdlc报表。在Visual Studio中,报表看起来不错;字体和边距与报表设计器中的完全相同。当我发布我的应用程序并在另一台计算机上尝试时,它会更改字体并切断左侧边距。我试图在报表设计器中更改字体和边距,并更改传递给打印机的边距(请参见下面的代码),但是在已发布的计算机上,它始终以其他字体和左侧的字体进行打印。我该如何解决?
在这一点上,我的猜测是(1.)发布时需要包括其他文件。我已经包括了rdlc,所以我不确定还包括什么。也许一些参考DLL报告? (2.)我需要将字体名称传递给PrintRDLCReport类中的某些东西。同样,不确定这是什么样子。 (3.)我需要另一种类型的“构建操作”而不是“内容”。我不知道还能选择什么。
任何想法都将不胜感激。在机器上运行后,我昨天发表了这篇文章。我只需要在客户端计算机上运行它即可。
这是已打印报告的扫描图像。当我通过Visual Studio运行应用程序时,最上面的一行来自报告(这是我希望它看起来的样子)。底线是我发布并安装该应用程序后从另一台计算机上获得的。
以下是直接打印我的报告的代码:
LocalReport report = new LocalReport();
report.ReportPath = @".\Reports\TrayLabels.rdlc";
report.DataSources.Add(
new Microsoft.Reporting.WinForms.ReportDataSource("ReportDataSource",
filtersToBuild));
PrintRDLCReport.PrintToPrinter(report);
这是PrintRDLCReport类的代码:
公共静态类PrintRDLCReport { 私有静态Logger logger = LogManager.GetCurrentClassLogger();
private static int m_currentPageIndex;
private static IList<Stream> m_streams;
private static PageSettings m_pageSettings;
public static Stream CreateStream(string name,
string fileNameExtension, Encoding encoding,
string mimeType, bool willSeek)
{
Stream stream = new MemoryStream();
m_streams.Add(stream);
return stream;
}
public static void Export(LocalReport report, bool print = true)
{
PaperSize paperSize = m_pageSettings.PaperSize;
Margins margins = m_pageSettings.Margins;
// The device info string defines the page range to print as well as the size of the page.
// A start and end page of 0 means generate all pages.
string deviceInfo = string.Format(
CultureInfo.InvariantCulture,
"<DeviceInfo>" +
"<OutputFormat>EMF</OutputFormat>" +
"<PageWidth>{0}</PageWidth>" +
"<PageHeight>{1}</PageHeight>" +
"<MarginTop>{2}</MarginTop>" +
"<MarginLeft>{3}</MarginLeft>" +
"<MarginRight>{4}</MarginRight>" +
"<MarginBottom>{5}</MarginBottom>" +
"<DeviceOrientation>{6}</DeviceOrientation>" +
"</DeviceInfo>",
1100,
850,
0,
0,
0,
0,
"LANDSCAPE"
);
Warning[] warnings;
m_streams = new List<Stream>();
report.Render("Image", deviceInfo, CreateStream,
out warnings);
foreach (Stream stream in m_streams)
stream.Position = 0;
if (print)
{
Print();
}
}
// Handler for PrintPageEvents
public static void PrintPage(object sender, PrintPageEventArgs e)
{
Stream pageToPrint = m_streams[m_currentPageIndex];
pageToPrint.Position = 0;
// Load each page into a Metafile to draw it.
using (Metafile pageMetaFile = new Metafile(pageToPrint))
{
Rectangle adjustedRect = new Rectangle(
e.PageBounds.Left - (int)e.PageSettings.HardMarginX,
e.PageBounds.Top - (int)e.PageSettings.HardMarginY,
e.PageBounds.Width,
e.PageBounds.Height);
// Draw a white background for the report
e.Graphics.FillRectangle(Brushes.White, adjustedRect);
// Draw the report content
e.Graphics.DrawImage(pageMetaFile, adjustedRect);
// Prepare for next page. Make sure we haven't hit the end.
m_currentPageIndex++;
e.HasMorePages = m_currentPageIndex < m_streams.Count;
}
}
public static 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;
printDoc.DefaultPageSettings.Landscape = true;
printDoc.DefaultPageSettings.Margins.Left = 0;
printDoc.DefaultPageSettings.Margins.Right = 0;
printDoc.DefaultPageSettings.Margins.Top = 0;
printDoc.DefaultPageSettings.Margins.Bottom = 0;
printDoc.Print();
}
}
public static void PrintToPrinter(this LocalReport report)
{
m_pageSettings = new PageSettings();
ReportPageSettings reportPageSettings = report.GetDefaultPageSettings();
m_pageSettings.PaperSize = reportPageSettings.PaperSize;
m_pageSettings.Margins = reportPageSettings.Margins;
Export(report);
}
public static void DisposePrint()
{
if (m_streams != null)
{
foreach (Stream stream in m_streams)
stream.Close();
m_streams = null;
}
}
private static string ToInches(int hundrethsOfInch)
{
double inches = hundrethsOfInch / 100.0;
return inches.ToString(CultureInfo.InvariantCulture) + "in";
}
}
这是rdlc报表的属性(我本来不得不总是将其更改为Content and Copy才能使报表甚至发布)。