/// <summary>
/// 导出PDF
/// </summary>
/// <param name="fileName">要保存的文件名</param>
/// <param name="MyDGV">表名</param>
/// <param name="PdfSavePath">保存路径</param>
/// <param name="PicPath">选择的图片路径</param>
private void ExportPDF(string fileName, DataGridView MyDGV, string PdfSavePath, string PicPath)
{
try
{
//MessageBox.Show("点击 确定 ,请等待……", "文件正在导出……", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
Document document = new Document(PageSize.A4.Rotate());
//PdfWriter.GetInstance(document, new FileStream(Application.StartupPath + fileName, FileMode.Create));
PdfWriter.GetInstance(document, new FileStream(PdfSavePath, FileMode.Create));
document.Open();
BaseFont bfChinese = BaseFont.CreateFont("C://windows//fonts//simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
iTextSharp.text.Font fontChinese = new iTextSharp.text.Font(bfChinese, 12, iTextSharp.text.Font.NORMAL, new iTextSharp.text.Color(0, 0, 0));
//导出label或文本框
document.Add(new Paragraph(this.label1.Text.ToString()+this.textBox1.Text, fontChinese));
document.Add(new Paragraph(this.label2.Text+this.textBox2.Text, fontChinese));
//导出图片
//iTextSharp.text.Image jpeg = iTextSharp.text.Image.GetInstance(Path.GetFullPath("log1.jpg"));
iTextSharp.text.Image jpeg = iTextSharp.text.Image.GetInstance(PicPath);
document.Add(jpeg);
//导出表格
PdfPTable table = new PdfPTable(MyDGV.Columns.Count);
for (int i = 0; i < MyDGV.ColumnCount; i++)
{
table.AddCell(new Phrase(MyDGV.Columns[i].HeaderText, fontChinese)); //表头
}
for (int i = 0; i < MyDGV.Rows.Count - 1; i++) //表中的数据
{
for (int j = 0; j < MyDGV.Columns.Count; j++)
{
string S;
if (MyDGV.Rows[i].Cells[j].Value == null)
{
S = "";
}
else
{
S = MyDGV.Rows[i].Cells[j].Value.ToString();
}
table.AddCell(new Phrase(S, fontChinese));
}
}
document.Add(table);
////打开导出文件
//DialogResult res = MessageBox.Show("已成功导出到:" + "\r\n" + "xxx//bin//debug//" + "\r\n" + "是否现在查看?", "提示是否打开文件", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
document.Close();
//if (res == DialogResult.Yes)
//{
// //默认用windows来打开文件
// System.Diagnostics.Process.Start(Application.StartupPath + @"/ssssss.pdf");
//}
}
catch (Exception ex)
{
MessageBox.Show("出错 " + ex);
}
}