You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Eric Chow <er...@gmail.com> on 2013/03/19 03:42:53 UTC

Add a transparent rectangle to the existed PDF with the co-ordinates.

Hello,

How can I add a transparent yellow rectangle to a specific co-ordinate
(llx, lly, urx, ury) into an existed PDF?


In fact, I want to highlight some text that I know the exact position in
the PDF. Would you please to teach me?


Best regards,
Eric

Re: Add a transparent rectangle to the existed PDF with the co-ordinates.

Posted by Maruan Sahyoun <sa...@fileaffairs.de>.
Hi Eric,

take a look at http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/OverlayPDF.java?view=markup how to work with Over/Under content.

Kind regards

Maruan Sahyoun


Am 20.03.2013 um 09:54 schrieb Eric Chow <er...@gmail.com>:

> Dear Maruan,
> 
> Thank you very much. It works fine.
> I tried iText before and it can draw a rectangle by using UnderContent!
> 
> Is it possible to do this in PDFBox?
> 
> Best regards,
> Eric
> 
> 
> 
> 
> 
> 
> On Wed, Mar 20, 2013 at 3:34 PM, Maruan Sahyoun <sa...@fileaffairs.de>wrote:
> 
>> Hi Eric,
>> 
>> a slightly enhanced sample with transparency.
>> 
>> 
>>            // For simplicity the code provided doesn't have any null
>> checks or
>>            // exception handling !!
>> 
>>            PDDocument document = PDDocument.load(...);
>>            PDPage page = (PDPage)
>> document.getDocumentCatalog().getAllPages().get(0);
>> 
>>            // The transparency, opacity of graphic objects can't be set
>> directly on the drawing commands.
>>            // Instead we need to define a graphic state which will become
>> part of the
>>            // resources. That state can be called later prior to doing
>> the graphic operations.
>>            // That's part of the ISO/PDF specification [ISO-32000: 8.4.1]
>> 
>>            /* --------- Set up the graphic state -------------- */
>> 
>>            // Define a new extended graphic state
>>            PDExtendedGraphicsState extendedGraphicsState = new
>> PDExtendedGraphicsState();
>>            // Set the transparency/opacity
>>            extendedGraphicsState.setNonStrokingAlphaConstant(0.8f);
>>            // Get the page resources.
>>            PDResources resources = page.findResources();
>>            // Get the defined graphic states.
>>            Map graphicsStateDictionary = resources.getGraphicsStates();
>>            // Add the new state definition. The name is the reference we
>> need later on.
>>            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);
>>            // Call the graphic state using the name defined
>>            contentStream.appendRawCommands("/TransparentState gs\n");
>>            contentStream.setNonStrokingColor(Color.yellow);
>>            contentStream.fillRect(100, 100, 200, 200);
>>            contentStream.close();
>>            document.save(...);
>>            document.close();
>> 
>> 
>> Kind regards
>> 
>> Maruan Sahyoun
>> 
>> Am 20.03.2013 um 02:53 schrieb Eric Chow <er...@gmail.com>:
>> 
>>> Hi Maruan,
>>> 
>>> Thanks for your example. But I don't want to using annotation since it
>> can
>>> be deleted.
>>> 
>>> I want to really highlight the text by giving the co-ordinate? Would you
>>> please to show me a simple example?
>>> 
>>> Best regards,
>>> Eric
>>> 
>>> 
>>> 
>>> 
>>> On Tue, Mar 19, 2013 at 3:21 PM, Maruan Sahyoun <sahyoun@fileaffairs.de
>>> wrote:
>>> 
>>>> Hi Eric,
>>>> 
>>>> I did something similar for one of our customers. I didn't use a
>> rectangle
>>>> but a highlight annotation. A sample for annotations can be found at [1]
>>>> 
>>>> Kind regards
>>>> 
>>>> Maruan Sahyoun
>>>> 
>>>> 
>>>> 
>> http://svn.apache.org/repos/asf/pdfbox/branches/before-maven-layout/src/org/pdfbox/examples/pdmodel/Annotation.java
>>>> 
>>>> Am 19.03.2013 um 03:42 schrieb Eric Chow <er...@gmail.com>:
>>>> 
>>>>> Hello,
>>>>> 
>>>>> How can I add a transparent yellow rectangle to a specific co-ordinate
>>>>> (llx, lly, urx, ury) into an existed PDF?
>>>>> 
>>>>> 
>>>>> In fact, I want to highlight some text that I know the exact position
>> in
>>>>> the PDF. Would you please to teach me?
>>>>> 
>>>>> 
>>>>> Best regards,
>>>>> Eric
>>>> 
>>>> 
>> 
>> 


