how to highlight a text or word in a pdf file using iTextsharp?

前端 未结 5 1729
陌清茗
陌清茗 2020-11-28 13:13

I need to search a word in a existing pdf file and i want to highlight the text or word

and save the pdf file

I have an idea using PdfAnnotation.CreateMarku

5条回答
  •  失恋的感觉
    2020-11-28 13:50

    @Jcis, I actually managed a workaround for handling multiple searches using your example as a starting point. I use your project as a reference in a c# project, and altered what it does. Instead of just highlighting I actually have it drawing a white rectangle around the search term, and then using the rectangle coordinates, place a form field. I also had to swap the contentbyte writing mode to getovercontent so that I block out the searched text entirely. What I actually did was to create a string array of search terms, and then using a for loop, I create as many different text fields as I need.

            Test.Form1 formBuilder = new Test.Form1();
    
            string[] fields = new string[] { "%AccountNumber%", "%MeterNumber%", "%EmailFieldHolder%", "%AddressFieldHolder%", "%EmptyFieldHolder%", "%CityStateZipFieldHolder%", "%emptyFieldHolder1%", "%emptyFieldHolder2%", "%emptyFieldHolder3%", "%emptyFieldHolder4%", "%emptyFieldHolder5%", "%emptyFieldHolder6%", "%emptyFieldHolder7%", "%emptyFieldHolder8%", "%SiteNameFieldHolder%", "%SiteNameFieldHolderWithExtraSpace%" };
            //int a = 0;
            for (int a = 0; a < fields.Length; )
            {
                string[] fieldNames = fields[a].Split('%');
                string[] fieldName = Regex.Split(fieldNames[1], "Field");
                formBuilder.PDFTextGetter(fields[a], StringComparison.CurrentCultureIgnoreCase, htmlToPdf, finalhtmlToPdf, fieldName[0]);
                File.Delete(htmlToPdf);
                System.Array.Clear(fieldNames, 0, 2);
                System.Array.Clear(fieldName, 0, 1);
                a++;
                if (a == fields.Length)
                {
                    break;
                }
                string[] fieldNames1 = fields[a].Split('%');
                string[] fieldName1 = Regex.Split(fieldNames1[1], "Field");
                formBuilder.PDFTextGetter(fields[a], StringComparison.CurrentCultureIgnoreCase, finalhtmlToPdf, htmlToPdf, fieldName1[0]);
                File.Delete(finalhtmlToPdf);
                System.Array.Clear(fieldNames1, 0, 2);
                System.Array.Clear(fieldName1, 0, 1);
                a++;
            }
    

    It bounces the PDFTextGetter function in your example back and forth between two files until I achieve the finished product. It works really well, and it would not have been possible without your initial project, so thank you for that. I also altered your VB to do the text field mapping like so;

               For Each rect As iTextSharp.text.Rectangle In MatchesFound
                    cb.Rectangle(rect.Left, rect.Bottom + 1, rect.Width, rect.Height + 4)
                    Dim field As New TextField(stamper.Writer, rect, FieldName & Fields)
                    Dim form = stamper.AcroFields
                    Dim fieldKeys = form.Fields.Keys
                    stamper.AddAnnotation(field.GetTextField(), page)
                    Fields += 1
                Next
    

    Just figured I would share what I managed to do with your project as a backbone. It even increments the field names as I need them to. I also had to add a new parameter to your function, but that's not worth listing here. Thank you again for this great head start.

提交回复
热议问题