using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
public static void SaveDataGridViewToPDF(DataGridView Dv, string FilePath)
{
FontFactory.RegisterDirectories();
iTextSharp.text.Font myfont = FontFactory.GetFont("Tahoma", BaseFont.IDENTITY_H, 12, iTextSharp.text.Font.NORMAL, iTextSharp.text.Color.BLACK);
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
pdfDoc.Open();
PdfWriter wri = PdfWriter.GetInstance(pdfDoc, new FileStream(FilePath, FileMode.Create));
pdfDoc.Open();
PdfPTable _mytable = new PdfPTable(Dv.ColumnCount);
for (int j = 0; j < Dv.Columns.Count; ++j)
{
Phrase p = new Phrase(Dv.Columns[j].HeaderText, myfont);
PdfPCell cell = new PdfPCell(p);
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
_mytable.AddCell(cell);
}
for (int i = 0; i < Dv.Rows.Count - 1; ++i)
{
for (int j = 0; j < Dv.Columns.Count; ++j)
{
Phrase p = new Phrase(Dv.Rows[i].Cells[j].Value.ToString(), myfont);
PdfPCell cell = new PdfPCell(p);
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
_mytable.AddCell(cell);
}
}
pdfDoc.Add(_mytable);
pdfDoc.Close();
System.Diagnostics.Process.Start(FilePath);
}
public static void SaveDataTableToPDF(System.Data.DataTable DTable, string FilePath)
{
FontFactory.RegisterDirectories();
iTextSharp.text.Font myfont = FontFactory.GetFont("Tahoma", BaseFont.IDENTITY_H, 12, iTextSharp.text.Font.NORMAL, iTextSharp.text.Color.BLACK);
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
pdfDoc.Open();
PdfWriter wri = PdfWriter.GetInstance(pdfDoc, new FileStream(FilePath, FileMode.Create));
pdfDoc.Open();
PdfPTable _mytable = new PdfPTable(DTable.Columns.Count);
for (int j = 0; j < DTable.Columns.Count; ++j)
{
Phrase p = new Phrase(DTable.Columns[j].ColumnName, myfont);
PdfPCell cell = new PdfPCell(p);
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
_mytable.AddCell(cell);
}
for (int i = 0; i < DTable.Rows.Count - 1; ++i)
{
for (int j = 0; j < DTable.Columns.Count; ++j)
{
Phrase p = new Phrase(DTable.Rows[i][j].ToString(), myfont);
PdfPCell cell = new PdfPCell(p);
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
_mytable.AddCell(cell);
}
}
pdfDoc.Add(_mytable);
pdfDoc.Close();
System.Diagnostics.Process.Start(FilePath);
}