當前位置:編程學習大全網 - 編程語言 - 如何運用Java組件itext生成pdf

如何運用Java組件itext生成pdf

首先從iText的官網下載這個開源的小組件。

iText官方網站

Java版iText組件

Java版工具包

C#版iText組件

C#版工具包

這裏筆者使用的是Java版itext-5.2.1。

將itext-5.2.1.zip壓縮包解壓縮後得到7個文件:itextpdf-5.2.1.jar(核心組件)、itextpdf-5.2.1-javadoc.jar(API文檔)、itextpdf-5.2.1-sources.jar(源代碼)、itext-xtra-5.2.1.jar、itext-xtra-5.2.1-javadoc.jar、itext-xtra-5.2.1-sources.jar

使用5步即可生成壹個簡單的PDF文檔。

復制代碼

1 // 1.創建 Document 對象

2 Document _document = new Document();

3 // 2.創建書寫器,通過書寫器將文檔寫入磁盤

4 PdfWriter _pdfWriter = PdfWriter.getInstance(_document, new FileOutputStream("生成文件的路徑"));

5 // 3.打開文檔

6 _document.open();

7 // 4.向文檔中添加內容

8 _document.add(new Paragraph("Hi"));

9 // 5.關閉文檔

10 _document.close();

復制代碼

OK,搞定,不出問題的話就會在妳指定的路徑中生成壹個PDF文檔,內容是純文本的“Hi”。

可是這樣並不能完全滿足我們的需求,因為通常我們要生成的PDF文件不壹定是純文本格式的,比如我現在要實現打印銷售單的功能,那麽最起碼需要繪制表格才行,怎麽辦呢?且跟筆者繼續向下研究。

在iText中,有專門的表格類,即PdfPTable類。筆者做了壹個簡單的表格示例,請先看代碼:

復制代碼

1 OutTradeList _otl = this.getOtlBiz().findOutTradeListById(this.getOtlid());

2 String _fileName = _otl.getOtlId() + ".pdf";

3

4 // iText 處理中文

5 BaseFont _baseFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", true);

6 // 1.創建 Document 對象

7 Document _document = new Document(PageSize.A4);

8

9 HttpServletResponse response = ServletActionContext.getResponse();

10 response.setContentType("application/pdf; charset=ISO-8859-1");

11 response.setHeader("Content-Disposition", "inline; filename=" + new String(_fileName.getBytes(), "iso8859-1"));

12

13 // 2.創建書寫器,通過書寫器將文檔寫入磁盤

14 PdfWriter _pdfWriter = null;

15 try {

16 _pdfWriter = PdfWriter.getInstance(_document, response.getOutputStream());

17 } catch (Exception e) {

18 this.setMessage("單據生成失敗,請檢查服務器目錄權限配置是否正確");

19 e.printStackTrace();

20 System.out.println("2.掛了");

21 // return INPUT;

22 return null;

23 }

24 if(_pdfWriter == null) {

25 this.setMessage("單據生成失敗,請檢查服務器目錄權限配置是否正確");

26 System.out.println("3.掛了");

27 // return INPUT;

28 return null;

29 }

30

31 // 3.打開文檔

32 _document.open();

33

34 // 4.創建需要填入文檔的元素

35 PdfPTable _table = new PdfPTable(4);

36 PdfPCell _cell = null;

37

38 _table.addCell(new Paragraph("單據號", new Font(_baseFont)));

39 _cell = new PdfPCell(new Paragraph(_otl.getOtlId()));

40 _cell.setColspan(3);

41 _table.addCell(_cell);

42

43 _table.addCell(new Paragraph("客戶名稱", new Font(_baseFont)));

44 _cell = new PdfPCell(new Paragraph(_otl.getClients().getName(), new Font(_baseFont)));

45 _cell.setColspan(3);

46 _table.addCell(_cell);

47

48 _table.addCell(new Paragraph("銷售日期", new Font(_baseFont)));

49 _cell = new PdfPCell(new Paragraph(_otl.getOutDate().toString()));

50 _cell.setColspan(3);

51 _table.addCell(_cell);

52

53 _cell = new PdfPCell();

54 _cell.setColspan(4);

55 PdfPTable _tabGoods = new PdfPTable(7);

56 // 添加標題行

57 _tabGoods.setHeaderRows(1);

58 _tabGoods.addCell(new Paragraph("序號", new Font(_baseFont)));

59 _tabGoods.addCell(new Paragraph("商品名稱", new Font(_baseFont)));

60 _tabGoods.addCell(new Paragraph("自定義碼", new Font(_baseFont)));

61 _tabGoods.addCell(new Paragraph("規格", new Font(_baseFont)));

62 _tabGoods.addCell(new Paragraph("數量", new Font(_baseFont)));

63 _tabGoods.addCell(new Paragraph("單價", new Font(_baseFont)));

64 _tabGoods.addCell(new Paragraph("小計", new Font(_baseFont)));

65 Object[] _outTrades = _otl.getOutTrades().toArray();

66 // 將商品銷售詳細信息加入表格

67 for(int i = 0; i < _outTrades.length;) {

68 if((_outTrades[i] != null) && (_outTrades[i] instanceof OutTrade)) {

69 OutTrade _ot = (OutTrade) _outTrades[i];

70 Goods _goods = _ot.getGoods();

71 _tabGoods.addCell(String.valueOf((++i)));

72 _tabGoods.addCell(new Paragraph(_goods.getName(), new Font(_baseFont)));

73 _tabGoods.addCell(_goods.getUserCode());

74 _tabGoods.addCell(_goods.getEtalon());

75 _tabGoods.addCell(String.valueOf(_ot.getNum()));

76 _tabGoods.addCell(String.valueOf(_ot.getPrice()));

77 _tabGoods.addCell(String.valueOf((_ot.getNum() * _ot.getPrice())));

78 }

79 }

80 _cell.addElement(_tabGoods);

81 _table.addCell(_cell);

82

83 _table.addCell(new Paragraph("總計", new Font(_baseFont)));

84 _cell = new PdfPCell(new Paragraph(_otl.getAllPrice().toString()));

85 _cell.setColspan(3);

86 _table.addCell(_cell);

87

88 _table.addCell(new Paragraph("操作員", new Font(_baseFont)));

89 _cell = new PdfPCell(new Paragraph(_otl.getProcure()));

90 _cell.setColspan(3);

91 _table.addCell(_cell);

92

93 // 5.向文檔中添加內容,將表格加入文檔中

94 _document.add(_table);

95

96 // 6.關閉文檔

97 _document.close();

98 System.out.println(_fileName);

99 this.setPdfFilePath(_fileName);

100 System.out.println("3.搞定");

101 // return SUCCESS;

102 return null;

復制代碼

以上代碼是寫在 Struts2 的 Action 中的,當用戶發送了請求之後直接將生成的PDF文件用輸出流寫入到客戶端,瀏覽器收到服務器的響應之後就會詢問用戶打開方式。

當然,我們也可以將文件寫入磁盤等等。

  • 上一篇:東方水泵編程
  • 下一篇:新加坡留學計算機專業好不好
  • copyright 2024編程學習大全網