Itextsharp 使用心得

1. Must use FontFactory.GetFont(...) to create a Font.  Cannot use below to create a Font

paragraphTitleContent.Font=new Font();

paragraphTitleContent.Font.SetFontFamily("Your Font Family");

Paragraph paragraphTitleContent = new Paragraph("273509239@qq.com");
            paragraphTitleContent.IndentationLeft = 0;
            paragraphTitleContent.Alignment = Element.ALIGN_LEFT;
            //here must use FontFactory.GetFont, cannot use new Font() and paragraphTitleContent.Font.SetFontFamily("fontFamily"), because the latter not work
            paragraphTitleContent.Font =FontFactory.GetFont("Your Font Family");
            paragraphTitleContent.Font.SetColor(255, 255, 25);
            paragraphTitleContent.Font.Size = 12;

2. Set Style (Italic,Bold,Underline are bool type)

 private static void SetStyleForTableOfContentParagraph(Paragraph paragraphContent, ReportSetupFontStyle fontStyle)
        {
            if (fontStyle.FontStyle.Italic && fontStyle.FontStyle.Bold && fontStyle.FontStyle.Underline)
            {
                paragraphContent.Font.SetStyle(Font.BOLDITALIC | Font.UNDERLINE);
            }
            else if (fontStyle.FontStyle.Italic && fontStyle.FontStyle.Bold)
            {
                paragraphContent.Font.SetStyle(Font.BOLDITALIC);
            }
            else if (fontStyle.FontStyle.Italic && fontStyle.FontStyle.Underline)
            {
                paragraphContent.Font.SetStyle(Font.ITALIC | Font.UNDERLINE);
            }
            else if (fontStyle.FontStyle.Bold && fontStyle.FontStyle.Underline)
            {
                paragraphContent.Font.SetStyle(Font.BOLD | Font.UNDERLINE);
            }
            else if (fontStyle.FontStyle.Underline)
            {
                paragraphContent.Font.SetStyle(Font.UNDERLINE);
            }
            else if (fontStyle.FontStyle.Bold)
            {
                paragraphContent.Font.SetStyle(Font.BOLD);
            }
            else if (fontStyle.FontStyle.Italic)
            {
                paragraphContent.Font.SetStyle(Font.ITALIC);
            }
            else
            {
                paragraphContent.Font.SetStyle(Font.NORMAL);
            }
        }

3. GetFont weight

 internal static float GetFontWidth(BaseFont baseFont, string text, float fontSize)
        {
            fontSize = fontSize > 0 ? fontSize : ReportingPlatformConstant.DefaultFontSize;
            return baseFont.GetWidthPoint(text, fontSize);
        }

4. Add bookmark in PDF

 public static void AddBookMarkInPDF(string outputFileAbsolutPath, BookMark bookmark)
        {
            if (System.IO.File.Exists(outputFileAbsolutPath) && bookmark != null && bookmark.BookMarkList != null && bookmark.BookMarkList.Count > 0)
            {
                List<TableOfContent> tocList = bookmark.BookMarkList.Where(x => x.HiddenTableOfContents == false).ToList();
                if (tocList != null && tocList.Count > 0)
                {
                    //temp file
                    string[] fileArray = outputFileAbsolutPath.Split('\\');
                    string fileNameTmp = fileArray[fileArray.Length - 1];
                    string[] alisNameArray = fileNameTmp.Split('.');
                    string tempFile = outputFileAbsolutPath.Replace(fileNameTmp, Guid.NewGuid().ToString() + "" + alisNameArray[alisNameArray.Length - 1]);
                    //string tempFile = outputFileAbsolutPath.Substring(0, outputFileAbsolutPath.LastIndexOf(".") - 1) + Guid.NewGuid().ToString() + ".pdf";
                    Document document = new Document(PageSize.LETTER.Rotate());
                    document.ResetPageCount();
                    FileStream fileStream = new FileStream(tempFile, FileMode.Create, FileAccess.Write);
                    PdfCopy copy = new PdfCopy(document, fileStream);
                    document.Open();

                    PdfReader reader = new PdfReader(outputFileAbsolutPath);
                    copy.AddDocument(reader);
                    reader.Close();
                    List<Dictionary<string, object>> bookmarks = new List<Dictionary<string, object>>();
                    var temp = copy.GetImportedPage(reader, 1); // get 1st page
                    var h = temp.Height; // get height of 1st page
                    for (int i = 0; i < tocList.Count; i++)
                    {
                        // Add first item to bookmarks.
                        Dictionary<string, object> test = new Dictionary<string, object>();
                        test.Add("Action", "GoTo");
                        test.Add("Title", tocList[i].Title);
                        test.Add("Page", tocList[i].ActualStartPageNumber + " XYZ 0 " + h + " 0"); // use height of 1st page
                        bookmarks.Add(test);
                    }
                    // Do your worst and afterwards set the bookmarks to Outline. So yeah.
                    copy.Outlines = bookmarks;
                    document.Close();
                    System.IO.File.Delete(outputFileAbsolutPath);
                    FileInfo info = new FileInfo(tempFile);
                    info.MoveTo(outputFileAbsolutPath);
                    System.IO.File.Delete(tempFile);
                }
            }
        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值