最近项目中需要导出PDF文件,最后上网搜索了一下,发现ITextSharp比较好用,所以做了一个例子:
public string ExportPDF()
{
//ITextSharp Usage
//Steps:1. Add content to cell;2. Add cell to table;3. Add table to document;4. Add document to rectangle;
string sAbsolutePath = ControllerContext.HttpContext.Server.MapPath(this.path);
string FileName = string.Format("Notice_{0}.pdf", DateTime.Now.ToString("yyyyMMddHHmmss"));
BaseFont bf = BaseFont.CreateFont("C:/WINDOWS/Fonts/Arial.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Rectangle rec = new Rectangle(, );
Document document = new Document(rec);
document.SetMargins(10f, 10f, 10f, 10f);
PdfWriter pdfwriter = PdfWriter.GetInstance(document,
new System.IO.FileStream(sAbsolutePath + "\\" + FileName, System.IO.FileMode.Create)
); document.Open();
Font font = new Font(bf); try
{
PdfPTable pdftable = new PdfPTable();
pdftable.HorizontalAlignment = ; //Set cell width
float[] cellwidth = { 3f, 5f, 5f, 3.5f, 5f };
pdftable.SetWidths(cellwidth); //Header
Font PDFFontA = FontFactory.GetFont("Arial", , Font.BOLD);
Font PDFFontB = FontFactory.GetFont("Arial", , Font.BOLD);
Font PDFFontC = FontFactory.GetFont("Arial", ); //Image Object
Image jpeg = Image.GetInstance(HttpContext.Server.MapPath("../images/buddy.jpg"));
PdfPCell cell = new PdfPCell(jpeg);
cell.FixedHeight = 40f;
cell.Colspan = ;
cell.Rowspan = ;
cell.Border = ;
pdftable.AddCell(cell); cell = new PdfPCell(new Phrase(("Did You Know?"), PDFFontB));
cell.Colspan = ;
cell.Rowspan = ;
cell.Border = ;
cell.BackgroundColor = new BaseColor(, , );//RGB Color Value
cell.HorizontalAlignment = ;
pdftable.AddCell(cell); cell = new PdfPCell(new Phrase("\nTitle: Hello World!\nDate: " + DateTime.Now.ToString("MM/dd/yyyy") + "\n\n", PDFFontC));
cell.Colspan = ;
cell.Rowspan = ;
cell.Border = ;
cell.BackgroundColor = new BaseColor(, , );//RGB Color Value
cell.HorizontalAlignment = ;
pdftable.AddCell(cell); cell = new PdfPCell(new Phrase("Notice", PDFFontA));
cell.BackgroundColor = new BaseColor(, , );//RGB Color Value
cell.Colspan = ;
cell.Padding = 5f;
cell.HorizontalAlignment = ;
pdftable.AddCell(cell); string[] AssHeader = { "DateTime", "Name", "Description", "Icon", "Notes"};
for (int i = ; i < AssHeader.Length; i++)
{
cell = new PdfPCell(new Phrase(AssHeader[i], PDFFontB));
cell.HorizontalAlignment = ;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
cell.BackgroundColor = new BaseColor(, , );
cell.PaddingBottom = 4f;
cell.PaddingTop = 4f;
pdftable.AddCell(cell);
}
for (int i = ; i < ; i++)
{
cell = new PdfPCell(new Phrase(new DateTime().ToShortDateString(), PDFFontC));
cell.HorizontalAlignment = ;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
cell.PaddingBottom = 4f;
cell.PaddingTop = 4f;
pdftable.AddCell(cell); cell = new PdfPCell(new Phrase("Jack Chean", PDFFontC));
cell.HorizontalAlignment = ;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
cell.PaddingBottom = 4f;
cell.PaddingTop = 4f;
pdftable.AddCell(cell); cell = new PdfPCell(new Phrase("Just for my testing!!", PDFFontC));
cell.HorizontalAlignment = ;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
cell.PaddingBottom = 4f;
cell.PaddingTop = 4f;
pdftable.AddCell(cell); cell = new PdfPCell(new Phrase("ICON", PDFFontC));
cell.HorizontalAlignment = ;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
cell.PaddingBottom = 4f;
cell.PaddingTop = 4f;
pdftable.AddCell(cell); cell = new PdfPCell(new Phrase("For Tom!", PDFFontC));
cell.HorizontalAlignment = ;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
cell.PaddingBottom = 4f;
cell.PaddingTop = 4f;
pdftable.AddCell(cell);
}
//Very important
//You can display the header in each page through this properity:HeaderRows
pdftable.HeaderRows = ; document.Add(pdftable);
//Pager
float tableheight = pdftable.CalculateHeights();
if (tableheight < && tableheight > )
{
document.NewPage();
} document.NewPage();
document.Close();
}
catch (Exception ex)
{
document.Add(new Paragraph(ex.Message.ToString(), font));
}
return FileName;
}
最后的显示效果:
参考文章:http://blog.csdn.net/adgjlxxx/article/details/43307227