Java中利用IText7生成PDF

下面介绍几种IText7生成PDF的一些页面元素的方法:

(官网http://developers.itextpdf.com/)

1、添加表格

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.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.HorizontalAlignment;
import com.itextpdf.layout.property.UnitValue;
public class Test {
	 public static final String DEST2 = "E:\\project\\test4.pdf";//文件路径
	 public static void test(String dest) throws Exception{
   	 PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
	 Document doc = new Document(pdfDoc);//构建文档对象
     PdfFont sysFont = PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H", false);//中文字体
   	 Table table = new Table(new float[]{2,4,4}).setWidth(UnitValue.createPercentValue(100));//构建表格以100%的宽度
   	 Cell cell1=new Cell().add(new Paragraph("表格1")).setFont(sysFont);//向表格添加内容
   	 Cell cell2=new Cell().add(new Paragraph("表格2")).setFont(sysFont);
   	 Cell cell3=new Cell().add(new Paragraph("表格3")).setFont(sysFont);
   	 table.addCell(cell1);
   	 table.addCell(cell2);
   	 table.addCell(cell3);  
        doc.add(table.setHorizontalAlignment(HorizontalAlignment.CENTER));//将表格添加入文档并页面居中
        doc.close();
   }
	public static void main(String[] args) throws E
  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值