How to place a text on specific location?

Hello,

we have a rather simple quest to solve but somehow it seems to be harder than thought… :slight_smile:

I have a PDF with five circles and I want to add programatically one text in each of the circles.
The text should be centered within the circles.

I tried to use TextStamp but somehow I a not able to find a good way to place this text centered to given coordinates (meaning: not the lower left point of the “text box” shoudl be in that given coordinates but the vertical and horizontal caenter of that text)

My first thought was to substract the half of the text height to the y coordinate and the half of the text length to the x coordinate

(text length calculated like this: Dim textWidth As Double = theFont.MeasureString(theText, textHeight))

But somehow did not get the correct results.

I tried also:
textStamp.TextAlignment = Aspose.Pdf.HorizontalAlignment.FullJustify
textStamp.HorizontalAlignment = HorizontalAlignment.Center
textStamp.VerticalAlignment = VerticalAlignment.Center

But this also did not seem to work.

And is it correcto to define the placement coordinates like this:
textStamp.XIndent = m_X
textStamp.YIndent = m_Y
?

Is there any better way?

Edit: I based my solution on this: Add Text Stamp to PDF using Python | Aspose.PDF

regards,
Johann

@hartit
You can see the answer to a similar question Add text on specific position
True, it’s in Java, but I think the meaning is clear.
If you have any questions about this, write in this topic.

Thanks I will try to “rework” my current code to try this approach.
I think I also struggle with the proper coordinates. It seems that there is no working way for me to get e.g. the origin in the lower left corner and a maximum x and y value because the page sizes seem to be “different”…
Wahat is the best way to get the “real” boundaries including everything that “margins” etc. might influence…?

Meanwhile I also solved my other problems with this task and I think I am on a good way!

@hartit
The newly created page has its origin point in the lower left corner. The X axis goes to the right, the Y axis goes up.
The code below (in C#) shows this, as well as getting the size occupied by the text.

var document = new Document();
Page page = document.Pages.Add();
var textFragment = new TextFragment();
textFragment.Text = "This is my text";

// These values cause text to be displayed in the bottom left corner of the page
// textFragment.Position.XIndent = 0;
// textFragment.Position.YIndent = 0;

// These values cause text to be displayed in the top right corner of the page
textFragment.Position.XIndent = PageSize.A4.Width - textFragment.Rectangle.Width;
textFragment.Position.YIndent = PageSize.A4.Height - textFragment.Rectangle.Height;

var par1 = new TextParagraph();
par1.Position = new Position(textFragment.Position.XIndent, textFragment.Position.YIndent); 
par1.AppendLine(textFragment);
var textBuilder1 = new TextBuilder(page);
textBuilder1.AppendParagraph(par1);

document.Save(dataDir + "Test_19.7.pdf");

However, this may vary by specifying transformations and may be different for a specific page.

1 Like

One of my mistakes was that I did not specif the position for the paragraph itself so it seems that specifying the position has no effect…

@hartit
I hope everything worked out for you now?