Blue Theme Orange Theme Green Theme Red Theme
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
Search :       Advanced Search �

Author Rank :
Page Views :
Downloads : 0
Rating :
 Rate it
Level :
Become a Sponsor

It is common to print PDF for ASP.NET. Actually, it is more convenient to print PDF than Excel because we don't need to set format. But it is a big trouble if printing PDF with drawing. Then, I find that it becomes easy through crystal report.

Now, I want to show a conclusion about how to create crystal report and convert to PDF to print.

Note: The report includes lots of data, images and paging.

  1. DataSet

    Create a new DataSet *.xcd in App_Code. Import a table to this DataSet from Server Explorer. Also, you can modify or add fields. If so, all the data which you want to display in one page is in this DataSet. Because crystal report can be displayed with many rows and groups, you can paste all the fields if you want to have many columns and rows. And the name can be displayed according to array, for example, name1, name2.
  2. Crystal Report

    Click Database fields in Field Manager and then choose one DataSet of ADO.NET for creating a rpt file. You can get the DataSet and put each field in appropriate place. Select table, right-click and choose insert OLE object. Then create from file. Then put in Section 3 (Detailed Material). Adjust fields to make them be under the table. After that, delete fields in Section 2.
  3. Convert Page to PDF. Add one button and copy the following code in method.

    ReportDocument rptdoc = new ReportDocument();
    rptdoc.Load(MapPath("rpt/YeWuChuLi.rpt"));//Crystal Report Path
    string filename = T_ProcessID.Text + ".pdf";//File Name of Generated PDF File
    string Fname = MapPath("rpt/" + filename);//PDF File Path
    rptdoc.SetDataSource(dtable);//Get data source. All the data can be read in SQL.
    DiskFileDestinationOptions crDiskFileDestinationOptions = new DiskFileDestinationOptions();

    crDiskFileDestinationOptions.DiskFileName = Fname;

    ExportOptions crExportOptions = rptdoc.ExportOptions;

    crExportOptions.ExportDestinationOptions = crDiskFileDestinationOptions;
    crExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
    crExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
    rptdoc.Export();//The above code can convert crystal report to PDF.
    Response.ClearContent();//The following code is used to get the converted PDF.
    Response.ContentType = "application/pdf";
    Response.WriteFile(@"rpt/" + filename);
    Response.Redirect(@"rpt/" + filename);

    Note: Add reference firstly.

    using CrystalDecisions.CrystalReports.Engine;
    using CrystalDecisions.Shared;

  4. Get Data Source.

    The simplest way is to assign SQL to DataTable. If one table includes another table, you need to get all the tables at first and then put them in different DataTable. For example, there are four tables in my table, table for Equipment Introduction, table for channel (information of 5 channels), table for valve (information of 3 channels), table for voltage regulator (information of 2 regulators). If the channels are more than 5, the data is displayed in next page.

    Because we cannot read data in rows from crystal report, so there is a method to convert columns to rows with SQL.

    Example: There is a table in database.

    Name sex birthday
    aa bb cc
    dd ee ff
    gg hh ii
    jj kk ll
    mm nn oo

    Convert like this:

    Name1 sex1 birthday1 Name2 sex2 birthday2 Name3 sex3 birthday3

    aa bb cc dd ee ff gg hh ii
    jj kk ll mm nn oo Null Null Null
    select (id-1)/3,
    Name1 = max(case when id%3 = 1 then Name else null end),
    Name1 = max (case when id%3 = 2 then Name else null end),
    Name1 = max (case when id%3 = 0 then Name else null end),
    Sex1 = max(case when id%3 = 1 then Type else null end),
    Sex2 = max (case when id%3 = 2 then Type else null end),
    Sex3 = max (case when id%3 = 0 then Type else null end),
    Birthday1 = max(case when id%3 = 1 then Quantity else null end),
    Birthday2 = max (case when id%3 = 2 then Quantity else null end),
    Birthday3 = max (case when id%3 = 0 then Quantity else null end),
    from (SELECT row_number() over(order by dbo.Table Name.Name) as id, table name.* FROM dbo.table name where ID='" + processid + "') tt group by (id-1)/3";
  5. Paging

    At first, choose the table what has the most columns. Then put all the data in one table according to the table.


    DataTable dtable = new DataTable;
    //#region Gas Equipment
    DataTable gasdevicedt = getgasdevicebyprocessid(T_ProcessID.Text);
    //#region Panalarm
    DataTable Alertdevicedt = GetAlertdeviceByProcessID(T_ProcessID.Text);
    //#region Gauges
    DataTable meadevicedt = getmeadevice(T_ProcessID.Text);
    //#region Regulator
    DataTable boosterdt = getboosterbyprocessid(T_ProcessID.Text);

    int k = table with most columns.Rows.Count;

    for (int i = 0; i <= k; i++)
    DataRow row = dtable.NewRow();
    row["ProjectName"] = T_ProjectName.Text;
    row["NodeEndTime"] = D_NodeEndTime.Text;

    row["NameA"] = gasdevicedt.Rows[i]["NameA"].ToString();
    row["NameB"] = gasdevicedt.Rows[i]["NameB"].ToString();
    row["NameC"] = gasdevicedt.Rows[i]["NameC"].ToString();
    row["NameD"] = gasdevicedt.Rows[i]["NameD"].ToString();
    row["NameE"] = gasdevicedt.Rows[i]["NameE"].ToString();

    row["AlertDeNameA"] = Alertdevicedt.Rows[i]["AlertDeNameA"].ToString();
    row["AlertDeNameB"] = Alertdevicedt.Rows[i]["AlertDeNameB"].ToString();
    row["AlertDeNameC"] = Alertdevicedt.Rows[i]["AlertDeNameC"].ToString();

    row["PipNameA"] = meadevicedt.Rows[i]["PipNameA"].ToString();
    row["PipNameB"] = meadevicedt.Rows[i]["PipNameA"].ToString();

    Then, we have data source of this table. And we can get the wanted PDF.

  6. Display Image in Crystal Report

    ReportDocument rptdoc = new ReportDocument();
    HandOverSet ds = _HandOver1.GetSingleHandOverByPHID(Convert.ToInt32(Request.QueryString["PHID"]));
    if (ds.HandOver.Rows[0]["PipelinePic"].ToString() == "")
    ds.HandOver.Rows[0]["PipelinePic"] = "nopic.gif";

    FileStream fs = new FileStream(MapPath("../images/pics/" +
    ds.HandOver.Rows[0]["PipelinePic"].ToString()), FileMode.Open); //Create File Stream
    BinaryReader br = new BinaryReader(fs);
    ds.HandOver.Rows[0]["img"] = br.ReadBytes((int)br.BaseStream.Length);
    crv.ReportSource = rptdoc;

    If ds is gotten when printing, all the fields are shown.

    The following fields are not displayed in PDF but in printer directly.

     System.Drawing.Printing.PrintDocument printDocument = new System.Drawing.Printing.PrintDocument();
     rptdoc.PrintOptions.PrinterName = printDocument.PrinterSettings.PrinterName;
     rptdoc.PrintToPrinter(1, true, 1, 1);

Finally, I want to introduce another article about converting office to PDF for you:

Hope helpful!

 [Top] Rate this article
 About the author
 Post a Feedback, Comment, or Question about this article

 � 2021  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.