`
javajack
  • 浏览: 21840 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

itext更换模板中图片的方法

 
阅读更多

生成pdf文件时,一般情况都是些表格或文字的,但也有pdf的需要包含图片的情况,

特别是固定格式的模板,其中包含图片时需要如何处理呢?

方法如下:

 

   首先我们模板文件中要有image域,才行,这个就不说了。

 

  以下是代码:

   

//这里只考虑图片,没有考虑其它对象如radio,list等
    Image image = Image.getInstance("c:\aa.jpg"); //image对象,不用多说
      
     if (image != null)
    {
    //从pdf的form域中取得对象,
   //tmpname为pdf中image的名称
     PushbuttonField pushbuttonField = form.getNewPushbuttonFromField(tmpname);     
     pushbuttonField.setImage(image);//将对象放入pushbuttonField
     
     PdfFormField editFormField = pushbuttonField.getField();  //生成fromfield
 
     form.replacePushbuttonField(tmpname, editFormField);//放入pdf
    }

  这里只贴出了部分代码,代码中没有考虑radio,list等情况。

 

   这样你生成的pdf中将包含图像。

   代码在itext2.7中测试通过。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics