I'm on .net framework 4.8 in my WPF app and I have two usages on RDLC. 1st is a fully fetched ReportViewer that uses a DataTable from postgres, 2nd is just a LocalReport with small number of parameters rendered as EMF and printed directly with use of default printer.
They both have what would seem to be rendering issues, but just on monitors that have recommended scaling (RS) >100%. The outcome is squashing of letters vertically and adding some extra space in between (I can provide samples as soon as I get access to client machine again). If I just increase scaling on my 100% RS monitor, everything prints out just fine. If I replace the >100% RS monitor with a 1080p 100% RS one, again, everything prints out fine. Printouts on machines with monitors with >100% RS come out always messed up irrelevant of the scaling I set in Windows. Issues can be quickly reproduced with just 'Print Layout' view in ReportViewer, exporting to PDF produces same results.
Since I have ReportViewer and a direct printout of LocalReport I was able to try out several different approaches:
Client machines run on either latest Windows 10 or close to latest and are rather empty otherwise.
Does it ring any bells? Any idea for potential fix?
I would love to use RDLC in my app, for the simplicity of development and usage, but those issues are really a no go for the technology.
Used to print a single document directly without preview from parameters.
class CytologiaPrinter : IDisposable
{
private static readonly ILog log = LogManager.GetLogger(typeof(CytologiaPrinter));
private int m_currentPageIndex;
private IList<Stream> m_streams;
private int WizytaID;
private CytologiaAnkieta Cytologia;
public CytologiaPrinter(int wizytaID)
{
WizytaID = wizytaID;
}
public CytologiaPrinter(CytologiaAnkieta cytologia)
{
Cytologia = cytologia;
}
public void Print()
{
try
{
CytologiaAnkieta cytologia;
if (Cytologia == null)
{
cytologia = DBCommunication.fetchCytologia(WizytaID);
}
else
{
cytologia = Cytologia;
}
if (cytologia != null && cytologia.AnkietaNumer != null && cytologia.AnkietaNumer.Length > 0)
{
LocalReport report = new LocalReport();
var cytologie = new List<CytologiaAnkieta>();
cytologie.Add(cytologia);
ReportDataSource reportDataSource = new ReportDataSource("DataSet1", cytologie);
report.DataSources.Add(reportDataSource);
report.ReportEmbeddedResource = "Suplement.CytologiaAnkieta.rdlc";
var parameters = new List<ReportParameter>();
//parameters.Add(...); //setting all parameters omitted for demo
report.SetParameters(parameters);
m_currentPageIndex = 0;
Print(cytologia);
}
}
catch (Exception ex)
{
log.Error("Error (" + ex.Message + "), stack:" + ex.StackTrace);
}
}
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 Export(LocalReport report)
{
string deviceInfo =
@"<DeviceInfo>
<OutputFormat>EMF</OutputFormat>
<PageWidth>29.7cm</PageWidth>
<PageHeight>21cm</PageHeight>
<MarginTop>1cm</MarginTop>
<MarginLeft>1cm</MarginLeft>
<MarginRight>1cm</MarginRight>
<MarginBottom>1cm</MarginBottom>
</DeviceInfo>"; //printing in landscape
Warning[] warnings;
m_streams = new List<Stream>();
report.Render("Image", deviceInfo, CreateStream,
out warnings);
if (warnings != null && warnings.Length > 0)
{
foreach (var warn in warnings)
{
log.Warn("Cytologia printing issues: " + warn.Message);
}
}
foreach (Stream stream in m_streams)
stream.Position = 0;
}
private void PrintPage(object sender, PrintPageEventArgs ev)
{
Metafile pageImage = new
Metafile(m_streams[m_currentPageIndex]);
Rectangle adjustedRect = new Rectangle(
ev.PageBounds.Left - (int)ev.PageSettings.HardMarginX,
ev.PageBounds.Top - (int)ev.PageSettings.HardMarginY,
ev.PageBounds.Width,
ev.PageBounds.Height);
ev.Graphics.FillRectangle(Brushes.White, adjustedRect);
ev.Graphics.DrawImage(pageImage, adjustedRect);
m_currentPageIndex++;
ev.HasMorePages = m_currentPageIndex < m_streams.Count;
}
private void Print(CytologiaAnkieta cytologia)
{
if (m_streams == null || m_streams.Count == 0)
throw new Exception("Error: no stream to print.");
PrintDocument printDoc = new PrintDocument();
printDoc.DefaultPageSettings.Landscape = true;
if (!printDoc.PrinterSettings.IsValid)
{
throw new Exception("Error: cannot find the default printer.");
}
else
{
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
m_currentPageIndex = 0;
printDoc.Print();
}
}
public void Dispose()
{
if (m_streams != null)
{
foreach (Stream stream in m_streams)
stream.Close();
m_streams = null;
}
}
}
xmlns:rv="clr-namespace:Microsoft.Reporting.WinForms;assembly=Microsoft.ReportViewer.WinForms"
...
<WindowsFormsHost DockPanel.Dock="Bottom" Margin="0 0 0 0" >
<rv:ReportViewer x:Name="RVDemo"/>
</WindowsFormsHost>
private void RaportGenerate_Click(object sender, RoutedEventArgs e)
{
RVDemo.Reset();
ReportDataSource reportDataSource = new ReportDataSource("Ankiety", DBCommunication.fetchCytologiaAnkietyReport(...));
RVDemo.LocalReport.DataSources.Add(reportDataSource);
RVDemo.LocalReport.ReportEmbeddedResource = "Suplement.Cytologie.rdlc";
var parameters = new List<ReportParameter>();
//parameters.Add(...); // omitted for demo
RVDemo.LocalReport.SetParameters(parameters);
RVDemo.RefreshReport();
}
If there are no fixes available for RDLC scaling issue on WPF app.
One possible workaround would be migrating the file rendering part to Web version of RDLC, which would ignore screen DPI (as far as I know).
You'll need extra resource to develop this.
But a few generic functions would be enough, in most cases.
Then your reports should be able to rendered with consistence scaling.
(You may not need addtional project library for ReportViewer.WebForms if the library Microsoft.ReportViewer.Common
version can be used by both WebForms and WinForms ReportViewer.)
Here's the possible solution:
1) Add a Library Project to your WPF solution
The solution should use .NET Framework 4+. It would look something like this.
2) Download the WebForm version of RDLC to the new library through NuGet
Look for Microsoft.ReportViewer.WebForms
by Microsoft.
Correct version of Microsoft.ReportViewer.Common
will be installed for you as dependence.
3) Create the code for Rendering through Web Version of RDLC
Create a static class for WDF project to use, this is a very simple sample for you to test if it works, before you continuing on.
Copy this class in the "RLDCRendering" Project:
using Microsoft.Reporting.WebForms;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RDLCRendering
{
public class RDLCRender
{
public static byte[] RenderReport(String reportPath, DataTable data)
{
Microsoft.Reporting.WebForms.ReportDataSource rdc1 =
new Microsoft.Reporting.WebForms.ReportDataSource("DataSet1", data);
Microsoft.Reporting.WebForms.ReportViewer v1 = new Microsoft.Reporting.WebForms.ReportViewer();
v1.LocalReport.DataSources.Clear();
v1.LocalReport.ReportPath = reportPath;
v1.LocalReport.DataSources.Add(rdc1);
return v1.LocalReport.Render(format: "PDF", deviceInfo: "");
}
}
}
The project would looks like this:
4) Hide the WPF version's report print button
Hide the Print /Save button with this example code so users would not use the faulted rendering method
ReportViewer.ShowPrintButton = false;
ReportViewer.ShowExportButton = false;
Add a print button on your WDF page, how you do it is up to you.
The end result is something like this:
Add a callback when the button is clicked, then provide all the needed data source, report path, output path to the library we created.
The follow is a sample code for you:
string connString = "Server=someIP;Database=catalogName;User Id=uid;Password=pwd";
SqlConnection sqlConn = new SqlConnection(connString);
SqlDataAdapter sqlDA = new SqlDataAdapter("select top 100 * from samplesData", sqlConn);
DataTable dt= new DataTable();
sqlDA.Fill(dt);
//Important
Byte[] bytes = RDLCRendering.RDLCRender.RenderReport(@"the path to the report teamplate\Report1.rdlc", dt);
using (FileStream stream = new FileStream(@"C:\test\test.pdf", FileMode.Create))
{
stream.Write(bytes, 0, bytes.Length);
}
5) Validate
You may change the output path as you need. When the button is clicked, a PDF file should be rendered and saved under the location you specified. In my sample, it is in C:\test\test.pdf.
If this solution works for you, you may continue to add parameters and etc. to the/other rendering function byte[] RenderReport
.
Then handle the returned byte file by sending it to printer or save to some local folder and open with other applications.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With