Re: Add a transparent rectangle to the existed PDF with the co-ordinates.

Posted by Eric Chow <er...@gmail.com>.
Dear Maruan,

Thank you very much. It works fine.
I tried iText before and it can draw a rectangle by using UnderContent!

Is it possible to do this in PDFBox?

Best regards,
Eric






On Wed, Mar 20, 2013 at 3:34 PM, Maruan Sahyoun <sa...@fileaffairs.de>wrote:

> Hi Eric,
>
> a slightly enhanced sample with transparency.
>
>
>             // For simplicity the code provided doesn't have any null
> checks or
>             // exception handling !!
>
>             PDDocument document = PDDocument.load(...);
>             PDPage page = (PDPage)
> document.getDocumentCatalog().getAllPages().get(0);
>
>             // The transparency, opacity of graphic objects can't be set
> directly on the drawing commands.
>             // Instead we need to define a graphic state which will become
> part of the
>             // resources. That state can be called later prior to doing
> the graphic operations.
>             // That's part of the ISO/PDF specification [ISO-32000: 8.4.1]
>
>             /* --------- Set up the graphic state -------------- */
>
>             // Define a new extended graphic state
>             PDExtendedGraphicsState extendedGraphicsState = new
> PDExtendedGraphicsState();
>             // Set the transparency/opacity
>             extendedGraphicsState.setNonStrokingAlphaConstant(0.8f);
>             // Get the page resources.
>             PDResources resources = page.findResources();
>             // Get the defined graphic states.
>             Map graphicsStateDictionary = resources.getGraphicsStates();
>             // Add the new state definition. The name is the reference we
> need later on.
>             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);
>             // Call the graphic state using the name defined
>             contentStream.appendRawCommands("/TransparentState gs\n");
>             contentStream.setNonStrokingColor(Color.yellow);
>             contentStream.fillRect(100, 100, 200, 200);
>             contentStream.close();
>             document.save(...);
>             document.close();
>
>
> Kind regards
>
> Maruan Sahyoun
>
> Am 20.03.2013 um 02:53 schrieb Eric Chow <er...@gmail.com>:
>
> > Hi Maruan,
> >
> > Thanks for your example. But I don't want to using annotation since it
> can
> > be deleted.
> >
> > I want to really highlight the text by giving the co-ordinate? Would you
> > please to show me a simple example?
> >
> > Best regards,
> > Eric
> >
> >
> >
> >
> > On Tue, Mar 19, 2013 at 3:21 PM, Maruan Sahyoun <sahyoun@fileaffairs.de
> >wrote:
> >
> >> Hi Eric,
> >>
> >> I did something similar for one of our customers. I didn't use a
> rectangle
> >> but a highlight annotation. A sample for annotations can be found at [1]
> >>
> >> Kind regards
> >>
> >> Maruan Sahyoun
> >>
> >>
> >>
> http://svn.apache.org/repos/asf/pdfbox/branches/before-maven-layout/src/org/pdfbox/examples/pdmodel/Annotation.java
> >>
> >> Am 19.03.2013 um 03:42 schrieb Eric Chow <er...@gmail.com>:
> >>
> >>> Hello,
> >>>
> >>> How can I add a transparent yellow rectangle to a specific co-ordinate
> >>> (llx, lly, urx, ury) into an existed PDF?
> >>>
> >>>
> >>> In fact, I want to highlight some text that I know the exact position
> in
> >>> the PDF. Would you please to teach me?
> >>>
> >>>
> >>> Best regards,
> >>> Eric
> >>
> >>
>
>

