Free Support Forum - aspose.com

Width / Height of FormattedText string?

I’m watermarking a PDF doc using Stamp.BindLogo and a FormattedText
object. I need to figure out placement (Stamp.SetOrigin?) based
on a number of settings, the page size (Pdf.PageSetup?), and the
width/height of the FormattedText string. I’d hoped to use
Pdf.GetStringWidth to help find the latter, but am not sure how to
convert the FontStyle enum that FormattedText takes to the string that
TextInfo takes for its Font member (as shown in the Pdf.GetStringWidth
example).



Can someone please show me how to convert a FontStyle enum to a “font”
string? Or, could someone illustrate a better way to achieve my
bigger objective of determining and setting the watermark text? Thanks very much!

Dear pmackay,

Thank you for considering Aspose.

We can add a method named GetStringWidth() in class FormattedText to get the string's width, GetStringHeight would be a little difficulty. What do yo want to do after get GetStringHeight() & GetStringWidth()?

Best regards.

Hi Georgie,



Thank you for the prompt response! My task is to position a text
watermark on a printed page, so I need 1) the viewable/printable page
dimensions, and
2) the string dimensions. The result of the calculations should
yield parameters to Stamp.SetOrigin, so all measurements should be in
PDF coordinate space. If you add “float
FormattedText.GetStringWidth()” and “float
FormattedText.GetStringHeight()” that gets me halfway there.



I’d like to do this within
Aspose.Pdf.Kit, but yesterday thought I found a way to find the page
size with Aspose.Pdf.PageSetup. My experiments yielded a
PageSetup.PageHeight of 842.0 and .PageWidth of 595.0, with
PageSetup.Margin = { 72.0, 90.0, 90.0, 72.0 }. Are these in PDF
coordinate space? The problem I encountered is that the watermark
would creep off the top of the page as my Stamp.SetOrigin ‘Y’
coordinate passed through 400-something. I had assumed I could
set the ‘Y’ origin at (842.0 - 72.0 - 12.0) for 12-point font and at
least see the bottom of the text.



Can you please tell me how to get the information I need to determine
placement within the page? Can you show me what I’m doing wrong
with PageSetup, if that’s indeed the data I need? Please forgive
my PDF ignorance!



Thanks Very Much,

Pete MacKay


Your way is right!

You can determine placement of the FormattedText by follow steps:

1. Getting the string' width and height through methods FormattedText.GetStringWidth() and FormattedText.GetStringHeight() (which the same of fontsize). (In FormattedText object, we do not consider newline)

2. Getting the page infomation through PageSetup class.

3.Determining the placement. The range of X you can choice to call the SetOrigin() is (PageSetup.Margin.Left, PageSetup.PageWidth - PageSetup.Margin.Right - GetStringWidth). The range of Y is (PageSetup.Margin.Bottom, PageSetup.PageHeight - PageSetup.Margin.Top - GetStringHeight())

For examples:
PageSize(842.0, 595.0)
Margin(72.0, 90.0, 90.0, 72.0)
FormattedText(height:12.0, width:167.4)
then,
range of X is (90.0, 595.0-90.0-167.4) = (90.0, 337.6)
range of Y is (72.0, 842.0-72.0-12.0) = (72.0, 758.0)
You can call Stamp.SetOrigin(100.0, 100.0) and it would not be out of page range.

The range of Y is (PageSetup.Margin.Bottom, PageSetup.PageHeight - PageSetup.Margin.Top - GetStringHeight()).



This doesn’t work; I tried it two days ago (my time zone). So I
experimented by increasing the Y parameter of SetOrigin until the
12-point text got to the top of the page. It was a little over
400 (as I mentioned), but not quite PageSetup.PageHeight / 2, which I
also tried. Sorry, but my boss is beating me up to look at the
Adobe SDK now. Thanks very much for your help.

More detail: the text is 12 point helvetica and the highest visible Y coordinate I can set it (using
SetOrigin) is 392.0 - not 758.0. Is there a way we can get the page
size information inside the Aspose.Pdf.Kit namespace? The
FormattedText functions only solve half the problem; fixing the page
size info is critical.



The last problem is that we need to wrap text that would otherwise go
off the right side of the page. Is there a way to watermark a PDF
file with a same-size PDF page? This one isn’t critical; the
first ones are and I only bought a few more days on this from our
Project Manager…



Thanks



P.S. I’m using C# and checked to make sure there are no float/int conversion issues.