.net 生成pdf表格

时间:2022-10-16 21:19:38

只需要建一个类文件就搞定了

 public class CreatePDF
{
public static CreatePDF Current { get { return new CreatePDF(); } }
public void CreatePDFs(RBS.Models.UserConfirmModel Model)
{
Document document = new Document();
string filepath = "/Upload/Pdf/";
Directory.CreateDirectory(HttpContext.Current.Server.MapPath(filepath));
PdfWriter.GetInstance(document, new FileStream(HttpContext.Current.Server.MapPath(filepath + "001.pdf"), FileMode.Create));
document.Open();
BaseFont bftitle = BaseFont.CreateFont(@"C:\\Windows\Fonts\SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font fonttitle = new Font(bftitle, , Font.UNDERLINE);
BaseFont bf1 = BaseFont.CreateFont(@"C:\\Windows\Fonts\SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font font1 = new Font(bf1, );
Font fonttitle10 = new Font(bf1, );
Font fonttitle12 = new Font(bf1, );
PdfPCell cellmode = new PdfPCell(); cellmode.Padding = ;
Paragraph Title = new Paragraph("信息总表", fonttitle);
Title.SetAlignment("center");
document.Add(Title);
PdfPTable topTable = new PdfPTable();
PdfPCell topCell = new PdfPCell();
topCell.PaddingTop = 10F;
topCell.Colspan = ;
topCell.Border = ;
topTable.AddCell(topCell);
document.Add(topTable);
PdfPTable table = new PdfPTable();
CreateCell(, false, "广州XXXXXX公司", fonttitle10, "left", table);
CreateCell(, false, "字第190898765号", fonttitle10, "right", table);
CreateCell(, , , "兹批准  广州市XXXXXXX分公司 去购买下列物品", fonttitle10, "center", table);
CreateCell(, true, "物品名称", fonttitle10, "center", table);
CreateCell(, true, "数量", fonttitle10, "center", table);
CreateCell(, true, "备注", fonttitle10, "center", table);
string[] arrNames = { "物品名称1", " 物品名称2", " ", " " };
string[] arrCounts = { "10吨", "800公斤", "", "" };
for (int i = ; i < arrNames.Length; i++)
{
CreateCell(, , , arrNames[i], fonttitle10, "center", table);
CreateCell(, , , arrCounts[i], fonttitle10, "center", table);
CreateCell(, , , " ", fonttitle10, "center", table);
}
CreateCell(, false, " 此证由填发日起止2013年03月19日有效 逾期作废", fonttitle10, "left", table);
CreateCell(, false, " 到本县、市以外购买物品时,必须经出售地*机关在备注栏内盖章后方能有效", fonttitle10, "left", table);
CreateCell(, , , " \r填发人:XXX", fonttitle10, "left", table);
CreateCell(, false, "广东省广州市\r\n2013年3月5日", fonttitle10, "right", table); document.Add(table);
document.Close();
} ///
/// 生成单元格
///
/// 合并列数
/// 是否需要边框线
/// 表格内显示的内容
/// 内容字体
/// 对齐方式
/// 此单元格填充的表
private void CreateCell(int Colspan, bool Border, string Content, Font font, string alignment, PdfPTable table)
{
if (Border)
CreateCell(Colspan, , , Content, font, alignment, table);
else
CreateCell(Colspan, , Content, font, alignment, table);
}
///
/// 生成单元格
///
/// 合并列数
/// 间距
/// 内容
/// 字体
/// 对齐方式
/// 此单元格填充的表
private void CreateCell(int Colspan, int Padding, string Content, Font font, string alignment, PdfPTable table)
{
CreateCell(Colspan, Padding, , Content, font, alignment, table);
}
///
/// 生成单元格
///
/// 合并列数
/// 间距
/// 边框线
/// 内容
/// 字体
/// 对齐方式
/// 此单元格填充的表
private void CreateCell(int Colspan, int Padding, int Border, string Content, Font font, string alignment, PdfPTable table)
{
CreateCell(Colspan, Padding, Border, , , Content, font, alignment, table);
}
///
/// 生成单元格
///
/// 合并列数
/// 间距
/// 边框线
/// 水平对齐方式
/// 垂直对齐方式
/// 内容
/// 字体
/// 对齐方式
/// 此单元格填充的表
private void CreateCell(int Colspan, int Padding, int Border, int HorizontalAlignment, int VerticalAlignment, string Content, Font font, string alignment, PdfPTable table)
{
PdfPCell cell = new PdfPCell();
cell.Colspan = Colspan;
cell.Padding = Padding;
if (HorizontalAlignment > )
cell.HorizontalAlignment = HorizontalAlignment;
if (VerticalAlignment > )
cell.VerticalAlignment = VerticalAlignment;
if (Border == )
cell.Border = Border;
Paragraph table_t = new Paragraph(Content, font);
table_t.SetAlignment(alignment);
cell.AddElement(table_t);
table.AddCell(cell);
} }