首先导入itextsharp.dll(及其依赖)到Plugins文件夹。
其次,看下面代码。
一个写入表格的例子,供学习参考使用:
using System.Collections.Generic;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public static class WritePDFHelper
{
public static void WritePDF(string filePath, string titleFrontPath, string textFrontPath, string title, List<string[][]> tableContent)
{
Document document = new Document();
try
{
PdfWriter.GetInstance(document, new FileStream(filePath, FileMode.Create));
document.Open();
BaseFont bf_Title = BaseFont.CreateFont(titleFrontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
iTextSharp.text.Font font_Title = new iTextSharp.text.Font(bf_Title, 20);
BaseFont bf_Txt = BaseFont.CreateFont(textFrontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
iTextSharp.text.Font font_Txt = new iTextSharp.text.Font(bf_Txt, 12);
Paragraph paragraph_title = new Paragraph(title, font_Title);
paragraph_title.Alignment = Rectangle.ALIGN_CENTER;
document.Add(paragraph_title);
Paragraph nullp = new Paragraph(" ", font_Title);
nullp.Leading = 10;
for (int i = 0; i < tableContent.Count; i++)
{
document.Add(nullp);
string[][] piece = tableContent[i];
PdfPTable piece_Table = new PdfPTable(piece[0].Length);
for (int j = 0; j < piece.Length; j++)
{
for (int k = 0; k < piece[j].Length; k++)
{
piece_Table.AddCell(new PdfPCell(new Phrase(piece[j][k], font_Txt)));
}
}
document.Add(piece_Table);
}
document.Close();
}
catch (DocumentException de)
{
throw de;
}
catch (IOException io)
{
throw io;
}
}
}
|