PDFNet (c#) - Howto change colour of individual words or characters?

Hi,

Evaluating PDFNet for a new project and I'm stuck on one thing at the moment.

I'm trying to work out how I can change the colour of particular words or characters in a PDF document. I've followed the sample ElementEdit code to be able to change all text to blue, and I can do that for specific e_text elements too, but sometimes the text I want is just part of an e_text element (for instance if I wanted to change the workd 'Secret' to red, but the e_text element was 'Top Secret' then I would end up with both words being red).

I can get at the individual words I want using the TextExtractor, but can't see any way from there to update the text colour. I can't see any way to split up an e_text element into bits so I can colour only part of it.

I would be happy if I could put another copy of the word that needs the colour applied over the top of the same word in the existing text element, but stuck with that idea too.

any suggestions?

Robin

Is your objective simply to change the color, or is this part of some larger objective? There might be an easier way to accomplish what you want, but we would need additional context/higher level overview.

Ok, so what I need to do is this

1 load a PDF from file
2 extract the text
3 evaluate the text that’s extracted to identify certain words that need to be highlighted in colour [note - it’s not just a case of highlighting every time a particular word appears in the PDF, a separate decision needs to be made for each occurence of the word]
4 apply the appropriate colour the the correct words in the PDF in memory.
5 Display the PDF inside our application (using your WPF control).

I’ve got all the steps working now except for step 4. I have tried alternatives like underlining the words or drawing a box around them, but that nowhere near as good for this particular project.

thanks,

Robin

I updated the code from this forum posting,
https://groups.google.com/d/msg/pdfnet-sdk/dLcJ9aQm00g/7Uy475ZlQAYJ

and if you if you replace our ElementEditTest code with this code, you can see it in action.

ElementEditTest.cs (17.7 KB)

Hi Ryan,

thanks for the sample code! - I have been able to adapt it to meet my use case requirements.

One thing I am noticing is that the code can in some cases move words to the wrong horizontal position in a line of text. I suspect it’s caused by this

case GState.GStateAttribute.e_text_pos_offset:
//ngs.Settext_pos_offset(gs.Gettext_pos_offset());
break;

ie the text pos offset is being lost when the code has to look at the text one glyph at a time.Obviously this line is commented it beacause the Settext_pos_offset and Gettext_pos_offset functions don’t exist on the GState class. Is there any workaround for this to get this property copied across correctly?

thanks again

Robin

That is commented out because the text position offset is always being used.

new_element.SetPosAdjustment(element.GetPosAdjustment());

I suspect something else is going on that I am not aware of. If you can send the following here, or to support, then the issue can be resolved.

Input PDF file.
One of our SDK samples modified to do what you want.

Generated output PDF file
Screenshot showing what you expected to get (the “error” in the output file)