I'm trying to some postal address text on a pdf at the same position as text showing the fieldname needed. Sorry if this is a bit wordy, I just want to try to cover everything:
The pdf looks like this:
I've got some code to detect the position of the %Title% field which gives me x=405 and y=311. I've converted these point values to mm and measured the distances in a pdf reader and they are OK, matching the bottom and left edges of the text.
Now I need to put a variable address on the pdf so it starts from the same position, 405, 311.
I can do this if I use showtextaligned commands for each line, but I need to use columntext so I can control the area used better and wrap around any lines that are too long. For example, if I have data with a field like this:
1 High Street round the back of the chip shop
I need it to wrap at a certain x value so I get:
1 High Street round the
back of the chip shop
I also need to have the address top aligned and be able to change font information on a per-field basis. That's led me to using phrases to get the font info and a table cell to have control over the alignment.
I have 2 problems with getting the text I stamp on the pdf to align exactly with the %Title% field:
Using the 311 y value to draw a rectangle puts the top of the rectangle at the bottom of the %Title% field, so presumably I need to adjust it by the height of the text, but I cant find out how to measure this height. I thought this was just the point size, but it's not.
Once I've done that, I've aligned the text to the top of cell with ALIGN_TOP, but there's a bit of padding which is stopping it going right to the top. I can adjust this with cell.SetLeading(6, 0) to visually get it right (ish), but I need this to work with different font sizes, so I can't just hard-code the the value, it needs to be derived from the font, but I'm not sure how to do this either.
Here's a pic to show what I'm seeing - I've offset the stamp it a bit to the left so it's not directly on top and left the border on for clarity:
Here's the bits of my code we are looking at:
Dim combi_area_rect As New Rectangle(LLX, LLY, URX, URY)
Dim ct_combi As New ColumnText(canvas)
ct_combi.SetSimpleColumn(combi_area_rect)
Dim fontinfo As New Font
Dim basecol As New BaseColor(35, 31, 32)
fontinfo = FontFactory.GetFont("c:\windows\fonts\Calibri.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED, 12, style:=0, basecol)
myphrase = New Phrase(mytext, fontinfo)
combi_phrase_list.Add(myphrase)
Dim table As New PdfPTable(1)
table.SetWidths({1})
table.WidthPercentage = 100
Dim cell As New PdfPCell
cell.HorizontalAlignment = Element.ALIGN_LEFT
cell.VerticalAlignment = Element.ALIGN_TOP
cell.Phrase = combi_phrase_list(0)
cell.FixedHeight = 100
table.AddCell(cell)
ct_combi.AddElement(table)
ct_combi.Go()
This is just using one phrase for testing purposes, not the whole address fields.
Could anyone give some guidance please on how to get the text within the table to align with the original text on the pdf? - am I using the right approach to have a table cell anyway?
Thanks for reading!