pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nikos Chantziaras <rea...@gmail.com>
Subject Re: Saving PDFHighlighter selection to PDF
Date Mon, 17 Feb 2014 15:18:53 GMT
I've come up with a good way to insert color-filled rectangles through 
the use of Overlay (so that they are rendered behind the text instead of 
the front of it.)

However, how do I get the text coordinates? For example, I'd like to get 
the coordinates for every occurrence of the word "mark me" so that I can 
create a new PDDocument with filled rectangles and then overlay the 
original over it. Currently, I have:

   PDDocument document = PDDocument.load("src.pdf");
   PDDocument underContent = new PDDocument();
   PDPage underPage = new PDPage();
   PDPageContentStream contStrm = new PDPageContentStream(underContent,
                                                          underPage);
   contStrm.setNonStrokingColor(Color.yellow);

   contStrm.fillRect(/* Here I need the coordinates */);

   contStrm.close();
   underContent.addPage(underPage);

   Overlay overlay = new Overlay();
   overlay.overlay(document, underContent);
   underContent.save("out.pdf");
   underContent.close();


On 17/02/14 13:52, Maruan Sahyoun wrote:
>[...]
> If you would like to make the highlight part of the PDF you could
> create an annotation or draw a rectangle behind the text.
>
> Steps would be a. find the text b. get the coordinates of the text c.
> create an annotation or a rectangle around the coordinates
>
> You could also use the information in the highlight xml file to get
> the coordinates for the annotation/rectangle
>
> Am 17.02.2014 um 12:09 schrieb Nikos Chantziaras <realnc@gmail.com>:
>
>> It is not apparent how to save a PDF after I highlight some text in
>> it. For example:
>>
>> PDDocument doc = PDDocument.load("source.pdf"); PDFHighlighter hl =
>> new PDFHighlighter(); java.io.FileWriter xml = new
>> java.io.FileWriter("tmp.xml"); hl.generateXMLHighlight(doc, "450",
>> xml);
>>
>> How do I now save source.pdf (or a copy of it, if overwriting is
>> not possible) with the text highlighted?

Mime
View raw message