从java代码里建立表格_如何使用Java在PDF文档中创建表格?

如何使用Java在PDF文档中创建表格?

注:iText开发环境设置,下载iText7 jar(社区版:http://github.com/itext/itext7/releases/tag/7.0.4 ) ,创建一个工程:java_itext,并将下载的itext7 jar包和slf4j( http://www.slf4j.org/download.html )工具包添加到构建路径中。项目结构如下图所示 -

5fc47651a73bfcb49b5fa88d19698c1c.png

以下是使用Java在PDF中创建表的程序。

package com.yiibai;

import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.InputStreamReader;

import java.io.UnsupportedEncodingException;

import com.itextpdf.kernel.font.PdfFont;

import com.itextpdf.kernel.font.PdfFontFactory;

import com.itextpdf.kernel.pdf.PdfDocument;

import com.itextpdf.kernel.pdf.PdfWriter;

import com.itextpdf.layout.Document;

import com.itextpdf.layout.element.Cell;

import com.itextpdf.layout.element.Table;

public class AddingTableToPDF {

public static void main(String args[]) throws Exception {

String file = "addingTableToPDF.pdf";

PdfDocument pdfDoc = new PdfDocument(new PdfWriter(file));

// Creating a Document object

Document doc = new Document(pdfDoc);

// Creating a table

Table table = new Table(2);

// Adding cells to the table

table.addCell(new Cell().add("名称"));

table.addCell(new Cell().add("Maxsu"));

table.addCell(new Cell().add("编号"));

table.addCell(new Cell().add("10010"));

table.addCell(new Cell().add("职位"));

table.addCell(new Cell().add("Java Developer"));

// 支持文中字体显示

PdfFont font = PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H", false);

table.setFont(font);

// Adding Table to document

doc.add(table);

// Closing the document

doc.close();

System.out.println("Table created successfully..");

}

}

执行上面示例代码,得到以下结果 -

Table created successfully..

输出文件内容如下所示 -

edbd149a07dd5815b71f4275fd2154a4.png

¥ 我要打赏

纠错/补充

收藏

下一篇:哥,这回真没有了

加QQ群啦,易百教程官方技术学习群

注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值