Pre-requisites

Please make sure you have completed the steps mentioned on How to extend Spring.Northwind to generate different Northwind reports?

Products by Category using Aspose.Pdf

Please follow the following steps to add Products by Category report using Aspose.Pdf

    1. Add sample "Northwind.xls" file in "Spring.Northwind.Web\App_Data\xls" folder. You can find this file in Download package/Source code of this project.
    2. Open "Web.xml" file on the root of "Spring.Northwind.Web" and add the following lines inside objects node
      
        <object type="Pdf/ProductsbyCategory.aspx">
          <property name="ProductDao" ref="ProductDao" />
          <property name="CategoryDao" ref="CategoryDao" />
        </object>
      
    3. Add a new folder named "Pdf" in Spring.Northwind.Web.2010
    4. Add a new ASP.NET page named "ProductsbyCategory.aspx" in Spring.Northwind.Web.2010\Pdf folder.
    5. Add a link to this "ProductsbyCategory.aspx" on "Default.aspx" so that you can access it when run the project in browser.
    6. Replace the html with
      
      
      <%@ Page Title="" Language="C#" MasterPageFile="~/Shared/MasterPage.master" AutoEventWireup="true" CodeBehind="ProductsbyCategory.aspx.cs" Inherits="Spring.Northwind.Web.Pdf.ProductsbyCategory" %>
      
      <asp:Content ID="Content1" ContentPlaceHolderID="content" runat="server">
      
          <h2>Create Products by Category report using Aspose.Pdf for .NET</h2>
          <div class="componentDescriptionTxt">
              This demo illustrates how to create a well formatted Products by Category report by using <a href="http://www.aspose.com/.net/pdf-component.aspx">Aspose.Pdf for .NET.</a>
              <ul class="noPaddingUl">
                  <li>Aspose.Pdf component gives you the agility to report your data in a variety of ways.</li>
                  <li>Aspose.Pdf component is fully functional for creating all types of reports.</li>
                  <li>You may customize the size and appearance of everything on a report. </li>
                  <li>You can display the information the way you want to see it.</li>
              </ul>
              The demo generates a printed report displaying products by category.
              <ul class="noPaddingUl">
                  <li>It has 3 columns per page and starts each category in a new column. </li>
                  <li>Spring.NET NHibernate is used to retrieve the data from the Products and Categories tables of Northwind database, to generate the report </li>
                  <li>You can either open the resulting PDF file into your PDF reader or save directly to your disk. </li>
              </ul>
              Click Process to see how example prints products by category. Output document will have 3 columns per page; each category will start in new column. 
          </div>
          <br />
          <asp:Button ID="btnProcess" runat="server" Text="Process" OnClick="btnProcess_Click" />
      
      </asp:Content>
      
      
      
    7. Replace the code-behind with
      
      
      using System;
      using System.ComponentModel;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using System.Web.UI;
      using System.Web.UI.WebControls;
      using Aspose.Pdf.Generator;
      using System.Data;
      using System.Data.OleDb;
      using Spring.Northwind.Dao;
      using Spring.Northwind.Domain;
      
      namespace Spring.Northwind.Web.Pdf
      {
          public partial class ProductsbyCategory : System.Web.UI.Page
          {
              private IProductDao productDao;
              private ICategoryDao categoryDao;
      
              public IProductDao ProductDao
              {
                  set { this.productDao = value; }
              }
      
              public ICategoryDao CategoryDao
              {
                  set { this.categoryDao = value; }
              }
      
              protected void Page_Load(object sender, EventArgs e)
              {
      
              }
      
              protected void btnProcess_Click(object sender, EventArgs e)
              {
                  Aspose.Pdf.Generator.Pdf pdf = GetProductsByCategory();
                  pdf.Save("productsbycategory.pdf", SaveType.OpenInAcrobat, Response);
                  Response.End();
              }
      
              public Aspose.Pdf.Generator.Pdf GetProductsByCategory()
              {
                  IList productsList = productDao.GetAll();
                  IList CategoryList = categoryDao.GetAll();
      
                  Aspose.Pdf.Generator.Pdf pdf = new Aspose.Pdf.Generator.Pdf();
                  pdf.IsTruetypeFontMapCached = false;
      
                  // If you have purchased a license,
                  // Set license like this: 
                  // string licenseFile = MapPath("License") + "\\Aspose.Pdf.lic"; 
                  // Aspose.Pdf.License lic = new Aspose.Pdf.License();
                  // lic.SetLicense(licenseFile);
      
                  string xmlFile = Server.MapPath("~/App_Data/xml/ProductsByCategory.xml");
                  pdf.BindXML(xmlFile, null);
      
                  Section section = pdf.Sections["section1"];
      
                  Aspose.Pdf.Generator.Table table1 = new Aspose.Pdf.Generator.Table(section);
                  section.Paragraphs.Add(table1);
                  table1.ColumnWidths = "314 314 314";
                  table1.Border = new BorderInfo((int)BorderSide.Top, 4, new Aspose.Pdf.Generator.Color(204));
                  table1.IsRowBroken = false;
                  table1.DefaultCellPadding.Top = table1.DefaultCellPadding.Bottom = 15;
                  table1.DefaultCellTextInfo.FontSize = 14;
                  table1.Margin.Top = 10;
      
                  int j;
                  Row curRow = null;
                  Cell curCell = null;
                  Aspose.Pdf.Generator.Table curSubTab = null;
                  string[] names = new string[]
                      {"Catagory: Beverages","Catagory: Condiments","Catagory: Confections",
                       "Catagory: Dairy Products","Catagory: Grains/Cereals","Catagory: Meat/Poultry",
                       "Catagory: Produce","Catagory: Seafood"};
      
                  for (j = 1; j <= 8; j++)
                  {
                      if (j == 1 || j == 4 || j == 7)
                          curRow = table1.Rows.Add();
                      curCell = curRow.Cells.Add();
                      curSubTab = new Aspose.Pdf.Generator.Table(curCell);
                      curSubTab.DefaultCellPadding.Top = curSubTab.DefaultCellPadding.Bottom = 3;
                      curCell.Paragraphs.Add(curSubTab);
                      curSubTab.ColumnWidths = "214 90";
      
                      Row row0 = curSubTab.Rows.Add();
                      Aspose.Pdf.Generator.TextInfo tf1 = new Aspose.Pdf.Generator.TextInfo();
                      tf1.FontSize = 16;
                      tf1.FontName = "Times-Bold";
      
                      row0.Cells.Add(names[j - 1], tf1);
      
                      IList filteredProductsList = (from productsTable in productsList
                                                             where (productsTable.Discontinued == false) && (productsTable.CategoryID == j)
                                                             orderby productsTable.ProductName
                                                             select productsTable).ToList();
      
                      DataTable dataTable1 = ConvertToDataTable(filteredProductsList);
      
                      curSubTab.ImportDataTable(dataTable1, true, 1, 0);
      
                      curSubTab.Rows[1].Border = new BorderInfo((int)(BorderSide.Top | BorderSide.Bottom), 4, new Aspose.Pdf.Generator.Color(204));
      
                      Row lastRow = curSubTab.Rows[curSubTab.Rows.Count - 1];
                      foreach (Cell cCell in lastRow.Cells)
                          cCell.Padding.Bottom = 20;
      
                      lastRow = curSubTab.Rows.Add();
                      lastRow.Cells.Add("number of products:");
                      lastRow.Cells.Add(dataTable1.Rows.Count.ToString());
                      lastRow.Border = new BorderInfo((int)BorderSide.Top, new Aspose.Pdf.Generator.Color(204));
                  }
      
                  curRow.Cells.Add();
                  return pdf;
              }
      
              public DataTable ConvertToDataTable(IList data)
              {
                  PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
                  DataTable table = new DataTable();
      
                  //foreach (PropertyDescriptor prop in properties)
                  //    table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
                  //foreach (T item in data)
                  //{
                  //    DataRow row = table.NewRow();
                  //    foreach (PropertyDescriptor prop in properties)
                  //        row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
                  //    table.Rows.Add(row);
                  //}
      
                  // Manual table conversion
                  table.Columns.Add("ProductName", typeof(string));
                  table.Columns.Add("UnitsInStock", typeof(short));
      
                  foreach (T item in data)
                  {
                      DataRow row = table.NewRow();
                      foreach (PropertyDescriptor prop in properties)
                      {
                          if (prop.Name.Equals("ProductName") || prop.Name.Equals("UnitsInStock"))
                              row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
                      }
                      table.Rows.Add(row);
                  }
      
                  return table;
      
              }
          }
      }
      
      
    8. Run the project using F5 and open ProductsbyCategory.aspx page. Click 'Process' button to generate the report.

Last edited Feb 28, 2014 at 2:05 PM by asposemarketplace, version 3