Skip navigation
Currently Being Moderated

Change text color

Sep 4, 2012 12:18 AM

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

 
Replies
  • Currently Being Moderated
    Sep 4, 2012 12:22 AM   in reply to bacas_christy

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 4, 2012 12:30 AM   in reply to bacas_christy

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 4, 2012 12:32 AM   in reply to bacas_christy

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 4, 2012 12:37 AM   in reply to bacas_christy

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

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 4, 2012 12:51 AM   in reply to bacas_christy

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 4, 2012 1:06 AM   in reply to Gilad D (try67)

    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.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points