You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Gilad Denneboom <gi...@gmail.com> on 2017/01/24 21:53:48 UTC

Move rotated text to center of page

Hi all,

I'm using a PDPageContentStream to create a watermark which is rotated by
45 or 315 degrees, and I would like to place it at the center of the page's
media box, but I can't seem to get it to work because the setTextRotation
function moves the bottom-left corner of the box, and I can't figure out
how the moveTextPositionByAmount works...
Basically what I would like is to be able to calculate the height and width
of the text, so I could figure out where on the page to place it so that
the center of both objects are aligned. Any tips how to achieve that?

Thanks in advance!

Re: Move rotated text to center of page

Posted by Gilad Denneboom <gi...@gmail.com>.
Yes, I'm familiar with the Pythagorean theorem...

On Tue, Jan 24, 2017 at 11:30 PM, Tilman Hausherr <TH...@t-online.de>
wrote:

> Am 24.01.2017 um 23:20 schrieb Gilad Denneboom:
>
>> Thanks, but surely PDFont.getStringWidth() will return the width of the
>> text string on its own, not of the rect that surrounds it when it is
>> rotated, right?
>>
>
> No. You'll need to so some math (cosine).
>
> https://en.wikipedia.org/wiki/Trigonometric_functions
>
> Tilman
>
>
>
>
>> On Tue, Jan 24, 2017 at 11:02 PM, Tilman Hausherr <TH...@t-online.de>
>> wrote:
>>
>> Am 24.01.2017 um 22:53 schrieb Gilad Denneboom:
>>>
>>> I can't figure out
>>>> how the moveTextPositionByAmount works...
>>>>
>>>> It is relative to the previous position since the start of the text
>>> mode.
>>> It starts at 0,0.
>>>
>>> Basically what I would like is to be able to calculate the height and
>>> width
>>>
>>>> of the text, so I could figure out where on the page to place it so that
>>>> the center of both objects are aligned.
>>>>
>>>>
>>> See the DrawPrintTextLocations example on how to calculate the height.
>>> It's quite tricky. The width you can get by PDFont.getStringWidth().
>>>
>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
>>> For additional commands, e-mail: users-help@pdfbox.apache.org
>>>
>>>
>>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
>

Re: Move rotated text to center of page

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 24.01.2017 um 23:20 schrieb Gilad Denneboom:
> Thanks, but surely PDFont.getStringWidth() will return the width of the
> text string on its own, not of the rect that surrounds it when it is
> rotated, right?

No. You'll need to so some math (cosine).

https://en.wikipedia.org/wiki/Trigonometric_functions

Tilman


>
> On Tue, Jan 24, 2017 at 11:02 PM, Tilman Hausherr <TH...@t-online.de>
> wrote:
>
>> Am 24.01.2017 um 22:53 schrieb Gilad Denneboom:
>>
>>> I can't figure out
>>> how the moveTextPositionByAmount works...
>>>
>> It is relative to the previous position since the start of the text mode.
>> It starts at 0,0.
>>
>> Basically what I would like is to be able to calculate the height and width
>>> of the text, so I could figure out where on the page to place it so that
>>> the center of both objects are aligned.
>>>
>>
>> See the DrawPrintTextLocations example on how to calculate the height.
>> It's quite tricky. The width you can get by PDFont.getStringWidth().
>>
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
>> For additional commands, e-mail: users-help@pdfbox.apache.org
>>
>>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Move rotated text to center of page

Posted by Gilad Denneboom <gi...@gmail.com>.
Thanks, but surely PDFont.getStringWidth() will return the width of the
text string on its own, not of the rect that surrounds it when it is
rotated, right?

On Tue, Jan 24, 2017 at 11:02 PM, Tilman Hausherr <TH...@t-online.de>
wrote:

> Am 24.01.2017 um 22:53 schrieb Gilad Denneboom:
>
>> I can't figure out
>> how the moveTextPositionByAmount works...
>>
>
> It is relative to the previous position since the start of the text mode.
> It starts at 0,0.
>
> Basically what I would like is to be able to calculate the height and width
>> of the text, so I could figure out where on the page to place it so that
>> the center of both objects are aligned.
>>
>
>
> See the DrawPrintTextLocations example on how to calculate the height.
> It's quite tricky. The width you can get by PDFont.getStringWidth().
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
>

