Click here to Skip to main content
16,012,116 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I'm Working On Export to Pdf i need to execute three tables in a single pdf.After first table in a new page second table should start as well as third table.I have created a datatable and dataset only one table is getting executed.

Regards
Balamurugan
Posted

1 solution

After the datatable we need to follow the below code,

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using BObject;
using BusinessLogic;
using System.Threading;
using System.Data;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
namespace MultipleTable
{
    
    public partial class ExportAll : Window
    {
        PIGbuslogic BL = new PIGbuslogic();
        PIGBObject BO = new PIGBObject();
        DataSet dsAllReportsData = new DataSet();

        private ProgressbarImage pbw = null;
        public delegate void OnWorkerMethodCompleteDelegate(string message);
        public event OnWorkerMethodCompleteDelegate OnWorkerComplete;
        public delegate void poplateLabelDelegate();

        public ExportAll()
        {
            InitializeComponent();
            OnWorkerMethodStart();
        }
        private void OnWorkerMethodStart()
        {
            OnWorkerComplete += new OnWorkerMethodCompleteDelegate(OnWorkerMethodComplete);
            ThreadStart tStart = new ThreadStart(WorkerMethod);
            Thread t = new Thread(tStart);
            t.Start();

            pbw = new ProgressbarImage();
            pbw.ShowInTaskbar = false;
            pbw.Owner = this.Owner;
            pbw.ShowDialog();

        }
        private void OnWorkerMethodComplete(string message)
        {
            pbw.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
            new Action(
            delegate()
            {
                pbw.Close();
            }
            ));
            pbw.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
            new Action(
            delegate()
            {
                Global.customCaption = "PDF Export";
                Global.customMessage = "PDF generated successfully..";
                CustomMessageBox CMB = new CustomMessageBox();
                CMB.ShowInTaskbar = false;
                CMB.ShowDialog();
            }
            ));
        }
        public void BindDatatoPDF(DataSet dsAllReportsData)
        {
            //Joint List Data
            try
            {
                if (dsAllReportsData.Tables.Count > 0)
                {
                    if (dsAllReportsData.Tables[0].Rows.Count > 0)
                    {
                        int pdfRowIndex = 1;
                        Document document = new Document(PageSize.LETTER);
                        PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("C://Bala//Saibabapdf.pdf", FileMode.Create));
                        document.Open();
                        iTextSharp.text.Font font5 = iTextSharp.text.FontFactory.GetFont(FontFactory.HELVETICA, 7);
                        float[] columnDefinitionSize = { 10F, 15F, 10F, 10F, 10F, 10F, 8F, 8F, 9F, 10F };
                        PdfPTable table = null;
                        PdfPCell cell = null;
                        table = new PdfPTable(columnDefinitionSize);
                        table.WidthPercentage = 100;
                        cell = new PdfPCell(new Phrase("DefectNumber" + " AnomalyTypeName" + "Length" + "Width" + "Depth" + " Distance" + "Latitude" + "Longitude" + "Orientation" + "Comments"));
                        PdfPCell imageCell = new PdfPCell(new Phrase("Joint List Data"));
                        imageCell.Colspan = 10; // either 1 if you need to insert one cell
                        imageCell.Border = 0;
                        imageCell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
                        //imageCell.FixedHeight = 0.3f;
                        table.AddCell(imageCell);
                        PdfPCell CellActext = new PdfPCell(new Phrase(new Chunk("DefectNumber", FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.BOLD, iTextSharp.text.Color.WHITE))));
                        CellActext.Colspan = 1;
                        CellActext.Border = 0;
                        CellActext.HorizontalAlignment = Element.ALIGN_LEFT;
                        CellActext.BackgroundColor = new iTextSharp.text.Color(24, 150, 191);
                        table.AddCell(CellActext);
                        //table.AddCell(new Phrase("DefectNumber", font5));
                        table.AddCell(new Phrase("AnomalyTypeName", font5));
                        table.AddCell(new Phrase("Length", font5));
                        table.AddCell(new Phrase("Width", font5));
                        table.AddCell(new Phrase("Depth", font5));
                        table.AddCell(new Phrase("Distance", font5));
                        table.AddCell(new Phrase("Latitude", font5));
                        table.AddCell(new Phrase("Longitude", font5));
                        table.AddCell(new Phrase("Orientation", font5));
                        table.AddCell(new Phrase("Comments", font5));
                        pdfRowIndex++;
                        for (int i = 0; i < dsAllReportsData.Tables[0].Rows.Count; i++)
                        {
                            table.AddCell(new Phrase(dsAllReportsData.Tables[0].Rows[i]["DefectNumber"].ToString(), font5));
                            table.AddCell(new Phrase(dsAllReportsData.Tables[0].Rows[i]["AnomalyTypeName"].ToString(), font5));
                            table.AddCell(new Phrase(dsAllReportsData.Tables[0].Rows[i]["Length"].ToString(), font5));
                            table.AddCell(new Phrase(dsAllReportsData.Tables[0].Rows[i]["Width"].ToString(), font5));
                            table.AddCell(new Phrase(dsAllReportsData.Tables[0].Rows[i]["Depth"].ToString(), font5));
                            table.AddCell(new Phrase(dsAllReportsData.Tables[0].Rows[i]["Distance"].ToString(), font5));
                            table.AddCell(new Phrase(dsAllReportsData.Tables[0].Rows[i]["Latitude"].ToString(), font5));
                            table.AddCell(new Phrase(dsAllReportsData.Tables[0].Rows[i]["Longitude"].ToString(), font5));
                            table.AddCell(new Phrase(dsAllReportsData.Tables[0].Rows[i]["Orientation"].ToString(), font5));
                            table.AddCell(new Phrase(dsAllReportsData.Tables[0].Rows[i]["Comments"].ToString(), font5));
                            pdfRowIndex++;

                        }
                        PdfPCell CellABC = new PdfPCell();
                        CellABC.AddElement(table);
                        CellABC.HorizontalAlignment = Element.ALIGN_CENTER;
                        CellABC.Border = 0;
                        CellABC.Colspan = 1;
                        table.AddCell(CellABC);
                        document.Add(table);
                        document.Add(Chunk.NEXTPAGE);
                        document.Add(table);
                        document.Add(Chunk.NEXTPAGE);
                        document.Add(table);
                        document.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                Global.customCaption = "PDF Export";
                Global.customMessage = "Error while generarting PDF..";
                CustomMessageBox CMB = new CustomMessageBox();
                CMB.ShowInTaskbar = false;
                CMB.ShowDialog();
            }

        }
       

        public void WorkerMethod()
        {
            try
            {
                BO.RunId = 1;//Convert.ToInt32(Global.RunOpend);
                //BO.DBPath = @"E:\Working Code\Project_August19\Project_Old\Project\bin\Debug\DB\Test Project.db";//Global.DBPath;
                BO.DBPath = @"C:\Bala\Test Project.db";//Global.DBPath;
                dsAllReportsData = BL.GetAllReportsData(BO);
                BindDatatoPDF(dsAllReportsData);
                OnWorkerComplete("Project Created Successfully");
            }
            catch (Exception ex)
            {
                pbw.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                new Action(
                 delegate()
                 {
                     pbw.Close();
                 }))
                ;

                Global.customMessage = "Please Try Again"; DataTable pdftbl = new DataTable();
                DataColumn c;
                DataRow r;
                Global.customCaption = "Alert";
                Thread.Sleep(1000);
            }
        }
    }
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900