0

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:

enter image description here

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:

  1. 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.

  2. 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:

enter image description here

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!

Ash_stack
  • 13
  • 5
  • PDFs aren't really meant to be edited. If you need to have these sorts of fields to get filled programmatically, have you considered creating an Acroform, filling out the fields programmatically, and then flattening the Acroform into a regular PDF? – André Lemos May 24 '23 at 11:07
  • Hi - unfortunately the pdf is supplied so I can't control this and they wouldn't want to have to put fields on for me. I'm happy to stamp over some new text, it's just the alignment bit I'm struggling with. Having said that, I wonder if there's some way to convert a regular pdf to a form..... – Ash_stack May 24 '23 at 11:57
  • To me it seems that your requirements are contradicting each other - on one hand you want to position text exactly on those place holders, on the other hand you want to break lines which will shift everything below away from its respective place holder. – mkl May 24 '23 at 15:08
  • Hi - the field names are basically a placeholder in that the address I stamp on needs to start at the same place, but addresses will vary so some will wrap onto 2 lines, some may not have all the fields populated so will be shorter, some may need to suppress empty lines etc etc. As long as I get the top line of the data appearing at the same point as the top line of the merge fields, everybody is happy. – Ash_stack May 25 '23 at 07:57
  • This link: https://stackoverflow.com/questions/27906725/itext-placement-of-phrase-within-columntext shows to use the font ascender to get the text to the top of the table, so now I just need the height of the font so I can position the table at the right point. Can anyone help with this bit? – Ash_stack May 26 '23 at 08:38

1 Answers1

0

I still haven't found a 'proper' way to do this, but if it helps anyone else, a workaround is to stamp the columntext to a dummy pdf of the same dimensions as you want to use with a rectangle with (say) URY value of 500.

This will put the text lower down than 500 due to the margin as described above, so what I've then done is read the position again from the dummy pdf - let's say it comes back as 490.

I then just take the 490 from the 500 and get the difference of 10 which is the top margin. I then just stamp the text again to my 'real' pdf using 510 as the rectangle URY instead, to allow for the columntext margin.

This has the desired result of the text I stamp appearing at y = 500 and matching the position of the original - there MUST be an easier way, but this will do for me!

Ash_stack
  • 13
  • 5