iTextSharp pdf demo 学习总结

PDF

using iTextSharp.text;
using iTextSharp.text.pdf;

Document document = new Document();
PdfWriter writer = null;
int titleSize = 13;
int contentSize = 8;
Font font= new Font(baseFont, contentSize);

//添加字体,支持pdf中文显示:
BaseFont baseFont = BaseFont.CreateFont(@"C:\Windows\Fonts\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(string.Format("{0}-{1}.pdf", DateTime.Now.Second, DateTime.Now.Millisecond),
FileMode.Create));


document.Open();

PdfPTable table = new PdfPTable(5); //初始化5列表
table.WidthPercentage = 94f; //表格宽度:1-100f
table.SpacingBefore = 5f;
table.HorizontalAlignment = Element.ALIGN_CENTER;
table.SplitRows = true;

PdfPCell cell;
Paragraph p;

//
p = new Paragraph(" " + LaoshiPingyuValue, new Font(baseFont, contentSize));
cell = new PdfPCell(p);
cell.Border = Rectangle.NO_BORDER;
cell.HorizontalAlignment = Element.ALIGN_LEFT;
cell.Colspan = 5; //次cell跨5列
cell.FixedHeight = 45f; //设置多行数据快的高度
cell.SetLeading(4, 1); //设置多行数据的行间距
table.AddCell(cell);
return table;


//加载本地图片到pdf上
string imagePath = "../../Data/header.png";

private PdfPTable GetTableHeader(string imagePath)
{
PdfPTable table = new PdfPTable(1);
table.WidthPercentage = 100f;
table.SplitRows = true;
table.HeaderRows = 0;
PdfPCell cell;

//add 图片
Image image = Image.GetInstance(imagePath);
cell = new PdfPCell(image, true);
table.AddCell(cell);

return table;
}

document.Add(GetTableContent(content_zongji, titleSize, contentSize));

document.Close();
writer.Close();

posted @ 2020-08-07 16:22  大树2  阅读(189)  评论(0编辑  收藏  举报