• iText7 For .Net 系列文章(三)文本注释、注释回复、链接注释


      在文档中增加注释或者评论,这是非常常用,PDF文档也包含这样的功能,在现有PDF文档上增加文本注释、对之前的注释进行回复。在Word、Excel还有链接的功能,跳转到本Word中其他位置(类似导航栏)、其他Word中莫个位置、甚至某个网址还有其他的功能,PDF也支持这些操作,这是只是以简单的超链接为例,可根据具体情况创建不同的PDFAction来执行不同的链接注释点击行为。不多说,直接上代码,可以对着练练手,然后工作中具体用到某个功能时再去具体仔细分析需要怎么改。

            #region Chapter 4
            private void textAnnotationToolStripMenuItem_Click(object sender, EventArgs e)
            {
                var fullFileName = GetSavingFileNameContainDirectory();
                if (string.IsNullOrWhiteSpace(fullFileName)) return;
                using (var fos = new FileStream(fullFileName, FileMode.Create))
                {
                    //Initialize PDF document
                    PdfDocument pdf = new PdfDocument(new PdfWriter(fos));
                    // Initialize document
                    Document document = new Document(pdf);
                    PdfFont yaHeiFont = PdfFontFactory.CreateRegisteredFont("yaHei_font", PdfEncodings.IDENTITY_H, EmbeddingStrategy.FORCE_EMBEDDED);
                    Paragraph p = new Paragraph().SetFont(yaHeiFont).SetFontSize(12).Add("这是一段含有注解的文本。");
                    document.Add(p);
                    var pdfPage = pdf.GetFirstPage();
                    //Create text annotation
                    PdfAnnotation ann = new PdfTextAnnotation(new Rectangle(20, 800, 0, 0))
                        .SetOpen(true)
                        .SetColor(new DeviceRgb(System.Drawing.Color.Green.R, System.Drawing.Color.Green.G, System.Drawing.Color.Green.B))
                        .SetTitle(new PdfString("iText"))
                        .SetContents("With iText, you can truly take your documentation needs to the next level.");
                    //ann.SetFlag(iText.Kernel.Pdf.Annot.PdfAnnotation.INVISIBLE);
                    pdfPage.AddAnnotation(ann);
    
                    document.Close();
    
                }
            }
    
            private void replyAnnotaionToolStripMenuItem_Click(object sender, EventArgs e)
            {
                var src = GetOpeningFileNameContainDirectory("PDF(*.pdf)|*.pdf");
                var dest = GetSavingFileNameContainDirectory();
                if (string.IsNullOrWhiteSpace(src) || string.IsNullOrWhiteSpace(dest)) return;
                PdfDocument pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest));
                PdfPage firstPage = pdfDoc.GetFirstPage();
                IList<PdfAnnotation> annots = firstPage.GetAnnotations();
    
                Rectangle stickyRectangle = annots[0].GetRectangle().ToRectangle();
                PdfAnnotation replySticky = new PdfTextAnnotation(stickyRectangle)
                    // This method specifies whether the annotation will initially be displayed open.
                    .SetOpen(true)
                    .SetIconName(new PdfName("Comment"))
    
                    // This method sets an annotation to which the current annotation is "in reply".
                    // Both annotations shall be on the same page of the document.
                    .SetInReplyTo(annots[0])
    
                    // This method sets the text label that will be displayed in the title bar of the annotation's pop-up window
                    // when open and active. This entry shall identify the user who added the annotation.
                    .SetText(new PdfString("Reply"))
    
                    // This method sets the text that will be displayed for the annotation or the alternate description,
                    // if this type of annotation does not display text.
                    .SetContents("Hello PDF")
                    .SetColor(new DeviceRgb(System.Drawing.Color.Green.R, System.Drawing.Color.Green.G, System.Drawing.Color.Green.B));
    
                firstPage.AddAnnotation(replySticky);
    
                pdfDoc.Close();
            }
    
            private void linkAnnotationToolStripMenuItem_Click(object sender, EventArgs e)
            {
                var dest = GetSavingFileNameContainDirectory();
                if (string.IsNullOrWhiteSpace(dest)) return;
                PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
                Document document = new Document(pdfDoc);
                // Create link annotation
                PdfLinkAnnotation annotation = (new PdfLinkAnnotation(new Rectangle(0, 0)).SetAction(PdfAction.CreateURI("https://itextpdf.com/")));
    
                Link link = new Link("here", annotation);
                Paragraph p = new Paragraph("The example of link annotation. Click ").Add(link.SetUnderline()).Add(" to learn more...");
    
                document.Add(p);
                document.Close();
            }
    
            #endregion
            private string GetSavingFileNameContainDirectory(string filter = "PDF(*.pdf)|*.pdf")
            {
                saveFileDialog1.Filter = filter;
                saveFileDialog1.AddExtension = true;
                saveFileDialog1.ShowDialog();
                return saveFileDialog1.FileName;
            }
    
            private string GetOpeningFileNameContainDirectory(string fileter = "CSV(*.CSV)|*.csv|txt files(*.txt)|*.txt")
            {
                openFileDialog1.Filter = fileter;
                openFileDialog1.ShowDialog();
                return openFileDialog1.FileName;
            }
    
    

     该文章主要用于记录备忘,如果读后对您有帮助,还望不吝点赞,如果实践过程中有任何疑问欢迎留言讨论。



    本文来自博客园,作者:业荒于嬉,转载请注明原文链接:https://www.cnblogs.com/FreeLoopPowter/p/15771025.html

    如内容对您有所帮助,还请不吝推荐一下!

  • 相关阅读:
    MyBatis框架Dao代理
    MyBatis对象分析及创建工具类
    搭建MyBatis开发环境及基本的CURD
    IDEA中配置Maven
    rpm 安装mysql8.0 ;安装deb
    SpringBoot 整合 xxl-job 指导手册
    设计模式(一) 单例设计模式
    SpringCloud (三) Eureka 注册中心
    SpringCloud (二) 注册中心理论
    SpringCloud (一) 微服务入门
  • 原文地址:https://www.cnblogs.com/FreeLoopPowter/p/15771025.html
Copyright © 2020-2023  润新知