Friday, 19 April 2013

Infragistics Report Using c# Code(Sample Example)



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using CTSERPSystem.BAL;
using Infragistics.Documents.Reports.Graphics;
using Infragistics.Documents.Reports.Report;
using Infragistics.Documents.Reports.Report.Table;
using Infragistics.Documents.Reports.Report.Section;
using Infragistics.Documents.Reports.Report.Shapes;
using Infragistics.Documents.Reports.Report.Text;
using CTSERPSystem.Entity;
using System.IO;
using Infragistics.Documents.Reports.Report.Band;


namespace CTSERPSystem
{
    public partial class TestForm : Form
    {
        public TestForm()
        {
            InitializeComponent();
        }

        private void TestForm_Load(object sender, EventArgs e)
        {

        }

        private void submitButton_Click(object sender, EventArgs e)
        {
            IList<ECRIssue> ecrIssues = new List<ECRIssue>();

            foreach (var ecrIssue in new ECRIssueManager().GetECRIssues())//retrieve datas from database,use ur own way  
                ecrIssues.Add(ecrIssue);
            CreateReport(ecrIssues, new string[] { "Name", "Requested By", "Issue Response", "Status", "Visible To Supplier", "Created By Id", "Created Time", "Last Modified Time", "Last Modified By Id" });//Title of the headrs);
            //CreateReport(ecrIssues, new string[] { "Name", "Requested By", "Issue Response", "Status", "Visible To Supplier", "Created By", "Created Time", "Last Modified Time", "Last Modified By" });//Title of the headrs
        }

        public static System.Drawing.Image ResizeImage(System.Drawing.Image sourceImage, System.Drawing.Size size)
        {
            decimal sourceImageHeight = sourceImage.Height, sourceImageWidth = sourceImage.Width, resizedImageHeight, resizedImageWidth;

            if (sourceImageHeight > sourceImageWidth)
            {
                if (sourceImageHeight > size.Height)
                    resizedImageHeight = size.Height;
                else
                    resizedImageHeight = sourceImageHeight;
                resizedImageWidth = (resizedImageHeight / sourceImageHeight) * sourceImageWidth;
            }
            else
            {
                if (sourceImageWidth > size.Width)
                    resizedImageWidth = size.Width;
                else
                    resizedImageWidth = sourceImageWidth;
                resizedImageHeight = (resizedImageWidth / sourceImageWidth) * sourceImageHeight;
            }
            System.Drawing.Image resizedImage = sourceImage.GetThumbnailImage(Convert.ToInt32(resizedImageWidth), Convert.ToInt32(resizedImageHeight), null, IntPtr.Zero);

            return resizedImage;
        }

