Java 是否将图像添加到iText中的acrofield?
我尝试使用acrofields填充PDF,我能够完美地添加字符串数据,但在向acrofields添加图像时遇到问题。 这是我添加字符串数据的代码Java 是否将图像添加到iText中的acrofield?,java,pdf,itext,acrofields,Java,Pdf,Itext,Acrofields,我尝试使用acrofields填充PDF,我能够完美地添加字符串数据,但在向acrofields添加图像时遇到问题。 这是我添加字符串数据的代码 File f = new File("F:/Test/Agreement.pdf"); InputStream sourceTemplatePDFUrlStream = new BufferedInputStream(new FileInputStream(f)); File destinationFile = new File
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();
您可以尝试将其与用于添加图像的代码一起添加
PdfContentByte content = stamper.getOverContent(reader.getNumberOfPages());
Image image = Image.getInstance(new URL("E:/signature/signature.png"));
image.setAbsolutePosition(450,650);
image.scaleAbsolute(200,200);
content.addImage(image);
reader.close();
return output.toByteArray();
以下解决方案奏效了:
public static void addImage(PdfStamper stamper,AcroFields form,String field,String fieldValue){
try{
System.out.println("Field "+field);
java.util.List<AcroFields.FieldPosition> photograph = form.getFieldPositions(field);
if(photograph!=null && photograph.size()>0){
Rectangle rect= photograph.get(0).position;
//if(StringUtils.isNotBlank(fieldValue)){
Image img = Image.getInstance(fieldValue);
img.scaleToFit(rect.getWidth(), rect.getHeight());
img.setBorder(2);
img.setAbsolutePosition(
photograph.get(0).position.getLeft() + (rect.getWidth() - img.getScaledWidth() )
, photograph.get(0).position.getTop() - (rect.getHeight()));
PdfContentByte cb = stamper.getOverContent((int)photograph.get(0).page);
cb.addImage(img);
//}
}
}catch(Exception e){
e.printStackTrace();
}
}
如果CustomerSign是AcroField则“官方”方法是使用按钮字段作为图像的占位符,并替换按钮的“图标”,如中所述:
有关完整的代码示例,请参见
免责声明:我是iText的原始开发人员,也是“iText在用”书籍的作者。@VigneshVino编辑了我的问题。.包含的代码您的堆栈跟踪将帮助我了解您的问题,以便回复!!很高兴收到您的回复:)我喜欢StackOverflow的概念。这就是我去年加入的原因。我试着每天回答几个问题(虽然我并不总是有时间)
addImage(stamper, form, "CustomerSign", "E:/signature/signature.png");
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());