ITextSharp生成PDF

ITextSharp就不多介绍了,下面就把遇到的坑一一记录下来,希望能够帮助到正在使用它的开发者们。操作pdf的方法都被作者封装好了,只是没有注释和说明,不过大部分的方法属性还是能看懂的,看不懂的可以反编译一下。

gethub下载dll地址https://github.com/itext/itextsharp/tags

1.输入文字不显示中文,文字换行

2.文字加颜色、字体大小、加粗、斜体、居中等骚操作

3.表格行合并、表格列合并

4.添加新页面

5.图片等比缩放、页面中心显示

下面代码演示:

首先添加几个dll

using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
private void SavePDF(HttpContext context)
        {
            Document document = new Document();
            //中文字体
            string chinese = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "KAIU.TTF");
            BaseFont baseFont = BaseFont.CreateFont(chinese, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            //文字大小12,文字样式
            Font cn = new Font(baseFont, 12, Font.NORMAL);

            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(@"D:\temp.pdf", FileMode.Create));
            document.Open();
            //最后一个参数是颜色,这里可以是rgb格式,也可以是默认定义的
            var title = new Paragraph("\n 这是一条标题0.0  hello ", new Font(baseFont, 14, Font.BOLD, BaseColor.RED));
            //居中
            title.Alignment = Element.ALIGN_CENTER;
            document.Add(title);

            Paragraph p = new Paragraph(" \n this is Second title \n ", cn);
            //Phrase p = new Phrase("这是一条标题0.0  hello", cn);
            p.Alignment = Element.ALIGN_CENTER;
            document.Add(p);

            //添加表格
            PdfPTable table = new PdfPTable(3);
            PdfPCell cell = new PdfPCell();

            table.AddCell("Row");
            cell = new PdfPCell(new Phrase("Cell"));
            cell.Colspan = 2;
            table.AddCell(cell);

            table.AddCell("row");
            cell = new PdfPCell(new Phrase("Cell"));
            cell.Colspan = 2;
            table.AddCell(cell);

            cell = new PdfPCell(new Phrase("Row"));
            cell.Rowspan = 2;
            table.AddCell(cell);
            table.AddCell("Cell");
            table.AddCell("Cell");
            table.AddCell("Cell");
            table.AddCell("Cell");

            table.HorizontalAlignment = Element.ALIGN_CENTER;
            document.Add(table);

            //新页面
            document.NewPage();
            document.Add(new Paragraph("Second page pic", cn));
            Image img = Image.GetInstance("E:/VsTest/testWeb/testWeb/Files/ts20171204.002.jpeg");

            float percentage = 1;
            //这里都是图片最原始的宽度与高度  
            float resizedWidht = img.Width;
            float resizedHeight = img.Height;

            //这时判断图片宽度是否大于页面宽度减去也边距,如果是,那么缩小,如果还大,继续缩小,  
            //这样这个缩小的百分比percentage会越来越小  
            while (resizedWidht > (document.PageSize.Width - document.LeftMargin - document.RightMargin) * 0.8)
            {
                percentage = percentage * 0.9f;
                resizedHeight = img.Height * percentage;
                resizedWidht = img.Width * percentage;
            }

            while (resizedHeight > (document.PageSize.Height - document.TopMargin - document.BottomMargin) * 0.8)
            {
                percentage = percentage * 0.9f;
                resizedHeight = img.Height * percentage;
                resizedWidht = img.Width * percentage;
            }

            //这里用计算出来的百分比来缩小图片  
            img.ScalePercent(percentage * 100);
            //图片定位,页面总宽283,高416;这里设置0,0的话就是页面的左下角 让图片的中心点与页面的中心店进行重合  
            img.SetAbsolutePosition(document.PageSize.Width / 2 - resizedWidht / 2, document.PageSize.Height / 2 - resizedHeight / 2);
            writer.DirectContent.AddImage(img);

            document.Close();

        }

最后看看效果

下面是该dll里面的字体和颜色集合

//public const int NORMAL = 0;
            //public const int BOLD = 1;
            //public const int ITALIC = 2;
            //public const int UNDERLINE = 4;
            //public const int STRIKETHRU = 8;
            //public const int BOLDITALIC = 3;
            //public const int UNDEFINED = -1;
            //public const int DEFAULTSIZE = 12;
            //public static readonly BaseColor WHITE;
            //public static readonly BaseColor BLUE;
            //public static readonly BaseColor CYAN;
            //public static readonly BaseColor MAGENTA;
            //public static readonly BaseColor GREEN;
            //public static readonly BaseColor ORANGE;
            //public static readonly BaseColor YELLOW;
            //public static readonly BaseColor RED;
            //public static readonly BaseColor BLACK;
            //public static readonly BaseColor DARK_GRAY;
            //public static readonly BaseColor GRAY;
            //public static readonly BaseColor LIGHT_GRAY;
            //public static readonly BaseColor PINK;

这里说说表格里面的PdfPTable,这个东西只能初始化他的列,表格里面add的时候是从左到右一行一行里面的单元格添加的,所以你添加的时候可以想象成输出乘法表那样。这里合并行的方法就是Colspan,列就是Rowspan,但是这里是属性。。。int类型数字几就是合并几行或者几列这样。。。其实一开始我以为不管是行合并列合并都是合并,应该有一个cell.row.merge(2)什么的,虽然不人性化但是习惯就好。

图片image对象就有意思了,它有宽和长度属性,但是长度是只读的,而且设置了宽度程序运行的时候会出错,哈哈哈。。。。这就尴尬了,所以最后用image的ScalePercent方法。。

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值