iTextSharp 生成PDF功能简单实现

准备工作:

  1. 创建Web项目
  2. 从Nuget中搜索并添加iTextSharp
  3. 添加如下两个引用

using iTextSharp.text;

using iTextSharp.text.pdf;

功能实现实例代码

/// <summary>
        /// 创建PDF
        /// </summary>
        /// <param name="dtSource">数据源</param>
        /// <returns></returns>
        private bool CreatePDF( DataTable dtSource)
        {
            try
            {
                Document doc = new Document(PageSize.A4);//定义一个Document,并设置页面大小为A4,竖向
                MemoryStream pdfMS = new MemoryStream();
                PdfWriter.GetInstance(doc, pdfMS);//PDF内容放入到流中
                doc.Open();
                float[] colSet = { 40, 80, 70, 70, 70, 70, 70, 70, 70, 50 };//设定每列宽度
                PdfPTable pdfTable = new PdfPTable(colSet.Length); //初始化列数
                pdfTable.SetWidthPercentage(colSet, PageSize.A4);
                pdfTable.NormalizeHeadersFooters();
                pdfTable.SplitLate = false;
                PdfPCell pdfPCell;//创建单元格
                BaseFont baseFont = BaseFont.CreateFont("C://WINDOWS//Fonts//simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//宋体
                BaseFont titleBaseFont = BaseFont.CreateFont("C://WINDOWS//Fonts//simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//黑体
                Font titleFont = new Font(titleBaseFont, 7);//标题字体
                Font contentFont = new Font(baseFont, 6);//正文字体
                int horizontalAlignment = Element.ALIGN_CENTER;  //水平居中(单元格文字位置)
                int verticalAlignment = Element.ALIGN_MIDDLE;   //垂直居中(单元格文字位置)
                int leftHAlignment = Element.ALIGN_LEFT;//水平居左

                #region 创建Table
                //标题
                pdfPCell = new PdfPCell(new Phrase("创建PDF测试", titleFont));
                pdfPCell.Colspan = 10;//合并单元格,默认为1
                pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
                pdfPCell.BackgroundColor = new BaseColor(0, 102, 204); //单元格背景颜色
                pdfPCell.HorizontalAlignment = horizontalAlignment;
                pdfPCell.VerticalAlignment = verticalAlignment;
                pdfTable.AddCell(pdfPCell);

                //Table列标题
                pdfPCell = new PdfPCell(new Phrase("序号", titleFont));
                pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
                pdfPCell.BackgroundColor = new BaseColor(0, 102, 204); //单元格背景颜色
                pdfPCell.HorizontalAlignment = horizontalAlignment;
                pdfPCell.VerticalAlignment = verticalAlignment;
                pdfTable.AddCell(pdfPCell);

                pdfPCell = new PdfPCell(new Phrase("报修编号", titleFont));
                pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
                pdfPCell.BackgroundColor = new BaseColor(0, 102, 204); //单元格背景颜色
                pdfPCell.HorizontalAlignment = horizontalAlignment;
                pdfPCell.VerticalAlignment = verticalAlignment;
                pdfTable.AddCell(pdfPCell);

                pdfPCell = new PdfPCell(new Phrase("报修标题", titleFont));
                pdfPCell.Colspan = 2;
                pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
                pdfPCell.BackgroundColor = new BaseColor(0, 102, 204); //单元格背景颜色
                pdfPCell.HorizontalAlignment = horizontalAlignment;
                pdfPCell.VerticalAlignment = verticalAlignment;
                pdfTable.AddCell(pdfPCell);

                pdfPCell = new PdfPCell(new Phrase("提交时间", titleFont));
                pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
                pdfPCell.BackgroundColor = new BaseColor(0, 102, 204); //单元格背景颜色
                pdfPCell.HorizontalAlignment = horizontalAlignment;
                pdfPCell.VerticalAlignment = verticalAlignment;
                pdfTable.AddCell(pdfPCell);

                pdfPCell = new PdfPCell(new Phrase("闭环时间", titleFont));
                pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
                pdfPCell.BackgroundColor = new BaseColor(0, 102, 204); //单元格背景颜色
                pdfPCell.HorizontalAlignment = horizontalAlignment;
                pdfPCell.VerticalAlignment = verticalAlignment;
                pdfTable.AddCell(pdfPCell);

                pdfPCell = new PdfPCell(new Phrase("报修人", titleFont));
                pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
                pdfPCell.BackgroundColor = new BaseColor(0, 102, 204); //单元格背景颜色
                pdfPCell.HorizontalAlignment = horizontalAlignment;
                pdfPCell.VerticalAlignment = verticalAlignment;
                pdfTable.AddCell(pdfPCell);

                pdfPCell = new PdfPCell(new Phrase("故障系统", titleFont));
                pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
                pdfPCell.BackgroundColor = new BaseColor(0, 102, 204); //单元格背景颜色
                pdfPCell.HorizontalAlignment = horizontalAlignment;
                pdfPCell.VerticalAlignment = verticalAlignment;
                pdfTable.AddCell(pdfPCell);

                pdfPCell = new PdfPCell(new Phrase("故障等级", titleFont));
                pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
                pdfPCell.BackgroundColor = new BaseColor(0, 102, 204); //单元格背景颜色
                pdfPCell.HorizontalAlignment = horizontalAlignment;
                pdfPCell.VerticalAlignment = verticalAlignment;
                pdfTable.AddCell(pdfPCell);

                pdfPCell = new PdfPCell(new Phrase("报修进度", titleFont));
                pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
                pdfPCell.BackgroundColor = new BaseColor(0, 102, 204); //单元格背景颜色
                pdfPCell.HorizontalAlignment = horizontalAlignment;
                pdfPCell.VerticalAlignment = verticalAlignment;
                pdfTable.AddCell(pdfPCell);

                int sortNo = 0;//序号
                               //表格行数据
                foreach (DataRow dr in dtSource.Rows)
                {
                    //序号
                    sortNo++;
                    pdfPCell = new PdfPCell(new Phrase(sortNo.ToString(), contentFont));
                    pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
                    pdfPCell.HorizontalAlignment = leftHAlignment;
                    pdfTable.AddCell(pdfPCell);
                    //报修编号
                    pdfPCell = new PdfPCell(new Phrase(dr[0].ToString(), contentFont));
                    pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
                    pdfPCell.HorizontalAlignment = leftHAlignment;
                    pdfTable.AddCell(pdfPCell);
                    //报修标题
                    pdfPCell = new PdfPCell(new Phrase(dr[1].ToString(), contentFont));
                    pdfPCell.Colspan = 2;
                    pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
                    pdfPCell.HorizontalAlignment = leftHAlignment;
                    pdfTable.AddCell(pdfPCell);
                    //提交时间
                    pdfPCell = new PdfPCell(new Phrase(dr[2].ToString(), contentFont));
                    pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
                    pdfPCell.HorizontalAlignment = leftHAlignment;
                    pdfTable.AddCell(pdfPCell);
                    //闭环时间
                    pdfPCell = new PdfPCell(new Phrase(dr[3].ToString(), contentFont));
                    pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
                    pdfPCell.HorizontalAlignment = leftHAlignment;
                    pdfTable.AddCell(pdfPCell);
                    //报修人
                    pdfPCell = new PdfPCell(new Phrase(dr[4].ToString(), contentFont));
                    pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
                    pdfPCell.HorizontalAlignment = leftHAlignment;
                    pdfTable.AddCell(pdfPCell);
                    //故障系统
                    pdfPCell = new PdfPCell(new Phrase(dr[5].ToString(), contentFont));
                    pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
                    pdfPCell.HorizontalAlignment = leftHAlignment;
                    pdfTable.AddCell(pdfPCell);
                    //故障等级
                    pdfPCell = new PdfPCell(new Phrase(dr[6].ToString(), contentFont));
                    pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
                    pdfPCell.HorizontalAlignment = leftHAlignment;
                    pdfTable.AddCell(pdfPCell);
                    //报修进度
                    pdfPCell = new PdfPCell(new Phrase(dr[7].ToString(), contentFont));
                    pdfPCell.BorderColor = new BaseColor(0, 0, 0); //单元格边框颜色,默认黑色
                    pdfPCell.HorizontalAlignment = leftHAlignment;
                    pdfTable.AddCell(pdfPCell);
                }
                #endregion

                #region 插入图片
                pdfPCell = new PdfPCell();
                pdfPCell.Border = 0;
                pdfPCell.Colspan = 10;
                pdfPCell.HorizontalAlignment = leftHAlignment;
                iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance("D:\\test.jpg");
                pdfPCell.Image = img; //图片 
                pdfTable.AddCell(pdfPCell);
                #endregion

                doc.Add(pdfTable);
                doc.NewPage();//分页
                doc.Close();
                //保存pdf文件
                byte[] fileByte = pdfMS.GetBuffer();
                string fileName = "D:\\" + Guid.NewGuid().ToString() + ".pdf";
                FileStream fs = new FileStream(fileName, FileMode.Create);
                BinaryWriter bw = new BinaryWriter(fs);
                bw.Write(fileByte);
                bw.Close();
                fs.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值