Thank you very much! It does work. The only thing is that you have to
use yellowStream.getCOSObject() instead of yellowStream in your last line.
Also, the PDPageContentStream.fillRect( x, y, w, h) method uses the bottom
left corner of the page as the origin (0,0) which is different from the PDF
standard -- the upper left corner. But that's not a problem as it's fixable
with simple arithmetic.

Thank you so much for your help. It would have taken me a long time to
figure it out on my own, if ever.

Alin Mazilu


On Fri, Jul 26, 2013 at 6:19 PM, Fred Hansen <zweibie...@yahoo.com> wrote:

> Caveat: I've not tried this; nor anything like it. I am answering because
> figuring out how to do it was a challenge.
>
> Presumably your program has variables 'page' and 'document' where the
> rectangle goes and variables llx, lly, w, and h delimiting the rectangle.
>
> Here's some code that might work.  (UNTESTED)
>
> // first construct a stream that draws a yellow rectangle
> //      at the desired coordinates, but on a temporary page
> PDPage tempPage = new PDPage();
> PDPageContentStream tempStream = new PDPageContentStream(document,
> tempPage);
> tempStream.setNonStrokingColor(0,255,255);    //a version of yellow
> tempStream.fillRect(llx, lly, w, h);   //  where to put rect
> tempStream.close();
>
> // now get a handle on the stream (I hope it is not an array)
> PDStream yellowStream = tempPage.getContents();
>
> // get the contents of the page
> COSDictionary dict = page.getCOSDictionary();
> COSBase pageStream = dict.getDictionaryObject("Contents");
>
> // make sure the contents are a COSArray
> COSArray pageStreamArray;
> if (pageStream instanceof COSStream) {
>     pageStreamArray = new COSArray();
>     pageStreamArray.add(pageStream);
>     dict.setItem("Contents", pageStreamArray);
> }
> else pageStreamArray = (COSArray)pageStream;
>
> // now we add yellowStream at the front of page.getContents()
> //   (in front so text is later drawn on top of it)
> pageStreamArray.add(0, yellowStream );
>
>   ------------------------------
>  *From:* Alin Mazilu <impet...@gmail.com>
> *To:* dev@pdfbox.apache.org
> *Sent:* Friday, July 26, 2013 12:33 PM
> *Subject:* PDF Text Highlight
>
> Hello all,
>
> I have a bit of a situation on my hands. Here it is: I have a bunch of PDF
> files sitting in a folder somewhere. What I have to do is search all of
> them for certain names and highlight those names with a yellow marker-like
> background and then I have to send all PDFs to a printer.
>
> I have done the searching and text extraction and the printing, but for the
> life of me, I can't figure out how to do the highlighting. What makes it
> even harder is that I have hundreds of these PDFs per day and human
> interaction is out of the question. It has to be a push of a button.
>
> Any ideas? I appreciate it.
>
> Alin Mazilu
>
>
>

Reply via email to