Save PDF file from RDLC Reports

When developer need to generate PDF file based on report without any Report viewer in UI.
Below is the code window application to generate PDF file on specific location as well as save dialog box in window or web application. (Note: This code I have added code for window application for save dialog box code).

Microsoft.Reporting.WinForms.ReportViewer reportViewer1 = new Microsoft.Reporting.WinForms.ReportViewer();
            Microsoft.Reporting.WinForms.LocalReport objRDLC = new Microsoft.Reporting.WinForms.LocalReport();
            reportViewer1.LocalReport.ReportEmbeddedResource = "OrderDetails.rdlc";
            ReportData oReportData = new ReportData();
            DataTable oReportDataTable = oReportData. OrderDetails (OrderID, UserID).Tables[0]; // Fetch data from database
            reportViewer1.LocalReport.EnableHyperlinks = true;
            reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", oReportDataTable));
            byte[] byteViewer = reportViewer1.LocalReport.Render("PDF");

            SaveFileDialog saveFileDialog1 = new SaveFileDialog();

            saveFileDialog1.Filter = "*PDF files (*.pdf)|*.pdf";
            saveFileDialog1.FilterIndex = 2;
            saveFileDialog1.RestoreDirectory = true;
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                FileStream newFile = new FileStream(saveFileDialog1.FileName, FileMode.Create);
                newFile.Write(byteViewer, 0, byteViewer.Length);

This code is helpfull when requirement comes for create a PDF and sent an email to specified id from RDLC reports.
Let me ask a if you need more information by adding comments.

“Enjoy Programming”
Amit Patel

About these ads

Add any customize text in crystal reports by .net code

In my requirement I have to show few search parameter in crystal reports which is passed from aspx page.
And that is not hard coded fixed because I have 10-15 parameters and I want to place it dynamically if whatever parameter’s value available that only needs to be attach in reports.

So I have implemented this by below approaches.
In Crystal reports rpt file I have take one default fields with “SearchTextRight” and “SearchTextLeft” to display value left and right side corner.

This both fields “CanGrouw” property set as “True” and attach each of the search parameter by “\n” so automatically new line added and text object’s height will increased.
Below are the code we have used for that.

 ReportClass rptH = GenerateReportClasss(Server.MapPath("~/Reports/GetReortData.rpt"));
            /// Attach serarch fields

            SearchDraw oSearchDrow = AttachCustomerHours(collection);
            if (oSearchDrow.SearchingStringLeft != string.Empty)
                TextObject SeachTextLeft = (TextObject)rptH.ReportDefinition.ReportObjects["SearchTextLeft"];
                SeachTextLeft.Text = oSearchDrow.SearchingStringLeft;
            if (oSearchDrow.SearchingStringRight != string.Empty)
                TextObject SeachTextRight = (TextObject)rptH.ReportDefinition.ReportObjects["SearchTextRight"];
                SeachTextRight.Text = oSearchDrow.SearchingStringRight;


Now all text will attached in reports’ PDF or excel file.

Let me know for more details

Amit Patel
“Enjoy Programming”

HTML to PDF Converter

In any of the business application a scenario comes when html file is available
and we need to covert that html file in PDF format

i.e.  if we have provided invoice template to user for modification and we need to create a final PDF as final result based on that provided html template then we need to required logic/code to convert html file or string in PDF file

So below code main targeting to resolve above problem
statement. I have implemented below code which is convert html file in PDF by

Below are the code for this. (VB.NET)

Private Sub CovertHTMLTOPDF()
        Dim htmlstr As StreamReader = New StreamReader("Enter your html files physical path")
        Dim strline As String

        strline = htmlstr.ReadToEnd

        ' Code to convert to pdf
        Dim doc As New Document(PageSize.A4, 8, 80, 50, 30, 65)
        Dim fsNew As New StringReader(strline)
        Dim document As New Document(PageSize.A4, 80, 50, 30, 65)

        Using ofilestream As New FileStream(Request.PhysicalApplicationPath + "Output related PDF file", FileMode.Create)
            PdfWriter.GetInstance(document, ofilestream)
            Using stringReader As New StringReader(strline)
                Dim parsedList As New ArrayList()
                parsedList = html.simpleparser.HTMLWorker.ParseToList(stringReader, Nothing)
                ' parse each html object and add it to the pdf document
                For Each item As Object In parsedList
                    document.Add(DirectCast(item, IElement))


            End Using

        End Using
    End Sub

Please insert a comment if you required any more clarification.

Amit Patel
“Enjoy Programming”


Get every new post delivered to your Inbox.

Join 238 other followers