Re: Add a transparent rectangle to the existed PDF with the co-ordinates.

Posted by Maruan Sahyoun <sa...@fileaffairs.de>.
Hi Eric,

a slightly enhanced sample with transparency.


            // For simplicity the code provided doesn't have any null checks or 
            // exception handling !!

            PDDocument document = PDDocument.load(...);
            PDPage page = (PDPage) document.getDocumentCatalog().getAllPages().get(0);
            
            // The transparency, opacity of graphic objects can't be set directly on the drawing commands.
            // Instead we need to define a graphic state which will become part of the 
            // resources. That state can be called later prior to doing the graphic operations.
            // That's part of the ISO/PDF specification [ISO-32000: 8.4.1]
 
            /* --------- Set up the graphic state -------------- */

            // Define a new extended graphic state
            PDExtendedGraphicsState extendedGraphicsState = new PDExtendedGraphicsState();
            // Set the transparency/opacity
            extendedGraphicsState.setNonStrokingAlphaConstant(0.8f);
            // Get the page resources.
            PDResources resources = page.findResources();
            // Get the defined graphic states.
            Map graphicsStateDictionary = resources.getGraphicsStates();
            // Add the new state definition. The name is the reference we need later on.
            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);
            // Call the graphic state using the name defined
            contentStream.appendRawCommands("/TransparentState gs\n");
            contentStream.setNonStrokingColor(Color.yellow);
            contentStream.fillRect(100, 100, 200, 200);
            contentStream.close();
            document.save(...);
            document.close();


Kind regards

Maruan Sahyoun

Am 20.03.2013 um 02:53 schrieb Eric Chow <er...@gmail.com>:

> Hi Maruan,
> 
> Thanks for your example. But I don't want to using annotation since it can
> be deleted.
> 
> I want to really highlight the text by giving the co-ordinate? Would you
> please to show me a simple example?
> 
> Best regards,
> Eric
> 
> 
> 
> 
> On Tue, Mar 19, 2013 at 3:21 PM, Maruan Sahyoun <sa...@fileaffairs.de>wrote:
> 
>> Hi Eric,
>> 
>> I did something similar for one of our customers. I didn't use a rectangle
>> but a highlight annotation. A sample for annotations can be found at [1]
>> 
>> Kind regards
>> 
>> Maruan Sahyoun
>> 
>> 
>> http://svn.apache.org/repos/asf/pdfbox/branches/before-maven-layout/src/org/pdfbox/examples/pdmodel/Annotation.java
>> 
>> Am 19.03.2013 um 03:42 schrieb Eric Chow <er...@gmail.com>:
>> 
>>> Hello,
>>> 
>>> How can I add a transparent yellow rectangle to a specific co-ordinate
>>> (llx, lly, urx, ury) into an existed PDF?
>>> 
>>> 
>>> In fact, I want to highlight some text that I know the exact position in
>>> the PDF. Would you please to teach me?
>>> 
>>> 
>>> Best regards,
>>> Eric
>> 
>> 


Re: Add a transparent rectangle to the existed PDF with the co-ordinates.

Posted by Maruan Sahyoun <sa...@fileaffairs.de>.
Hi Eric,

            PDDocument document = PDDocument.load(provide filename, file ….);
            PDPage page = (PDPage) document.getDocumentCatalog().getAllPages().get(0);
            PDPageContentStream contentStream = new PDPageContentStream(document, page,true,true);  //append the content stream
            contentStream.setStrokingColor(66, 177, 230);  
            contentStream.setStrokingColor(66, 177, 230);  
            contentStream.drawLine(100, 100, 200, 200);
            contentStream.fillRect(100, 100, 200, 200);
            contentStream.close();
            document.save(provide filename, file ….);
            document.close();


