Creating a PDF from a RDLC Report in the Background
This is easy to do, you can render the report as a PDF, and save the resulting byte array as a PDF file on disk. To do this in the background, that's more a question of how your app is written. You can just spin up a new thread, or use a BackgroundWorker (if this is a WinForms app), etc. There, of course, may be multithreading issues to be aware of.
Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string filenameExtension;
byte[] bytes = reportViewer.LocalReport.Render(
"PDF", null, out mimeType, out encoding, out filenameExtension,
out streamids, out warnings);
using (FileStream fs = new FileStream("output.pdf", FileMode.Create))
{
fs.Write(bytes, 0, bytes.Length);
}
RDLC export to pdf with high resolution background image
After some digging and testing I finally got it working (at least imo)
Solution
- Setting the background of the report
I set the background of the report as following:
Source: External
Value: ="file:" + Parameters!PathToStationeryPaper.Value
MIMEType: image/jpeg
- Check usage of stationary paper
As not all reports of the whole software solution use stationary paper, I just check the reports parameter.
private bool reportUsesStationaryPaper()
{
var result = false;
foreach (var param in reportViewer1.LocalReport.GetParameters())
{
if (param.Name.Equals("UsesStationeryPaper"))
{
result = true;
break;
}
}
return result;
}
- Show low-res image on preview
In the event “reportviewer_RenderingBegin” I set a low-resolution image to improve performance. You can also set a version of the stationary paper with a watermark here
private void reportViewer1_RenderingBegin(object sender, CancelEventArgs e)
{
if (reportUsesStationaryPaper())
{
//Reset report to low-resolution mode for screen rendering
var pathToStationaryPaper = "[…]/stationary_lowres.png";
reportViewer1.LocalReport.SetParameters(new Microsoft.Reporting.WinForms.ReportParameter("PathToStationeryPaper", pathToStationaryPaper));
}
}
- Add device info
In the event “reportviewer_ReportExport” I set the path to a high-resolution image and additionally add device infos (“ReportExportEventArgs.DeviceInfo”) to increase the resolution as the export functionality seems to be able to work with higher quality.
private void reportViewer1_ReportExport(object sender, ReportExportEventArgs e)
{
if (reportUsesStationaryPaper())
{
//Reset report to high-resolution mode for printing
string deviceInfo =
@"<DeviceInfo>
<DpiX>300</DpiX>
<DpiY>300</DpiY>
</DeviceInfo>";
e.DeviceInfo = deviceInfo;
var pathToStationaryPaper = "[…]/stationary_highres.png";
reportViewer1.LocalReport.SetParameters(new Microsoft.Reporting.WinForms.ReportParameter("PathToStationeryPaper", pathToStationaryPaper));
}
}
- [OPT] Show export button
If you don’t need a separate button to call the export method of the reportviewer element you can just enable it via UI Designer or code
Result
- Preview
Higher performance as a lower resolution can be set and, if you want, a “preview” watermark 1
- Print on a „pdf printer“ (here „Microsoft print to pdf”)
Still fuzzy as we can’t set the resolution in “reportviewer_PrintingBegin” or “reportviewer_Print”
- Export with Reportviewer
When exported via export of the reportviewer control you will now get a beautiful high-resolution image as background of the report /p>
Further consequences
As the users of my client were used to “print to pdf” the client now either needs to train the users to use the reportviewer export button or a separate button “Export to PDF” will be introduced in the view.
Generate PDF from RDLC report in winform
Report was created in vs 2008 but, opened in vs 2013 and trying to generate PDF using Microsoft ReportViewer winform dll 10.0 so, that happened.
Its fixed by, opening old report file into VS 2010 and made related changes and use that one in visual studio. so, Microsoft Report viewer winform dll 10.0 is able to open this report.
its solved.
Thanks
Save RDLC reports as PDF programmatically
What's your question in here? Is it that it does not work?
here is an example of something we didin 2005. We defined a control called rptViewer1 which can be visible or not depending of your needs. strFormat should contain "PDF" and strNomFicher the full path.
BTW the variable names and functions are in french, but that will work anyway :)
Public Sub CreerFichierRapport(ByVal strNomFichier As String, ByVal strFormat As String)
Dim bytes() As Byte
Dim strDeviceInfo As String = ""
Dim strMimeType As String = ""
Dim strEncoding As String = ""
Dim strExtension As String = ""
Dim strStreams() As String
Dim warnings() As Warning
Dim oFileStream As FileStream
_stream = New List(Of Stream)
Try
bytes = rptViewer1.LocalReport.Render(strFormat, strDeviceInfo, strMimeType, strEncoding, strExtension, strStreams, warnings)
oFileStream = New FileStream(strNomFichier, FileMode.Create)
oFileStream.Write(bytes, 0, bytes.Length)
_stream.Add(oFileStream)
Finally
If Not IsNothing(oFileStream) Then
oFileStream.Close()
oFileStream.Dispose()
End If
End Try
End Sub
Build simple multi page PDF with RDLC
You can use a TextBox
with CanGrow
property set to True
inside a Body
whose height is higher (2x) than Page
height.
Please note that you can view the final effect only in Print Layout
mode of ReportViewer
.
How to take accurate print from RDLC report without saving as pdf
I found a solution by rendering the report viewer into pdf and directly will be displayed in PDF Print
Warning[] warnings;
string[] streamIds;
string contentType;
string encoding;
string extension;
//Export the RDLC Report to Byte Array.
byte[] bytes = ReportViewer1.LocalReport.Render("PDF", null, out contentType, out encoding, out extension, out streamIds, out warnings);
// Open generated PDF.
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = contentType;
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
Refer
https://www.aspsnippets.com/questions/110559/Export-PDF-from-RDLC-Report-and-open-in-Browser-on-Button-Click-using-C-and-VBNet-in-ASPNet/
Related Topics
How to Get My C# Program to Sleep for 50 Msec
How to Use CSS on an HTML.Actionlink in C#
How to Flatten an Expandoobject Returned via JSONresult in ASP.NET MVC
Posting JSON to Url via Webclient in C#
JSON.Net Serialize Object with Root Name
How to Avoid System.Io.Pathtoolongexception
How to Convert a Utf-8 String into Unicode
What's the Hardest or Most Misunderstood Aspect of Linq
Implement Validation for Wpf Textboxes
What Is the Purpose of Anonymous { } Blocks in C Style Languages
Binding Wpf Datagrid Cell Background Colour with Trigger
Asp.Net-Mvc: Razor '@' Symbol in Js File
How to Lock a Table on Read, Using Entity Framework
Best .Net Memory and Performance Profiler