9 Replies Latest reply: Sep 4, 2012 1:14 AM by bacas_christy RSS

    Change text color

    bacas_christy Community Member

      Hi all,

       

      I m finding some text in PDF file. After finding the text now I want to change the color of the text using acrobat javascript.

       

      Is it possible?

       

      Below I have pasted my coding for your quick reference, but its not working.

       

      var sourceDoc=this;

      cpage=156;

      var reg=/Terry/;

      numWords = this.getPageNumWords(156);

      for (var j = 0; j < numWords; j++)

      {

      var word = this.getPageNthWord(156,j,false);

      if(reg.test(word))

      {

      word.strokeColor=color.red;

      //word.fillColor=color.red;

      }

      }

       

       

      Thanks in advance,

      Christy

        • 1. Re: Change text color
          try67 CommunityMVP

          No. Acrobat JavaScript has very limited access to the underlying PDF

          content. It can get some information about it (like the textual content or

          where a word is located), but can't change it in any (direct) way.

          • 2. Re: Change text color
            bacas_christy Community Member

            Hi Try,

             

            Is there any other possibility (indirect way) to change the color, since I m creating link for that word so I have to differenciate the text in different color.

             

            Thanks,

            Christy

            • 3. Re: Change text color
              try67 CommunityMVP

              What you can do is overlay the existing text with an opaque text box, where

              you can set the font color. Then you populate the box with the text from

              the file that you're covering. However, be aware that this will never look

              100% perfect.

              You'll need to place the box using the quads of the word that you're

              covering.

              • 4. Re: Change text color
                Test Screen Name CommunityMVP

                For access to page contents you'd need to write a plug-in (C or C++). This is possible in a plug-in, but this particular task is surprisingly difficult and challenging, for example two different APIs (text search and PDFEdit have to be worked with and there are no conversions between them, font encodings need to be understood, there are many colour spaces to accomodate, the words to highlight might be split over multiple areas; not a good first plug-in to write.

                • 5. Re: Change text color
                  bacas_christy Community Member

                  Hi Try,

                   

                  Of course, for insrting link I m creating  box using the quads.

                   

                  Could you please share sample coding?

                   

                  Thanks,

                   

                  Christy

                  • 6. Re: Change text color
                    Test Screen Name CommunityMVP

                    This is not a question of sample coding, it is a lot of work.

                    • 7. Re: Change text color
                      try67 CommunityMVP

                      It's not that much work, actually. You just need to convert the quads to a

                      rect (which you might already do for your link), then use the addField()

                      method to add the text box, and then set its properties (font, font size,

                      font color, read-only, etc.) the way you want them to be.

                      Also, you can use the MouseUp event of the text box to do the same action

                      that your link does, so you won't have to create the link on top of the box.

                      • 8. Re: Change text color
                        Test Screen Name CommunityMVP

                        Ah,sorry, my reply crossed over. I can see that, although you cannot change the text colour in the page body, JavaScript can put a box to cover it with new text. Simple, except for font matching, which would have to be pre-configured or otherwise specified by the user. Since you're adding the same text twice, check that text extraction and copy/paste and accessibility still work to the extent you need them to.

                        • 9. Re: Change text color
                          bacas_christy Community Member

                          Hi Try,

                           

                          Thanks for your support.

                           

                          Thanks,

                          Christy