ItextSharp 生成pdf文件
2016-07-01 13:34 迷忙 阅读(455) 评论(0) 编辑 收藏 举报1、创建document文档
public Document GetPdfDocument()
{
Document doc = new Document(CreateExpressRectangle(), 10, 10, 10, 10);
return doc;
}
protected Rectangle CreateExpressRectangle()
{
Rectangle rect = new Rectangle(400, 500);
return rect;
}
2、生成writer对象
PdfWriter write = PdfWriter.GetInstance(doc, new FileStream("fileName.pdf", FileMode.Create));
3、打开当前document
if (!doc.IsOpen())
{
doc.Open();
}
4、创建document内容,并加入document
生成条形码:
Barcode128 bar128 = new Barcode128();
bar128.Baseline = 10; //设置基线
bar128.Size = 13;//设置字体大小
bar128.CodeType = Barcode128.CODE128;//条码类型
bar128.Code = Code;//条码字串
Image img128 = bar128.CreateImageWithBarcode(write.DirectContent, BaseColor.BLACK, BaseColor.BLACK);
img128.ScaleAbsolute(170, 50);//设置比例
字体:
string fontPath = @"C:\windows\Fonts\simsun.ttc,1";//宋体,系统字体
BaseFont bf = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font font = new Font(bf, 12);
表格pdfpTable:
PdfPTable tableHeader = new PdfPTable(3);
tableHeader.SetTotalWidth(new float[] { 40f, 150f, 190f });
tableHeader.LockedWidth = true;
单元格: PdfPCell cellHeader = new PdfPCell(new Phrase("测试测试", font));
cellHeader.HorizontalAlignment = Element.ALIGN_CENTER;
cellHeader.VerticalAlignment = Element.ALIGN_MIDDLE;
cellHeader.MinimumHeight = 40;
cellHeader.Table=。。。//单元格内添加表格,占单元格100%
doc .add(....);
5、关闭document
if(doc.IsOpen())
{
doc.Close();
}