pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Maruan Sahyoun <sahy...@fileaffairs.de>
Subject Re: Saving PDFHighlighter selection to PDF
Date Thu, 27 Feb 2014 08:09:18 GMT
Hi,

if you are using Overlay.java please use OverlayPDF.java instead. Overlay.java will be removed
in the next major release.

To add some highlight rectangle handling it yourself you can use

     PDDocument document = PDDocument.loadNonSeq( ... );
     PDPage page = (PDPage) document.getDocumentCatalog().getAllPages().get(0);
        /* Set up the graphic state */

     // Define a new extended graphic state
     PDExtendedGraphicsState extendedGraphicsState = new PDExtendedGraphicsState();
     // Set the transparency/opacity
     extendedGraphicsState.setNonStrokingAlphaConstant(0.5f);
     // Get the page resources.
     PDResources resources = page.findResources();

     // Get the defined graphic states.
     Map<String, PDExtendedGraphicsState> graphicsStateDictionary = resources.getGraphicsStates();
     
     if (graphicsStateDictionary == null) {
    	 graphicsStateDictionary = new HashMap<String, PDExtendedGraphicsState>();
     }
     
     graphicsStateDictionary.put("TransparentState", extendedGraphicsState);
     resources.setGraphicsStates(graphicsStateDictionary);

     /* End of setup */

     // Now we will be able to call the state definition before doing the drawing
     PDPageContentStream contentStream = new PDPageContentStream(document, page,true,true);

     contentStream.appendRawCommands("/TransparentState gs\n");
     contentStream.setNonStrokingColor(Color.yellow);

     // put in the coordinates for the rectangle found by the text extraction
     contentStream.fillRect(100f,100f,100f,100f);
     contentStream.close();

     document.save( ... );

null checks, error handling etc. is omitted so the code is clearer. 

For the annotations - how did you print the PDF? If you print it using e.g. Adobe Reader printing
the annotations is optional and has to be activated in the printer dialog, otherwise they
will be omitted. 

BR
Maruan Sahyoun



Am 26.02.2014 um 23:48 schrieb Nikos Chantziaras <realnc@gmail.com>:

> That doesn't work, it turns out. Only the test PDFs were working. Trying to overlay "real"
PDFs result in an error that Cos Arrays are not supported.
> 
> So I then tried to use PDF annotations instead. This seemed to work fine. However, when
someone tried to print the PDF, annotated text was not highlighted at all, even though it
looks just fine on screen!
> 
> So I'm out of ideas. It would appear that PDFBox is indeed incapable of highlighting
text?
> 
> 
> On 17/02/14 17:18, Nikos Chantziaras wrote:
>> 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.)
>> 
>> 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
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message