C# 将数据写入PDF文档

首先添加引用:

using iTextSharp.text.pdf;
using iTextSharp.text;
         //列,虚拟一个Table用来测试
         DataTable dt = new DataTable();
            for (int i = 0; i < 5; i++)
            {
                dt.Columns.Add();
            }
            //
            for (int i = 0; i < 20; i++)
            {
                dt.Rows.Add(0, 1, 2, 3, 4);
            }


            this.dataGridView1.DataSource = dt;
 BaseFont bf = BaseFont.CreateFont("C:/WINDOWS/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            iTextSharp.text.Document document
                = new iTextSharp.text.Document();

            //通过IO来操作,以创建的方式保存在应用程序根目录。
            iTextSharp.text.pdf.PdfWriter.GetInstance(document,
                    new System.IO.FileStream("Demo123.pdf", System.IO.FileMode.Create)
                );
            document.Open();

            //载入字体,这里调用了PC本地的字体
            iTextSharp.text.Font font = new iTextSharp.text.Font(bf);

            document.Add(new Paragraph("Col1" + " " + "Col2" + "" + "Col3" + " " + "Col4" + " " + "Col5", font));
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                document.Add(new Paragraph(dataGridView1.Rows[i].Cells[0].Value.ToString() + " " + dataGridView1.Rows[i].Cells[1].Value.ToString()
                    + " " + dataGridView1.Rows[i].Cells[2].Value.ToString() + " " + dataGridView1.Rows[i].Cells[3].Value.ToString()
                    + " " + dataGridView1.Rows[i].Cells[4].Value.ToString(), font));
            }

            document.Close();

 

上一篇:Vert.x - Core EventBus


下一篇:【每日一题】【位于index后的双指针&排序数组】15. 三数之和/NC54 数组中相加和为0的三元组-211117/220206