This is a migrated thread and some comments may be shown as answers.

Use ExportToPDf to generate a PDF from ASPX page?

3 Answers 111 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 09 Jan 2010, 12:07 PM

Hi:

I have an ASP.NET application that uses tabs and grids to display data to the end user, and they want the ability to click a button and have all the data on the web page generated to a PDF file. There are some images, some data in grids, and some text in text boxes.

Is it possible to build a report step-by-step from the data on the page, and then pass that report to the Export method to generate the PDF? So for example something like:

Report report1 = new Report();
report1.addImage( image):
table table1 = new table();
for (int i=0; i<radGrid.Rows.Count; i++)
{
  For each row of data, add a row to the table
}
report1.add(table);

And so on until all the data is added to a report, and then use

ExportToPdf(report1)

Is this possible and/or is there another way to send all the data from the ASPX page to a PDF using the component?


Here is some code I used to try this, and I did get a PDF file, but it was empty:

 

 void ExportToPDF(Telerik.Reporting.Report reportToExport)
   {
       ReportProcessor reportProcessor = new ReportProcessor();
       RenderingResult result = reportProcessor.RenderReport("PDF", reportToExport, null);

       string fileName = result.DocumentName + ".pdf";

       Response.Clear();
       Response.ContentType = result.MimeType;
       Response.Cache.SetCacheability(HttpCacheability.Private);
       Response.Expires = -1;
       Response.Buffer = true;

       Response.AddHeader("Content-Disposition",
                          string.Format("{0};FileName=\"{1}\"",
                                        "attachment",
                                        fileName));

       //Response.Write("test");
       Response.BinaryWrite(result.DocumentBytes);
       Response.End();
   }   

 
  protected void Button1_Click(object sender, EventArgs e)
  {     
    try
    {
      Telerik.Reporting.Report report1 = new Telerik.Reporting.Report();
      DataTable dt = new DataTable();
      DataColumn column1 = new DataColumn();
      column1.ColumnName = "1";
      column1.ReadOnly = true;
      dt.Columns.Add(column1);
      DataColumn column2 = new DataColumn();
      column2.ColumnName = "2";
      column2.ReadOnly = true;
      dt.Columns.Add(column2);

      for (int i = 0; i < 25; i++)
      {
        DataRow theRow = dt.NewRow();
        theRow["1"] = i.ToString();
        int z = i + 5;
        theRow["2"] = z.ToString();
        dt.Rows.Add(theRow);
      }

      report1.DataSource = dt;
      ExportToPDF(report1);
   }
    catch (Exception ex)
    {
      txtMessage.Text += "Error: " + ex.ToString();
    }
}

3 Answers, 1 is accepted

Sort by
0
Steve
Top achievements
Rank 2
Veteran
answered on 11 Jan 2010, 02:30 AM
If you have access to the RadControls I'd suggest trying feeding the direct HTML into the RadEditor Content property, then using it's built in export functionality...might be easier since it knows just about full html and can render the links.
0
John
Top achievements
Rank 1
answered on 11 Jan 2010, 02:38 PM
Hi Steve:

That's  a really interesting idea, and I guess once all the data was in the Editor they could edit it to their heart's content before doing the export...I like that!!

Do you know how I can get data into the editor from the Code behind file? I was able to add things like a string using RadEditor1.Content += "Content", but I am not sure how to add the data from a datagrid. I tried to build a table from the data grid and adding that, but no joy. I have 4 data grids whose content needs to be added to the editor.

John
0
Steve
Top achievements
Rank 2
Veteran
answered on 11 Jan 2010, 02:44 PM
Tags
General Discussions
Asked by
John
Top achievements
Rank 1
Answers by
Steve
Top achievements
Rank 2
Veteran
John
Top achievements
Rank 1
Share this question
or