zoukankan      html  css  js  c++  java
  • C#导出成PDF文档

     /// <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);
                }     
            }

  • 相关阅读:
    51nod 1254 最大子段和 V2
    51nod 1115 最大M子段和 V3
    51nod 1053 最大M子段和 V2
    51nod 1052 最大M子段和
    51nod 1051 最大子矩阵和
    web.config或App.config中AttachDBFilenamex相对路径问题
    [转帖]unity3D OnTriggerEnter和OnCollisionEnter的一点个人心得(主要讲区别)
    unity3d 第一人称脚本解释MouseLook
    unity3d-游戏实战突出重围,整合游戏
    unity3d-游戏实战突出重围,第四天 添加角色
  • 原文地址:https://www.cnblogs.com/zhcnblog/p/2568533.html
Copyright © 2011-2022 走看看