Sample Code for Printing Invoice Report to PDF

Posted by: ronnnyr on 14 September 2017, 7:17 pm EST

    • Post Options:
    • Link

    Posted 14 September 2017, 7:17 pm EST

    [activereports_archive]Hi All,

    I used ActiveReports3 in the past and I was able to print PDF and set header value programmatically in the print.cs file.

    However, with ActiveReports11, I couldn’t see PDF as an option in the library (GrapeCity.ActiveReports.Export.) - it only shows HTML as the option.

    Also, how can I go to .cs file from .rdlx?

    I would like to set each textbox with the value programmatically. I was able to do this in ActiveReports3 under Print_ReportStart method.

    Thanks in advance.[/activereports_archive]

  • Posted 14 September 2017, 7:17 pm EST

    [activereports_archive]Hello

    ActiveReports3 was released way back and since then ActiveReports has undergone many changes.

    1. In ActiveReports11, for exporting report to PDF format, you need to add “GrapeCity.ActiveReports.Export.Pdf.v11” dll into your project’s References section. You may find this dll at “C:\Program Files (x86)\Common Files\GrapeCity\ActiveReports 11”

      For detailed information on how to export ActiveReports, please refer to the following Documentation link:

      http://arhelp.grapecity.com/webhelp/AR11/index.html#ExportingReports.html

    2. For setting the Header/Footer text with code, you would need to access the controls present in the ReportItems collection and then change their properties as follows:

      [csharp]rpt = new PageReport(new System.IO.FileInfo(“…/…/Sample_RDLReport.rdlx”));

      GrapeCity.ActiveReports.PageReportModel.TextBox tbHeader = rpt.Report.PageHeader.ReportItems[“HeaderTextBox”] as GrapeCity.ActiveReports.PageReportModel.TextBox;

      tbHeader.Value = “Header Text”;

    GrapeCity.ActiveReports.PageReportModel.TextBox tbFooter = rpt.Report.PageFooter.ReportItems[“FooterTextBox”] as GrapeCity.ActiveReports.PageReportModel.TextBox;

    tbFooter.Value = “Footer Text”;[/csharp]

    For more information on this please refer to the following Blog demonstrating how to access report items at run time:

    http://arhelp.grapecity.com/2014/09/11/how-to-access-pagereports-reportitems-using-code/

    Also I have attached a sample application exporting RDL report to pdf and setting the Header/Footer text programmatically.

    Let us know if you have any further queries.

    Regards

    Ruchir Agarwal[/activereports_archive]

    2017/03/prj_ExportToPdfAR11.zip

  • Posted 14 September 2017, 7:17 pm EST

    [activereports_archive]Hi Ruchir,

    Thanks for the references. I still have difficulty load the page when user click on the button. Here is some more details of my projects.

    1. I’m using ASP.NET Web Forms application
    2. I have default.aspx page which will display 3 buttons: Print Invoice, Print Packing Slip, and Print Picking Ticket
    3. Where should I populate the ReportItems - in default.aspx.cs or print.aspx.cs page?
    4. In ActiveReports3, I was able to populate report item in the printInvoice rdl .cs page. There are Print_Initialize and Print_ReportStart method. Where can I do the same for ActiveReports11?

    Thanks.

    [/activereports_archive]

  • Posted 14 September 2017, 7:17 pm EST

    [activereports_archive]I learnt that Section Report is the one I need to use and it will allow me to call ReportStart method from the code behind similar to ActiveReports3.

    However, how can I save the file as rpx? From VS2010, I add new item and I choose ActiveReports 11 Section Report (code-based) and it doesn’t create rpx file. There is another option to add item as ActiveReports 11 Section Report (xml-based) and it will be saved as rpx, however, it doesn’t have .cs file.

    So, which type of report should I choose which will give me both designer as well as code behind?

    Please help. Thanks.[/activereports_archive]

  • Posted 14 September 2017, 7:17 pm EST

    [activereports_archive]Hello,

    Using VisualStudio, you can add a SectionReport in your ASP.Net application in two ways:

    1. Code-based SectionReport: Adding this report type in your application, report layouts are saved as C#/Vb files within the project in which they are created. These files are compiled into the application when you build it and each report is composed of three files:

      a) rptYourReportName.vb or .cs

      b) rptYourReportName.Designer.vb or .cs

      c) rptYourReportName.resx

    2. Xml-based SectionReport: Adding this report type in your application, the layout is saved as a stand-alone Report XML (RPX) file and these files are not compiled into your application.

    But if you want to have *rpx file along with code behind file, then you should use Code-based SectionReport and then you save the report as an rpx file at run time with the help of “SaveLayout()” method.

    For more information on how to save report as *.rpx file, please refer to the following Documentation link: http://arhelp.grapecity.com/webhelp/AR11/index.html#SaveandLoadReportLayoutFilesRPX.html

    I have also attached a sample ASP.Net application, having a code-behind file for modifying the Header/Footer value in the Report_Start event and then saving the report as an *.rpx file.

    Let me know if you need anything else or if I missed anything.

    Regards

    Ruchir Agarwal[/activereports_archive]

    2017/03/prj_AR11ExportToPDF_SectionReport.zip

  • Posted 14 September 2017, 7:17 pm EST

    [activereports_archive]Hi Ruchir,

    I still wasn’t able to set the text value of my controls using ReportStart method.

    Please see attached file.

    1. For some reason, my web viewer doesn’t show the report.
    2. Can I not using web viewer and go straight to pdf print/export?
    3. For whatever reason, the order date label didn’t get set with the value I provided in the ReportStart method.
    4. Just FYI, I save the rpx file by going to report designer then Report - Save Layout and I saved it under Reports folder.

    Thanks in advance.[/activereports_archive]

    2017/03/AR_Section_ExportPDF.zip

  • Posted 14 September 2017, 7:17 pm EST

    [activereports_archive]Here is my webviewer (attach). Also I attach the PDF after clicking the button - it shows the default value, not the one I set in ReportStart event.[/activereports_archive]

    2017/03/WebViewer1.pdf

    2017/03/MyPDF.pdf

  • Posted 14 September 2017, 7:17 pm EST

    [activereports_archive]Hi Ruchir,

    Basically I’m trying to create an invoice report and I believe Section report is the right way to go.

    I will query Salesforce object using API and then set the Label/Textbox value - I believe this needs to be done in ReportStart event.

    So, there won’t be any database binding, and its more of the object/List<> binding.

    Is there any example for this?

    Couple of questions regarding section report:

    1. Should I save the report in Reports folder or debug folder?
    2. Should I save the report at runtime or save using Report menu - Save Layout? Since I would like to use designer to create my report template, I believe I have to save it through Report menu - Save Layout.
    3. Do we need to use WebViewer? Can we just print it to pdf directly?

    Thanks in advance.[/activereports_archive]

  • Posted 14 September 2017, 7:17 pm EST

    [activereports_archive]Hi,

    Thanks for sending across the application, however the application did not work at our end as it is missing some files.

    1. If you do not want to use WebViewer for viewing the report and would like to straight away export the report as pdf file, all you need to do is modify the code snippet:

      [csharp] Response.AddHeader(“content-disposition”, “inline;filename=MyExport.pdf”);[/csharp] to

      [csharp] Response.AddHeader(“content-disposition”, “attachment;filename=MyExport.pdf”);[/csharp] in your PrintPackingSlip.aspx.cs file.
    2. It does not matter where you save your report in Reports folder/Debug folder.
    3. If you want to change your report items text in “ReportStart” event then you need to save the report at run-time by clicking on the “Export to Pdf” button, only then you will have the custom text you set in “ReportStart” event. Since if you save the report using VS’s Report menu – Save Layout option, then it will save the report at design time with report item’s having static text and not reflecting the changes made in ReportStart event (since ReportStart event fires at run-time).
    4. For binding the report datasource at runtime, please refer to the following Documentation link:

      http://arhelp.grapecity.com/webhelp/AR11/index.html#arWLKRunTimeDataSources.html

    I have modified the sample application sent earlier to export the report to directly to a pdf file.

    Note: Make sure to build the application once before running the sample.

    ~Ruchir Agarwal[/activereports_archive]

    2017/03/prj_AR11ExportToPDF_SectionReport_modified.zip

  • Posted 14 September 2017, 7:17 pm EST

    [activereports_archive]Hi Ruchir,

    Please find attached is the full project.

    I submitted the case as well since there is a file upload limitation.

    As you see, the label is still set with static value from report designer.

    And regarding your comment:

    1. If you want to change your report items text in “ReportStart” event then you need to save the report at run-time by clicking on the “Export to Pdf” button, only then you will have the custom text you set in “ReportStart” event. Since if you save the report using VS’s Report menu – Save Layout option, then it will save the report at design time with report item’s having static text and not reflecting the changes made in ReportStart event (since ReportStart event fires at run-time).

    I would like to be able to design the report (placing of the label and text box, etc.) and bind only the values at run time, because it is difficult to align the controls during run time.

    Thanks again in advance.

    [/activereports_archive]

  • Posted 14 September 2017, 7:17 pm EST

    [activereports_archive]Hi Ronny

    Thank you for creating a case for sharing across the full project.

    The label values were still static in exported pdf file because though the “ReportStart” event handler was created and label text was set to some custom text inside it, “ReportStart” event handler was not referenced anywhere (might be because the handlers were just copy pasted…etc) due to which the event was not getting called and hence label text values were not updated to run time values. For creating reference to the event, you may double click the empty area in report designer which will create an event handler for “ReportStart” event (for now I have done the same for you)

    1. For designing the report (placing of the label and text box, etc.), double click “PackingSlip.cs” file which will open up the Integrated Report Designer. And for setting their run-time values and/or binding their values at run time use the code file of “PackingSlip.cs” {press F7 on Designer}.

    I have modified and attached your sample project for reference.

    Regards

    Ruchir Agarwal[/activereports_archive]

    2017/03/ARSample_modified.zip

  • Posted 14 September 2017, 7:17 pm EST

    [activereports_archive]Thanks Ruchir. It works now.

    [/activereports_archive]

  • Posted 14 September 2017, 7:17 pm EST

    [activereports_archive]Hi Ronny,

    Glad to know we could help resolve your issue.

    Let us know if you face any problem in future.

    ~Ruchir Agarwal[/activereports_archive]

  • Posted 22 June 2018, 3:29 pm EST

    I try to follow your step but still not able to set the text value. But I have learned about this matter after reading this post. After the interruption, I made contact https://supportprop58.com/microsoft-edge-support/

    they told me wright process, you can also go there if you need help.

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels