After the datatable we need to follow the below code,
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)
{
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;
imageCell.Border = 0;
imageCell.HorizontalAlignment = 1;
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("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;
BO.DBPath = @"C:\Bala\Test Project.db";
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);
}
}
}
}