当前位置: 代码迷 >> Web前端 >> itext改换模板中图片的方法
  详细解决方案

itext改换模板中图片的方法

热度:43   发布时间:2012-09-21 15:47:26.0
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中测试通过。