Re: Move rotated text to center of page

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 24.01.2017 um 22:53 schrieb Gilad Denneboom:
> I can't figure out
> how the moveTextPositionByAmount works...

It is relative to the previous position since the start of the text 
mode. It starts at 0,0.

> Basically what I would like is to be able to calculate the height and width
> of the text, so I could figure out where on the page to place it so that
> the center of both objects are aligned.


See the DrawPrintTextLocations example on how to calculate the height. 
It's quite tricky. The width you can get by PDFont.getStringWidth().




---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Move rotated text to center of page

Posted by Gilad Denneboom <gi...@gmail.com>.
Old habits die hard, you know... But thanks, I switched this project over
to v2 and it's working fine.

On Wed, Jan 25, 2017 at 4:57 PM, Tilman Hausherr <TH...@t-online.de>
wrote:

> Am 24.01.2017 um 23:42 schrieb Gilad Denneboom:
>
>> Is this a new feature? I was using version 1.8.13 and couldn't find it. I
>> guess I'll need to switch to version 2.x.x ...
>>
>
>
> Yes it is new. It was available in low level in 1.8.* but the code is much
> larger:
>
>     PDExtendedGraphicsState gs0 = new PDExtendedGraphicsState();
>     gs0.setNonStrokingAlphaConstant(value);
>     Map<String, PDExtendedGraphicsState> graphicsStatesMap =
> page.getResources().getGraphicsStates();
>     if (graphicsStatesMap == null)
>     {
>        graphicsStatesMap = new HashMap<String, PDExtendedGraphicsState>();
>     }
>     graphicsStatesMap.put("gs0", g0);
>     page.getResources().setGraphicsStates(graphicsStatesMap);
>     contentStream.appendRawCommands("/gs0 gs\n");
>
> Better update to 2.0.* so that all the work we put into this has a purpose.
>
>
> Tilman
>
>
>
>> On Tue, Jan 24, 2017 at 11:35 PM, Tilman Hausherr <TH...@t-online.de>
>> wrote:
>>
>> Am 24.01.2017 um 23:26 schrieb Gilad Denneboom:
>>>
>>> Thanks again, but I'm not seeing any examples on how to use
>>>> setGraphicsStateParameters (or is it SetGraphicsStateParameters?) in
>>>> this
>>>> context... Could you help me out?
>>>>
>>>> You just call PDPageContentStream.setGraphicsStateParameters() with the
>>> variable.
>>>
>>> https://stackoverflow.com/questions/38433534/pdfbox-inconsis
>>> tent-co-ordinate-system-how-do-i-draw-a-rectangle
>>> https://stackoverflow.com/questions/39609617/how-to-add-text
>>> -watermark-to-pdf-in-java-using-apache-pdfbox
>>> https://stackoverflow.com/questions/40267719/pdfbox-2-1-0-
>>> when-printed-from-ie11-transparent-text-watermark-has-opaque-ba
>>>
>>> Tilman
>>>
>>>
>>>
>>> On Tue, Jan 24, 2017 at 11:05 PM, Tilman Hausherr <THausherr@t-online.de
>>>> >
>>>> wrote:
>>>>
>>>> Am 24.01.2017 um 22:58 schrieb Gilad Denneboom:
>>>>
>>>>> One other thing: Is it possible to set the text's opacity level?
>>>>>
>>>>>>
>>>>>>               PDExtendedGraphicsState gs0 = new
>>>>> PDExtendedGraphicsState();
>>>>>               gs0.setNonStrokingAlphaConstant(value);
>>>>>
>>>>> then assign this extended graphics state (outside of the text mode)
>>>>> with
>>>>> setGraphicsStateParameters().
>>>>>
>>>>> Tilman
>>>>>
>>>>>
>>>>>
>>>>> ---------------------------------------------------------------------
>>>>> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
>>>>> For additional commands, e-mail: users-help@pdfbox.apache.org
>>>>>
>>>>>
>>>>>
>>>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
>>> For additional commands, e-mail: users-help@pdfbox.apache.org
>>>
>>>
>>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
>

