显示在acro文本字段位置的图象 [英] Showing image on a acro text field position

查看:172
本文介绍了显示在acro文本字段位置的图象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PDF文档,其中包含已经分享给客户端的acro文本字段。现在,客户端希望在其中一个文本字段中插入签名图像。我的经理让我尝试一种方法来做同样的事情。我的想法是替换文本字段位置顶部的图像,并将文本字段的大小调整为图像大小。

I had a PDF document which has acro text fields which is already shared to the client. Now the client wants to insert signature image in one of the text fields. My manager asked me to try a way to do the same.My idea is to replace an image on top of the text field position and resize the text field as image size.

为了将pdf的acro文本字段替换为图像,我正在尝试如下

For replacing acro text field of pdf into image, i am trying as below

1.Finding the text field by its field id 

String position = null; 
List<FieldPosition> fieldPositons = form.getFieldPositions("50106");
                    for (FieldPosition position :fieldPositons) {
                        this.position = position.position;
                    }
2. Setting the image into that position of text field
     Image image = Image.getInstance("ImageFileName");
Float dimensions = position.split("x");
image.setAbsolutePosition(dimensions[0], dimensions[1]);    
content.addImage(image);

根据给定的图像宽度和高度,我需要更改acro文本字段的宽度和高度。

Based on the given image width and height i need to change the width and height of acro text field.

任何人都可以尝试如下,我的逻辑是否适用于itext pdf库。让我知道是否有任何想法用图像替换acro文本字段

Can any one tried as below, Is my logic works with itext pdf library. Let me know if any idea to replace acro text field with image

推荐答案

改变一个维度是不是一个好主意。 AcroField,因为PDF中的所有内容都添加在绝对位置。当您更改字段的尺寸时,您可能会与其他内容重叠。

It is never a good idea to change the dimensions of an AcroField as all the content in a PDF is added at absolute positions. When you change the dimension of a field, you risk that it will overlap with other content.

因此,您最好的选择是调整图像的大小AcroField。如您所示,您可以获得如下字段位置:

Hence, your best option is to adapt the size of the image to the size of the AcroField. As you already indicated, you can get the field position like this:

AcroFields.FieldPosition f = form.GetFieldPositions("50106")[0];

请注意,将其设为字符串。您可以使用 FieldPosition 对象,如下所示:

Note that it's not a good idea to make this a string. You can use the FieldPosition object like this:

int page = f.page;
Rectangle rect = f.position;

您可以像这样缩放和定位图像:

You can scale and position the image like this:

Image image = Image.getInstance("ImageFileName");
image.ScaleToFit(rect.Width, rect.Height);
image.SetAbsolutePosition(rect.Left, rect.Bottom);

ScaleToFit()方法将扩展图像以适合表单字段尺寸的方式,尊重图像的原始纵横比(您不想添加拉伸图像)。

The ScaleToFit() method will scale the image in such a way that it will fit the dimensions of the form field, respecting the original aspect ratio of the image (you don't want to add a stretched image).

您需要变量才能将图像添加到正确的页面:

You need to page variable to add the image to the correct page:

PdfContentByte canvas = stamper.GetOverContent(page);
canvas.AddImage(image);

重要提示:


  • 如果您按上述方式添加图片,则需要删除该字段(如果展平表单,则会自动执行此操作)。

  • 如果表单字段是按钮,则不应使用上面的代码。相反,你应该更换按钮的图标。这在我对问题的回答中有所描述如何使用itextsharp更改PDF Formular的按钮图标?此答案解释了执行您要执行的操作的标准方法,但它要求您要添加图像的字段是按钮字段(根据您的问题,它似乎是一个文本字段)。

  • If you add the image as described above, you need to remove the field (this happens automatically if you flatten the form).
  • If the form field is a button, then you shouldn't use the above code. Instead you should replace the icon of the button. This is described in my answer to the question How to change a Button Icon of a PDF Formular with itextsharp? This answer explains the standard way to do what you're trying to do, but it requires that the field where you want to add the image is a button field (and based on your question, it seems that it's a text field).

这篇关于显示在acro文本字段位置的图象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