在iTextSharp中,将一个表格单元格盖章在PDF上以匹配现有文本的位置。

huangapple go评论106阅读模式
英文:

Stamping a table cell on a pdf to match position of existing text in itextsharp

问题

I understand that you want a translation of the code-related content. Here's the translated part:

我正在尝试将一些邮寄地址文本放在PDF中,与显示所需字段名称的文本位置相同。如果这有点啰嗦,我只是想尽量涵盖所有内容:

PDF文件如下所示:

[![enter image description here][1]][1]

我已经编写了一些代码来检测%Title%字段的位置,它给出了x=405和y=311。我已将这些点值转换为毫米,并在PDF阅读器中测量了距离,它们都正确,与文本的底部和左边缘匹配。

现在,我需要在PDF上放置一个可变地址,使其从相同的位置开始,即405,311。

如果我使用showtextaligned命令为每一行设置文本,我可以做到这一点,但我需要使用columntext以更好地控制使用的区域,并自动换行任何太长的行。例如,如果我有一个字段的数据如下:

**1 High Street round the back of the chip shop** 

我需要它在某个x值处换行,以便我得到:

**1 High Street round the**

**back of the chip shop**

我还需要使地址顶部对齐,并能够根据每个字段更改字体信息。这导致我使用短语获取字体信息并使用表格单元格来控制对齐方式。

我在将文本盖在PDF上时遇到了两个问题,无法与%Title%字段完全对齐:

1. 使用y值311来绘制矩形会将矩形的顶部放在%Title%字段的底部,因此我可能需要通过文本的高度进行调整,但我无法找到如何测量此高度的方法。我以为这只是点大小,但实际上不是。

2. 一旦我完成了上述操作,我已将文本与ALIGN_TOP单元格的顶部对齐,但有一些填充会阻止它完全靠近顶部。我可以通过使用cell.SetLeading(6, 0)进行视觉调整(大致),但我需要它能够与不同的字体大小一起工作,因此不能只是硬编码的值,它需要从字体中派生出来,但我不确定如何做到这一点。

这里有一张图片显示了我看到的情况 - 我将印章向左移了一点,以使其不直接在上面,但保留了边框以便更清晰:

[![enter image description here][2]][2]

这是我们关注的我的代码部分:

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()

这仅用于测试目的,而不是整个地址字段。

是否有人能提供一些关于如何使表中的文本与PDF上的原始文本对齐的指导? - 我是否使用了正确的方法来使用表格单元格?
谢谢阅读!

Please note that this is the translated code-related portion of your text. If you have any specific questions or need further assistance with this code, feel free to ask.

英文:

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:

在iTextSharp中,将一个表格单元格盖章在PDF上以匹配现有文本的位置。

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:

在iTextSharp中,将一个表格单元格盖章在PDF上以匹配现有文本的位置。

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!

答案1

得分: 0

我仍然没有找到一种“合适”的方法来做到这一点,但如果对其他人有帮助,一种解决方法是将列文本盖在与要使用的相同尺寸的虚拟 PDF 上,带有 URY 值为 500 的矩形。

这将由于上述边距将文本放置在低于 500 的位置,因此我随后从虚拟 PDF 中再次读取位置 - 假设返回的值为 490。

然后,我只需从 500 中减去 490 并得到差值为 10,这就是顶部边距。然后,我再次使用 510 作为矩形 URY 将文本盖在我的“真实” PDF 上,以考虑列文本的边距。

这样就实现了我盖印的文本以 y = 500 出现并匹配原始位置的期望结果 - 必须有一种更简单的方法,但这对我来说就足够了!

英文:

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!

huangapple
  • 本文由 发表于 2023年5月24日 18:44:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76322664.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定