Re: Move rotated text to center of page

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 24.01.2017 um 23:42 schrieb Gilad Denneboom:
> Is this a new feature? I was using version 1.8.13 and couldn't find it. I
> guess I'll need to switch to version 2.x.x ...


Yes it is new. It was available in low level in 1.8.* but the code is 
much larger:

     PDExtendedGraphicsState gs0 = new PDExtendedGraphicsState();
     gs0.setNonStrokingAlphaConstant(value);
     Map<String, PDExtendedGraphicsState> graphicsStatesMap = 
page.getResources().getGraphicsStates();
     if (graphicsStatesMap == null)
     {
        graphicsStatesMap = new HashMap<String, PDExtendedGraphicsState>();
     }
     graphicsStatesMap.put("gs0", g0);
     page.getResources().setGraphicsStates(graphicsStatesMap);
     contentStream.appendRawCommands("/gs0 gs\n");

Better update to 2.0.* so that all the work we put into this has a purpose.

Tilman


>
> On Tue, Jan 24, 2017 at 11:35 PM, Tilman Hausherr <TH...@t-online.de>
> wrote:
>
>> Am 24.01.2017 um 23:26 schrieb Gilad Denneboom:
>>
>>> Thanks again, but I'm not seeing any examples on how to use
>>> setGraphicsStateParameters (or is it SetGraphicsStateParameters?) in this
>>> context... Could you help me out?
>>>
>> You just call PDPageContentStream.setGraphicsStateParameters() with the
>> variable.
>>
>> https://stackoverflow.com/questions/38433534/pdfbox-inconsis
>> tent-co-ordinate-system-how-do-i-draw-a-rectangle
>> https://stackoverflow.com/questions/39609617/how-to-add-text
>> -watermark-to-pdf-in-java-using-apache-pdfbox
>> https://stackoverflow.com/questions/40267719/pdfbox-2-1-0-
>> when-printed-from-ie11-transparent-text-watermark-has-opaque-ba
>>
>> Tilman
>>
>>
>>
>>> On Tue, Jan 24, 2017 at 11:05 PM, Tilman Hausherr <TH...@t-online.de>
>>> wrote:
>>>
>>> Am 24.01.2017 um 22:58 schrieb Gilad Denneboom:
>>>> One other thing: Is it possible to set the text's opacity level?
>>>>>
>>>>               PDExtendedGraphicsState gs0 = new PDExtendedGraphicsState();
>>>>               gs0.setNonStrokingAlphaConstant(value);
>>>>
>>>> then assign this extended graphics state (outside of the text mode) with
>>>> setGraphicsStateParameters().
>>>>
>>>> Tilman
>>>>
>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
>>>> For additional commands, e-mail: users-help@pdfbox.apache.org
>>>>
>>>>
>>>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
>> For additional commands, e-mail: users-help@pdfbox.apache.org
>>
>>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Move rotated text to center of page

Posted by Gilad Denneboom <gi...@gmail.com>.
Is this a new feature? I was using version 1.8.13 and couldn't find it. I
guess I'll need to switch to version 2.x.x ...

On Tue, Jan 24, 2017 at 11:35 PM, Tilman Hausherr <TH...@t-online.de>
wrote:

> Am 24.01.2017 um 23:26 schrieb Gilad Denneboom:
>
>> Thanks again, but I'm not seeing any examples on how to use
>> setGraphicsStateParameters (or is it SetGraphicsStateParameters?) in this
>> context... Could you help me out?
>>
>
> You just call PDPageContentStream.setGraphicsStateParameters() with the
> variable.
>
> https://stackoverflow.com/questions/38433534/pdfbox-inconsis
> tent-co-ordinate-system-how-do-i-draw-a-rectangle
> https://stackoverflow.com/questions/39609617/how-to-add-text
> -watermark-to-pdf-in-java-using-apache-pdfbox
> https://stackoverflow.com/questions/40267719/pdfbox-2-1-0-
> when-printed-from-ie11-transparent-text-watermark-has-opaque-ba
>
> Tilman
>
>
>
>> On Tue, Jan 24, 2017 at 11:05 PM, Tilman Hausherr <TH...@t-online.de>
>> wrote:
>>
>> Am 24.01.2017 um 22:58 schrieb Gilad Denneboom:
>>>
>>> One other thing: Is it possible to set the text's opacity level?
>>>>
>>>>
>>>              PDExtendedGraphicsState gs0 = new PDExtendedGraphicsState();
>>>              gs0.setNonStrokingAlphaConstant(value);
>>>
>>> then assign this extended graphics state (outside of the text mode) with
>>> setGraphicsStateParameters().
>>>
>>> Tilman
>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
>>> For additional commands, e-mail: users-help@pdfbox.apache.org
>>>
>>>
>>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
>

