gpt4 book ai didi

java - 在 iText 中将图像添加到 acrofield?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:08:43 26 4
gpt4 key购买 nike

我正在尝试使用 acrofields 填充 PDF,我能够完美地添加字符串数据,但在将图像添加到 acrofields 时遇到问题。这是我添加字符串数据的代码..

    File f = new File("F:/Test/Agreement.pdf");
InputStream sourceTemplatePDFUrlStream = new BufferedInputStream(new FileInputStream(f));
File destinationFile = new File("F:/Test/ag1.pdf");

PdfReader reader = new PdfReader(sourceTemplatePDFUrlStream);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(
destinationFile));

AcroFields form = stamper.getAcroFields();
Image img = Image.getInstance("E:/signature/signature.png");
Set fields = form.getFields().keySet();

Hashtable val = new Hashtable();
val.put("name", "xxx" );
val.put("city_street_zip", "xxx"+" "+"xxx"+" "+"xxx");
val.put("chck_1", "Yes" );
val.put("chck_2", "No");
val.put("chck_3", "Yes" );
val.put("street_address", "xxx" );
val.put("account_num", "1234");



Enumeration enumeration = val.keys();

// iterate through Hashtable val keys Enumeration
while (enumeration.hasMoreElements()) {
String nextElement = (String) enumeration.nextElement();
String nextElementValue = (String) val.get(nextElement);
//System.out.println(nextElement + ":=================fillData===================:" + nextElementValue);
form.setField(nextElement, nextElementValue);
}

//Form flattening makes the form non-editable and saveable with the
//form val filled in
stamper.setFormFlattening(true);

stamper.close();

最佳答案

执行此操作的“官方”方法是使用按钮字段作为图像的占位符,并按照 my book 中所述替换按钮的“图标” :

PushbuttonField ad = form.getNewPushbuttonFromField(imageFieldName);
ad.setLayout(PushbuttonField.LAYOUT_ICON_ONLY);
ad.setProportionalIcon(true);
ad.setImage(Image.getInstance("E:/signature/signature.png"));
form.replacePushbuttonField("advertisement", ad.getField());

参见 ReplaceIcon.java完整的代码示例。

免责声明:我是 iText 的原始开发者和“iText in Action”书籍的作者。

关于java - 在 iText 中将图像添加到 acrofield?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16052047/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com