c#使用itextsharp的实例

本文介绍如何使用iTextSharp库生成包含汉字、表格及图片的PDF文档。详细讲解了设置文档格式、添加页眉页脚、定义字体、创建表格及插入图片的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在编程中用到将一些报告生成pdf文件,用到了itextsharp版本是4.1.2.0和最新的版本的某些函数有出入

Document doc = new Document(PageSize.A4, 100, 100, 80, 80);//文档A4样式,左右页边距100,上下80
PdfWriter.GetInstance(doc, new FileStream(path + "" + pdfname + ".pdf", FileMode.Create));//创建文档            
HeaderFooter footer = new HeaderFooter(new Phrase(""), true);//页脚加页码居中下面画一条横线
footer.SetAlignment("center");
            footer.Border = iTextSharp.text.Rectangle.ALIGN_RIGHT;
            doc.Footer = footer;

 

 

自带库中的字体不能进行汉语的编辑需要创建汉字的字体

BaseFont bfHei = BaseFont.CreateFont(@"c:\Windows\fonts\simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//黑体
BaseFont bfSun = BaseFont.CreateFont("C:\\Windows\\Fonts\\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//宋体

上面的两个分别为创建黑体和宋体,需要注意的是宋体是ttc并且后面加一个0

 

 

iTextSharp.text.Font sun12red = new iTextSharp.text.Font(bfSun, 12, iTextSharp.text.Font.NORMAL, iTextSharp.text.Color.RED);//宋体12号红


iTextSharp.text.Font sun12chuti = new iTextSharp.text.Font(bfSun, 12,iTextSharp.text.Font.BOLD);//宋体12号加粗

编辑中可能会用到不同的字号颜色 ,上面两个是具体字体的定义

 

 

            Paragraph bianhao = new Paragraph("编号:"+num+"", sun10);
            bianhao.SetAlignment("right");
            doc.Add(bianhao);

段落默认是左对齐 也可以right设置为居右,center设置为居中

 

 

PdfPCell cell1 = new PdfPCell(new Phrase("项目", sun12));
            cell1.HorizontalAlignment = 1;
            PdfPCell cell2 = new PdfPCell(new Phrase("分数", sun12));
            cell2.HorizontalAlignment = 1;
            PdfPCell cell3 = new PdfPCell(new Phrase("1", sun12));
            PdfPCell cell4 = new PdfPCell(new Phrase("2", sun12));
            PdfPCell cell5 = new PdfPCell(new Phrase("3", sun12));
            PdfPCell cell6 = new PdfPCell(new Phrase("4", sun12));
            PdfPCell cell7 = new PdfPCell(new Phrase("5", sun12));
            PdfPCell cell8 = new PdfPCell(new Phrase("6", sun12));
            PdfPCell cell9 = new PdfPCell(new Phrase("7", sun12));
            PdfPCell cell10 = new PdfPCell(new Phrase("8", sun12));

            table.AddCell(cell1);
            table.AddCell(cell2);
            table.AddCell(cell3);
            table.AddCell("" + pf + "");
            table.AddCell(cell4);
            table.AddCell("" + rp + "");
            table.AddCell(cell5);
            table.AddCell("" + sf + "");
            table.AddCell(cell6);
            table.AddCell("" + re + "");
            table.AddCell(cell7);
            table.AddCell("" + bp + "");
            table.AddCell(cell8);
            table.AddCell("" + gh + "");
            table.AddCell(cell9);
            table.AddCell("" + vt + "");
            table.AddCell(cell10);
            table.AddCell("" + mh + "");
            doc.Add(table);

上面是进行表格的编辑HorizontalAlignment可以用来设置表格元素的对齐方式

 

iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance("D:\\reportpdf\\img\\ex.jpg");
            img.ScalePercent(60f);
            doc.Add(img);

图片的添加ScalePercent(60f)用来设置图片显示为原图的比例




 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值