Re: Move rotated text to center of page

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 24.01.2017 um 23:26 schrieb Gilad Denneboom:
> Thanks again, but I'm not seeing any examples on how to use
> setGraphicsStateParameters (or is it SetGraphicsStateParameters?) in this
> context... Could you help me out?

You just call PDPageContentStream.setGraphicsStateParameters() with the 
variable.

https://stackoverflow.com/questions/38433534/pdfbox-inconsistent-co-ordinate-system-how-do-i-draw-a-rectangle
https://stackoverflow.com/questions/39609617/how-to-add-text-watermark-to-pdf-in-java-using-apache-pdfbox
https://stackoverflow.com/questions/40267719/pdfbox-2-1-0-when-printed-from-ie11-transparent-text-watermark-has-opaque-ba

Tilman

>
> On Tue, Jan 24, 2017 at 11:05 PM, Tilman Hausherr <TH...@t-online.de>
> wrote:
>
>> Am 24.01.2017 um 22:58 schrieb Gilad Denneboom:
>>
>>> One other thing: Is it possible to set the text's opacity level?
>>>
>>
>>              PDExtendedGraphicsState gs0 = new PDExtendedGraphicsState();
>>              gs0.setNonStrokingAlphaConstant(value);
>>
>> then assign this extended graphics state (outside of the text mode) with
>> setGraphicsStateParameters().
>>
>> Tilman
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
>> For additional commands, e-mail: users-help@pdfbox.apache.org
>>
>>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Move rotated text to center of page

Posted by Gilad Denneboom <gi...@gmail.com>.
Thanks again, but I'm not seeing any examples on how to use
setGraphicsStateParameters (or is it SetGraphicsStateParameters?) in this
context... Could you help me out?

On Tue, Jan 24, 2017 at 11:05 PM, Tilman Hausherr <TH...@t-online.de>
wrote:

> Am 24.01.2017 um 22:58 schrieb Gilad Denneboom:
>
>> One other thing: Is it possible to set the text's opacity level?
>>
>
>
>             PDExtendedGraphicsState gs0 = new PDExtendedGraphicsState();
>             gs0.setNonStrokingAlphaConstant(value);
>
> then assign this extended graphics state (outside of the text mode) with
> setGraphicsStateParameters().
>
> Tilman
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
>

Re: Move rotated text to center of page

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 24.01.2017 um 22:58 schrieb Gilad Denneboom:
> One other thing: Is it possible to set the text's opacity level?


             PDExtendedGraphicsState gs0 = new PDExtendedGraphicsState();
             gs0.setNonStrokingAlphaConstant(value);

then assign this extended graphics state (outside of the text mode) with 
setGraphicsStateParameters().

Tilman


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Move rotated text to center of page

Posted by Gilad Denneboom <gi...@gmail.com>.
One other thing: Is it possible to set the text's opacity level?

On Tue, Jan 24, 2017 at 10:53 PM, Gilad Denneboom <gilad.denneboom@gmail.com
> wrote:

> Hi all,
>
> I'm using a PDPageContentStream to create a watermark which is rotated by
> 45 or 315 degrees, and I would like to place it at the center of the page's
> media box, but I can't seem to get it to work because the setTextRotation
> function moves the bottom-left corner of the box, and I can't figure out
> how the moveTextPositionByAmount works...
> Basically what I would like is to be able to calculate the height and
> width of the text, so I could figure out where on the page to place it so
> that the center of both objects are aligned. Any tips how to achieve that?
>
> Thanks in advance!
>