添加富文本到AcroField在iTextSharp的文本、AcroField、iTextSharp

2023-09-05 02:40:00 作者:眼淚〆占據了整個眼眶

我需要添加一些格式化的文本使用iTextSharp的PDF表单字段,我似乎无法弄清楚。这是我的我目前设定的领域,但插入的文本的某些部分将需要加粗或有其他格式。

I need to add some formatted text to a PDF form field using iTextSharp and I can't seem to figure it out. This is how I'm currently setting the fields, but certain portions of the inserted text will need to be bold or have other formatting.

stamper.AcroFields.SetField("fieldName", "fieldValue")
stamper.FormFlattening = True

我见过其他地方的那我应该能够将一些XML,包括格式化信息,而不是,但我不知道这是什么的xml应该是什么样子。

I've seen elsewhere that I should be able to insert some xml that includes formatting information instead, but I have no idea what this xml should look like.

下面是从该页面的部分,导致我相信,我要的是可能的:

Here's the section from that page that leads me to believe that what I want is possible:

它仍然邮票实际的XML code   到外地与读者不会   使其在冲压为富文本   PDF。

It still stamps the actual XML code onto the field and the Reader will not render it as rich text in the stamped PDF.

和工作应对这样的:

有关的工作,你必须打电话   AcroFields.setGenerateAppearances(假)。   需要注意的是较旧的阅读器版本不   生成亮相。

For that to work you'll have to call AcroFields.setGenerateAppearances(false). Note that older Reader versions don't generate appearances.

我应该该XML的样子,或者是有另一种方式做到这一点?

What should this xml look like, or is there another way to accomplish this?

推荐答案

前阵子,我加的能力iText的设置字段的富文本值。我没加任何渲染code来支持这一行动。

A while back, I added the ability to iText to set the rich text value of a field. I didn't add any rendering code to back this up.

这意味着看到丰富的价值的唯一方法是通过一个支持它的PDF阅读器(读卡器/ Acrobat中)。为了让他们知道他们需要重建所有现场露面,你必须调用 acroFields.setGenerateAppearances(假)。这将禁用的iText的外观产生,而是要求PDF阅读器为您代劳。

That means the only way to see that rich value is via a PDF viewer that supports it (Reader/Acrobat). In order for them to know they need to rebuild all field appearances, you have to call acroFields.setGenerateAppearances(false). This disables iText's appearance generation, and instead requests that the PDF viewer do it for you.

如果你想的扁平丰富的文本的全范围内的iText [夏普]你将不得不做一些事情扭曲:

If you want flattened rich text wholly within iText[Sharp] you're going to have to do something screwy:

在画你丰富的文本转换为PDF页面在一个单独的文件。当前页的尺寸应当匹配字段的大小。保存它(内存流是罚款)。 添加一个新的 PushbuttonField 设置为 LAYOUT_ICON_ONLY 。使用相同的边框与原始文本字段。设置按钮的背景颜色(透明)。 创建一个 PdfImportedPage 您的富文本页面,并将此导入的页面是新的 PushbuttonField 的图标(模板)。 平展。 Draw your rich text into a PDF page in a separate document. The size of this page should match the size of the field. Save it out (a memory stream is fine). Add a new PushbuttonField, set to LAYOUT_ICON_ONLY. Use the same bounding box as the original text field. Set the pushbutton's background color to null (transparent). Create a PdfImportedPage of your rich-text page and set this imported page to be the new PushbuttonField's icon (template). Flatten.

痛苦的,但有效的。

您可以做#1 HTMLWorker和使用原来的富文本/ HTML作为输入。

You can do #1 with HTMLWorker and use your original rich text/HTML as input.

其实,你很可能得到更有效的与HTMLWorker供给ColumnText对象呈现在同一个文档,而不是一个新的HTML,然后写出来,读回再次颇有几分。掀起PdfTemplate任何旧网页的DirectContent与适当的边界框。

In fact, you could probably get quite a bit more efficient with an HTMLWorker feeding a ColumnText object to render your HTML in the same document rather than a new one, then write it out and read it back in again. Whip up a PdfTemplate from any old page's DirectContent with the appropriate bounding box.

这会吸少得多。