When you do:
baseCanvas.ShowTextAligned("Some text", x, y, TextAlignment.CENTER, 0);
Then you want the coordinate (x, y)
to coincide with the middle of the text "some text"
.
In your code snippet, you are centering some text around the coordinate (555, 839)
and some text around the coordinate (40, 809)
which explains the difference.
Since you are using iText 7, why don't you take advantage of the fact that you can now easily position Paragraph
objects at absolute positions? The iText 7 jump-start tutorial for .NET already introduces some of the basic building blocks, but the Building blocks tutorial goes into more depth.
Take a look at the first example of chapter 2 and adapt it like this:
PdfPage page = pdf.AddNewPage();
PdfCanvas pdfCanvas = new PdfCanvas(page);
Rectangle rectangle = new Rectangle(36, 650, 100, 100);
Canvas canvas = new Canvas(pdfCanvas, pdf, rectangle);
PdfFont font = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);
PdfFont bold = PdfFontFactory.createFont(FontConstants.TIMES_BOLD);
Text title =
new Text("The Strange Case of Dr. Jekyll and Mr. Hyde").SetFont(bold);
Text author = new Text("Robert Louis Stevenson").SetFont(font);
Paragraph p = new Paragraph().Add(title).Add(" by ").Add(author);
p.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
canvas.Add(p);
canvas.Close();
This should add the text inside the rectangle (36, 650, 100, 100)
and center all content.