C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > C# iTextSharp生成PDF

C#使用iTextSharp生成PDF的示例代码

作者:StevenChen85

这篇文章主要为大家详细介绍了C#如何使用iTextSharp生成PDF的相关知识,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以了解下

需求

按数据层级生成PDF文件,要有目录,目录里要有真实的页码,附件内容用表格显示,每页要有页码,大标题 做为封面当独显示一页,

PDF内容

大标题,

目录(里有对应的页码)

正文 里有 表格

每页还有页码

实现代码

/// <summary>
        /// 生成 pdf 文件
        /// </summary>
        /// <param name="model">实体对象</param>
        /// <param name="caseDetailsViewModels">数据LIST</param>
        /// <param name="TargetPath">目标文件路径</param>
        /// <param name="NewFileName">文件名</param>
        /// <returns></returns>
        private static string createPDF(ViewModel model, List<DetailsViewModel> DetailsViewModels, string TargetPath, string NewFileName)
        {
            string outMergeFile = TargetPath + NewFileName;
 
            if (Directory.Exists(outMergeFile))
            {
                Directory.Delete(outMergeFile);
            }
            var pageSize = PageSize.A4;
            iTextSharp.text.Document document = new iTextSharp.text.Document(pageSize);
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(outMergeFile, FileMode.Create));
            try
            {
                document.Open();
 
                #region 字体
                Font BF_Light = FontFactory.GetFont(@"C:\Windows\Fonts\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
 
                Font bigTitle_Light = FontFactory.GetFont(@"C:\Windows\Fonts\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
                bigTitle_Light.Size = 20;
 
                Font title_Light = FontFactory.GetFont(@"C:\Windows\Fonts\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
                title_Light.SetColor(0, 0, 255);
                title_Light.Size = 16;
 
                Font body_Light = FontFactory.GetFont(@"C:\Windows\Fonts\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
                body_Light.Size = 12;
                //var titleFont = GetFont();
                //titleFont.SetStyle(Font.BOLD);
                //titleFont.Color = BaseColor.BLACK;
                //titleFont.Size = 16;
                //new Font(baseFont2, 20f) 
                #endregion
 
                //第一页
                //段落 大标题
                #region 大标题
                var titleParagraph = new Paragraph(new Phrase(model.TitleModel.F_ContentStr, bigTitle_Light));
                float x = 300;
                float y = document.PageSize.Height / 2;
                ColumnText.ShowTextAligned(writer.DirectContent, Element.ALIGN_CENTER, titleParagraph, x, y, 0);
                #endregion
 
                //第二页
                #region 增加目录页
                document.NewPage();
                var catalogueP = new Paragraph(new Phrase("目录", body_Light));
                //catalogueP.IndentationLeft = 160;
                //document.Add(catalogueP);
                //页头显示的位置
                ColumnText.ShowTextAligned(writer.DirectContent, Element.ALIGN_CENTER, catalogueP, document.PageSize.Width / 2, document.Top, 0);
                foreach (var item in model.TwoTitleList)
                {
                    Chunk leader = new Chunk(new DottedLineSeparator(), 400);//400
                    var F_ContentStrTxt = item.F_ContentStr.Trim();
                    Paragraph pp = new Paragraph(new Phrase("    " + F_ContentStrTxt, BF_Light));
                    pp.Add(leader);
                    pp.Add(new Phrase(!string.IsNullOrEmpty(item.F_Page) ? item.F_Page : "1~1", BF_Light));
                    document.Add(pp);
                }
                #endregion
                //正文
                //第三页
                #region 正文
                int iPageNum = 3;
                int beginPageNum = 3;
                BaseFont baseFont2 = BaseFont.CreateFont(@"C:\Windows\Fonts\simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                document.NewPage();
                foreach (var item in model.TwoTitleList)
                {
                    iPageNum = writer.PageNumber;
                    //标题
                    document.Add(new Paragraph(new Phrase(item.F_ContentStr, title_Light)));
                    foreach (var itemThree in item.ThreeContentList)
                    {
                        itemThree.F_ContentStr = itemThree.F_ContentStr.Replace("\r\n", string.Empty);
                        //内容
                        if (itemThree.F_ContentStr.Contains("</p>"))
                        {
                            var contentStr = itemThree.F_ContentStr.Replace("<p>", string.Empty);
                            var contentList = contentStr.Split("</p>");
                            foreach (var itemCon in contentList)
                            {
                                var itemConTxt = itemCon.Replace("\n", string.Empty).Trim();
                                var bydyPhrase = new Paragraph(new Phrase("    " + itemConTxt, body_Light));
                                document.Add(bydyPhrase);
                                if (writer.PageNumber > iPageNum)
                                {
                                    writePageNumber(document, writer, BF_Light);
                                }
                            }
                        }
                        else
                        {
                            var txt = itemThree.F_ContentStr.Replace("\n", string.Empty).Trim();
                            document.Add(new Paragraph(new Phrase("    " + txt, body_Light)));
                        }
                    }
                    //if (writer.PageNumber > iPageNum)
                    //{
                    item.F_Page = string.Format("{0}~{1}", iPageNum, writer.PageNumber);
                    iPageNum = writer.PageNumber;
                    //}
                }
                #endregion
 
                #region 附件
                if (model.FileList != null && model.FileList.Count > 0)
                {
                    var bydyPhrase1 = new Paragraph(new Phrase("    (一)检索结果分析列表", body_Light));
                    document.Add(bydyPhrase1);
                    document.Add(new Paragraph(new Phrase("  ")));
 
                    int groupNum = 1;
                    foreach (var item in model.FileList)
                    {
                        var keywordTxt = item.Keyword.Replace("\n", string.Empty).Trim();
                        var bydyPhrase = new Paragraph(new Phrase("    " + keywordTxt, body_Light));
                        document.Add(bydyPhrase);
                        document.Add(new Paragraph(new Phrase("  ")));
 
                        PdfPTable table = new PdfPTable(4);
                        int[] widths = { 40, 20, 20, 20 };
                        table.SetWidths(widths);//设置每列的宽度(求和百分比5,10=1,2)
                                                //为pdfpTable的构造函数传入整数3,pdfpTable被初始化为一个三列的表格
                                                //PdfPCell cell = new PdfPCell(new Phrase("第一行 跨3列", BF_Light));
                                                //cell.Colspan = 6;//跨3列
 
                        //0=Left, 1=Centre, 2=Right
                        //table.AddCell(cell);
                        PdfPCell cell = new PdfPCell(new Phrase("标题", BF_Light));
                        cell.HorizontalAlignment = 1;
                        table.AddCell(cell);
 
                        PdfPCell cel2 = new PdfPCell(new Phrase("案号", BF_Light));
                        cel2.HorizontalAlignment = 1;
                        table.AddCell(cel2);
 
                        PdfPCell cel3 = new PdfPCell(new Phrase("是否加入检索报告", BF_Light));
                        cel3.HorizontalAlignment = 1;
                        table.AddCell(cel3);
 
                        PdfPCell cel4 = new PdfPCell(new Phrase("未加入检索报告原因", BF_Light));
                        cel4.HorizontalAlignment = 1;
                        table.AddCell(cel4);
                        foreach (var itemKey in item.KeywordFileList)
                        {
                            table.AddCell(new Phrase(itemKey.F_CaseTitle, BF_Light));
                            table.AddCell(new Phrase(itemKey.F_CaseCode.ToString(), BF_Light));
                            table.AddCell(new Phrase(itemKey.F_IsAdd ? "是" : "否", BF_Light));
                            table.AddCell(new Phrase(itemKey.F_NoAddCause, BF_Light));
                            groupNum++;
                        }
                        document.Add(table);
                    }
                }
                #endregion
 
                #region 生成案例
                if (caseDetailsViewModels != null && caseDetailsViewModels.Count > 0)
                {
                    var bydyPhrase2 = new Paragraph(new Phrase("    (二)检索案例全文", body_Light));
                    document.Add(bydyPhrase2);
                    document.Add(new Paragraph(new Phrase("  ")));
 
                    foreach (var item in caseDetailsViewModels)
                    {
                        foreach (var itemCase in item.CaseItemList)
                        {
                            //标题
                            itemCase.F_Title = itemCase.F_Title.Replace("\r\n", string.Empty);
                            var F_TitleTxt = itemCase.F_Title.Trim();
                            document.Add(new Paragraph(new Phrase("  " + F_TitleTxt, body_Light)));
 
                            //内容
                            itemCase.F_Content = itemCase.F_Content.Replace("\r\n", string.Empty);
                            if (itemCase.F_Content.Contains("</p>"))
                            {
                                var contentStr = itemCase.F_Content.Replace("<p>", string.Empty);
                                var contentList = contentStr.Split("</p>");
                                foreach (var itemCon in contentList)
                                {
                                    var itemConTxt = itemCon.Replace("\n", string.Empty).Trim();
                                    var bydyPhrase = new Paragraph(new Phrase("  " + itemConTxt, body_Light));
                                    document.Add(bydyPhrase);
                                    if (writer.PageNumber > iPageNum)
                                    {
                                        writePageNumber(document, writer, BF_Light);
                                    }
                                }
                            }
                            else
                            {
                                var F_ContentTxt = itemCase.F_Content.Replace("\n", string.Empty).Trim();
                                document.Add(new Paragraph(new Phrase("  " + F_ContentTxt, body_Light)));
                            }
                        }
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                LogFactory.GetLogger("createReportDetailsPDFCatalogue").Info("createReportDetailsPDFCatalogue 报错" + ex.Message);
            }
            finally
            {
                if (document != null)
                {
                    document.Close();
                }
                if (writer != null)
                {
                    writer.Close();
                }
            }
            return outMergeFile;
        }
 
        /// <summary>
        /// 写页码
        /// </summary>
        /// <param name="document"></param>
        /// <param name="writer"></param>
        /// <param name="BF_Light"></param>
        private static void writePageNumber(iTextSharp.text.Document document, PdfWriter writer, Font BF_Light)
        {
            Phrase header = new Phrase("第" + (writer.PageNumber).ToString() + "页", BF_Light);// - 1
                                                                                             //页脚显示的位置
            ColumnText.ShowTextAligned(writer.DirectContent, Element.ALIGN_CENTER, header, document.PageSize.Width / 2, document.Bottom, 0);
        }

调用,注意要调用两次,第一次生成时不知道目录里的内容在第几页,第一次在生成时收集页码。这时会生成一个临时文件。第二次生成的目录里的页码才是真实有效的数据。所以要生成两次。

就好比你在写WORD时也是不知道目录里的内容会写在第几次,都是一、二级标题和内容都写完后,再去生成目录的。这里的道理也是一样的。

var fileNameTempPath = createPDF(model, null, uploadPath, fileNameTemp);
var savePath = createPDF(model, caseDetailsViewModels, uploadPath, fileName);

到此这篇关于C#使用iTextSharp生成PDF的示例代码的文章就介绍到这了,更多相关C# iTextSharp生成PDF内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文