C#PDFドキュメントにデータを書き込む

5279 ワード

最初に参照を追加:
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();