Maruan Sahyoun

Am 20.03.2013 um 02:53 schrieb Eric Chow <er...@gmail.com>:

> Hi Maruan,
> 
> Thanks for your example. But I don't want to using annotation since it can
> be deleted.
> 
> I want to really highlight the text by giving the co-ordinate? Would you
> please to show me a simple example?
> 
> Best regards,
> Eric
> 
> 
> 
> 
> On Tue, Mar 19, 2013 at 3:21 PM, Maruan Sahyoun <sa...@fileaffairs.de>wrote:
> 
>> Hi Eric,
>> 
>> I did something similar for one of our customers. I didn't use a rectangle
>> but a highlight annotation. A sample for annotations can be found at [1]
>> 
>> Kind regards
>> 
>> Maruan Sahyoun
>> 
>> 
>> http://svn.apache.org/repos/asf/pdfbox/branches/before-maven-layout/src/org/pdfbox/examples/pdmodel/Annotation.java
>> 
>> Am 19.03.2013 um 03:42 schrieb Eric Chow <er...@gmail.com>:
>> 
>>> Hello,
>>> 
>>> How can I add a transparent yellow rectangle to a specific co-ordinate
>>> (llx, lly, urx, ury) into an existed PDF?
>>> 
>>> 
>>> In fact, I want to highlight some text that I know the exact position in
>>> the PDF. Would you please to teach me?
>>> 
>>> 
>>> Best regards,
>>> Eric
>> 
>> 


Re: Add a transparent rectangle to the existed PDF with the co-ordinates.

Posted by Eric Chow <er...@gmail.com>.
Hi Maruan,

Thanks for your example. But I don't want to using annotation since it can
be deleted.

I want to really highlight the text by giving the co-ordinate? Would you
please to show me a simple example?

Best regards,
Eric




On Tue, Mar 19, 2013 at 3:21 PM, Maruan Sahyoun <sa...@fileaffairs.de>wrote:

> Hi Eric,
>
> I did something similar for one of our customers. I didn't use a rectangle
> but a highlight annotation. A sample for annotations can be found at [1]
>
> Kind regards
>
> Maruan Sahyoun
>
>
> http://svn.apache.org/repos/asf/pdfbox/branches/before-maven-layout/src/org/pdfbox/examples/pdmodel/Annotation.java
>
> Am 19.03.2013 um 03:42 schrieb Eric Chow <er...@gmail.com>:
>
> > Hello,
> >
> > How can I add a transparent yellow rectangle to a specific co-ordinate
> > (llx, lly, urx, ury) into an existed PDF?
> >
> >
> > In fact, I want to highlight some text that I know the exact position in
> > the PDF. Would you please to teach me?
> >
> >
> > Best regards,
> > Eric
>
>

Re: Add a transparent rectangle to the existed PDF with the co-ordinates.

Posted by Maruan Sahyoun <sa...@fileaffairs.de>.
Hi Eric,

I did something similar for one of our customers. I didn't use a rectangle but a highlight annotation. A sample for annotations can be found at [1]

Kind regards

Maruan Sahyoun

http://svn.apache.org/repos/asf/pdfbox/branches/before-maven-layout/src/org/pdfbox/examples/pdmodel/Annotation.java

Am 19.03.2013 um 03:42 schrieb Eric Chow <er...@gmail.com>:

> Hello,
> 
> How can I add a transparent yellow rectangle to a specific co-ordinate
> (llx, lly, urx, ury) into an existed PDF?
> 
> 
> In fact, I want to highlight some text that I know the exact position in
> the PDF. Would you please to teach me?
> 
> 
> Best regards,
> Eric