好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

java通过PDF模板填写PDF表单

本文实例为大家分享了java通过pdf模板填写pdf表单的具体代码,包括图片,供大家参考,具体内容如下

需要用到的java包:

 itext.jar、itextasian.jar的jar包。这个包里面定义了与中文输出相关的一些文件。

编写的表单如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

import java.io.bytearrayoutputstream;

import java.io.fileoutputstream;

import java.io.ioexception;

import java.util.hashmap;

import java.util.map;

 

import com.itextpdf.text.documentexception;

import com.itextpdf.text.image;

import com.itextpdf.text.rectangle;

import com.itextpdf.text.pdf.acrofields;

import com.itextpdf.text.pdf.basefont;

import com.itextpdf.text.pdf.pdfcontentbyte;

import com.itextpdf.text.pdf.pdfreader;

import com.itextpdf.text.pdf.pdfstamper;

 

/**

  * pdf工具类

  * @author moshunwei

  * @since 2018-02-01

  */

public class pdfutil {

 

  /**

  * 根据模板生成pdf

  * @param data map(string,object)

  * @return

  */

  public static boolean createpdf(string path,map<string, object> data) {

  pdfreader reader = null ;

  acrofields s = null ;

  pdfstamper ps = null ;

  bytearrayoutputstream bos = null ;

  try {

  reader = new pdfreader( "d:\\test.pdf" );

  bos = new bytearrayoutputstream();

  ps = new pdfstamper(reader, bos);

  s = ps.getacrofields();

 

  /**

  * 使用中文字体 使用 acrofields填充值的不需要在程序中设置字体,在模板文件中设置字体为中文字体 adobe 宋体 std l

  */

  basefont bfchinese = basefont.createfont( "stsongstd-light" , "unigb-ucs2-h" , false );

  /**

  * 设置编码格式

  */

  s.addsubstitutionfont(bfchinese);

 

 

  // 遍历data 给pdf表单表格赋值

  for (string key : data.keyset()) {

  s.setfield(key,data.get(key).tostring());

  }

 

  // 如果为false那么生成的pdf文件还能编辑,一定要设为true

  ps.setformflattening( true );

  /**

  * 添加图片

  */

  string imgpath= "d:/n5.jpg" ;

  int pageno = s.getfieldpositions( "img" ).get( 0 ).page;

  rectangle signrect = s.getfieldpositions( "img" ).get( 0 ).position;

  float x = signrect.getleft();

  float y = signrect.getbottom();

  // 读图片

  image image = image.getinstance(imgpath);

  // 获取操作的页面

  pdfcontentbyte under = ps.getovercontent(pageno);

  // 根据域的大小缩放图片

  image.scaletofit(signrect.getwidth(), signrect.getheight());

  // 添加图片

  image.setabsoluteposition(x, y);

  under.addimage(image);

  @suppresswarnings ( "resource" )

  fileoutputstream fos = new fileoutputstream( "d:\\shouju_fb.pdf" );

  fos.write(bos.tobytearray());

  return true ;

  } catch (ioexception | documentexception e) {

  system.out.println( "读取文件异常" );

  e.printstacktrace();

  return false ;

  } finally {

  try {

  bos.close();

  ps.close();

  reader.close();

  } catch (ioexception | documentexception e) {

  system.out.println( "关闭流异常" );

  e.printstacktrace();

  }

  }

  }

 

  public static void main(string[] args) {

  map<string, object> data = new hashmap<string, object>();

  data.put( "id" , "12312321" );

  data.put( "name" , "小帅哥" );

  data.put( "sex" , "男" );

  data.put( "age" , "21" );

  pdfutil.createpdf( "d:/n5.jpg" ,data);

  }

}

还有相应的编辑pdf表单的工具,默认用adobe acrobat。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://blog.csdn.net/qq_40150691/article/details/79249029?utm_source=blogxgwz3

查看更多关于java通过PDF模板填写PDF表单的详细内容...

  阅读:52次