2013-04-17 60 views
6

我想使用acrofields來填充PDF,我能夠完美地添加字符串數據,但是在向acrofields添加圖像時遇到問題。 這是我添加字符串數據代碼..將圖像添加到iText的acrofield中?

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(); 
+0

告訴我們你的代碼 –

+0

@VigneshVino編輯我question..included代碼 –

+0

您的堆棧跟蹤會幫助我瞭解u'r問題 –

回答

11

「官方」的方式來做到這一點,是有一個按鈕字段作爲佔位符圖像,並更換按鍵的「圖標」作爲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」書籍的作者。

+1

感謝您的答覆!很高興得到你的回覆:) –

+8

我喜歡StackOverflow的概念。這就是我去年加入的原因。我嘗試每天回答幾個問題(儘管我並不總是有時間)。 –

+0

form.getNewPushbuttonFromField(imageFieldName)爲我返回null – Simon

0

你可以嘗試用你的代碼添加此添加圖片

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

以下解決方案工作:

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

調用以上方法:

addImage(stamper, form, "CustomerSign", "E:/signature/signature.png"); 

其中CustomerSign是AcroField

+0

幫助,感謝@Anil –