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 <[email protected]>
>To: [email protected]
>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
>
>
>