        private void CreateReport(IList<ECRIssue> ecrIssues, string[] columnName)
        {
            Report report = new Report();
            ISection section = report.AddSection();

            //section.AddQuickText("MyHeader").Font.DoubleUnderline = true; ;
            IBand reportBand = section.AddBand();
            reportBand.Paddings.Bottom = 1;
            var reportImage = reportBand.AddImage(new Infragistics.Documents.Reports.Graphics.Image(ResizeImage(System.Drawing.Image.FromFile(@"Images\CTSLogo.jpg"), new System.Drawing.Size(100, 50))));
            reportImage.KeepRatio = true;
            //
            // Create a title for the Section.
            IText textForSection = section.AddText();
            textForSection.Margins.Bottom = 1;
            textForSection.Margins.Right = 10;
            textForSection.Alignment = TextAlignment.Right;
            textForSection.Style = new Infragistics.Documents.Reports.Report.Text.Style(Infragistics.Documents.Reports.Graphics.Fonts.ArialBold, Infragistics.Documents.Reports.Graphics.Brushes.Black);
            textForSection.AddContent("ECR Issues");


            //
            section.PageSize = new PageSize(800, 600);
            section.PagePaddings.Top = 10;
            section.PagePaddings.Left = 10;
            section.PagePaddings.Right = 10;
            section.PagePaddings.Bottom = 25;

            Infragistics.Documents.Reports.Report.Table.ITable table = section.AddTable();
            // Create a new pattern for the cells.
            Infragistics.Documents.Reports.Report.Table.TableCellPattern tableCellPattern =
              new Infragistics.Documents.Reports.Report.Table.TableCellPattern();
            tableCellPattern.Borders = new Borders(new Infragistics.Documents.Reports.Graphics.Pen(Infragistics.Documents.Reports.Graphics.Colors.Black));
            tableCellPattern.Background = new Background(Infragistics.Documents.Reports.Graphics.Brushes.White);
            tableCellPattern.Paddings = new Paddings(5, 10);
          
            table.Header.Repeat = true;           
            ITableCell theHeaderCell = null;          
            for (int titleOfTheHeader = 0; titleOfTheHeader < columnName.Length; titleOfTheHeader++)// cell name for header
            {
                theHeaderCell = table.Header.AddCell();
                theHeaderCell.Width = new RelativeWidth(150);
                tableCellPattern.Apply(theHeaderCell);
                theHeaderCell.Background = new Background(Infragistics.Documents.Reports.Graphics.Brushes.White);
                IText tableCellTextForHeader = theHeaderCell.AddText();
                tableCellTextForHeader.Alignment =
                          new TextAlignment(Alignment.Center, Alignment.Middle);
                tableCellTextForHeader.Style.Font.Bold = true;
                //stationeryText.Style = New Style(New Font("Verdana", 72), Brushes.Silver)
                tableCellTextForHeader.Style = new Style(new Infragistics.Documents.Reports.Graphics.Font("Arial Rounded MT Bold", 11), Infragistics.Documents.Reports.Graphics.Brushes.Black);
                tableCellTextForHeader.AddContent("" + columnName[titleOfTheHeader] + "");              
            }

            Infragistics.Documents.Reports.Report.Table.ITableRow tableRow;
            Infragistics.Documents.Reports.Report.Table.ITableCell tableCell;

            for (int row = 0; row < ecrIssues.Count; row++)
            {
                tableRow = table.AddRow();
                for (int column = 0; column < columnName.Length; column++)
                {
                    tableCell = tableRow.AddCell();
                    tableCell.Width = new RelativeWidth(150);//150 length for Issue Response              
                    tableCellPattern.Apply(tableCell);
                    tableCell.Background = new Background(Infragistics.Documents.Reports.Graphics.Brushes.White);
                    IText tableCellText = tableCell.AddText();
                    tableCellText.Style.Font.Bold = false;
                    tableCellText.Alignment =
                      new TextAlignment(Alignment.Center, Alignment.Middle);
                    if (column == 0)//Name
                        tableCellText.AddContent("" + ecrIssues.ElementAt(row).Name + "");
                    else if (column == 1)//Requested By
                        tableCellText.AddContent("" + new ECRManager().GetEcr(ecrIssues.ElementAt(row).RequestId).Name + "");
                    else if (column == 2)//Issue Response
                        tableCellText.AddContent("" + ecrIssues.ElementAt(row).IssueResponse + "");
                    else if (column == 3)//Status
                        tableCellText.AddContent("" + ((StatusTypes)ecrIssues.ElementAt(row).StatusId).ToString() + "");//((EnumDisplayStatus)value)
                    else if (column == 4)//Visible To Supplier
                    {
                        if (ecrIssues.ElementAt(row).IsVisibleToSupplier)
                            tableCellText.AddContent("Yes");
                        else tableCellText.AddContent("No");
                    }
                    else if (column == 5)//Created by user
                        tableCellText.AddContent("" + ecrIssues.ElementAt(row).CreatedByUserId + "");
                    else if (column == 6)//Created date
                        tableCellText.AddContent("" + ecrIssues.ElementAt(row).CreatedByDateTime + "");
                    else if (column == 7)//Last Modified time
                        tableCellText.AddContent("" + ecrIssues.ElementAt(row).LastModifiedDateTime + "");
                    else if (column == 8)//Last Modified user
                        tableCellText.AddContent("" + ecrIssues.ElementAt(row).LastModifiedUserId + "");
                }
            }
            // Get a reference to the section's PageNumbering object.
            Infragistics.Documents.Reports.Report.Section.PageNumbering pageNumbering = section.PageNumbering;
            // Create a style for the page numbering font.
            pageNumbering.Style = new Infragistics.Documents.Reports.Report.Text.Style(Fonts.Arial, Infragistics.Documents.Reports.Graphics.Brushes.Black);
            /* The Template property is the actual string that shows the page numbering. Use the [Page #] place-
             holder for the current page and the [TotalPages] place-holder for the total amount of pages in the entire document.*/
            pageNumbering.Template = "Page [Page #] of [TotalPages]";
            /* Setting SkipFirst to true does not place page numbering on the first page of the section. This
            is useful if the first page is a Title page.*/
            pageNumbering.SkipFirst = false;
            /* The page numbering will be aligned with the right side of the page. Valid values off the Alignment enum include Left, Center, and Right.*/
            pageNumbering.Alignment.Horizontal = Infragistics.Documents.Reports.Report.Alignment.Right;
            /* The page numbering will be located at the bottom of the page. Valid values off the lignment enum include Top and Bottom.*/
            pageNumbering.Alignment.Vertical = Infragistics.Documents.Reports.Report.Alignment.Bottom;
            /*The page numbering is at the extreme bottom of the page, so we need to change the Y Offset
             in order to bring it in line with the rest of the page footer text.*/
            pageNumbering.OffsetY = -18;
            ISectionFooter ftr = section.AddFooter();
            ftr.Height = 10;
            IText text = ftr.AddText(5, -18);
            text.AddContent(string.Format("    Date printed: {0}", DateTime.Now.ToString("dd-MMM-yyyy hh:mmtt")));

            string myDocuments = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
            report.Publish(myDocuments + "\\Report.pdf", FileFormat.PDF);
            System.Diagnostics.Process.Start(myDocuments + "\\Report.pdf");
        }

    }
}

No comments:

Post a Comment