Java 生成 pdf

Java 可以通过 IText 直接把内容生成 pdf 文件,下面的 Demo 主要演示了文件属性、页眉页脚、表格、段落文字、图片的生成。
先上代码:

package html2PDF;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;

import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

public class CreatePdf {
	
	public static void main(String[] args) throws Exception {
		Document document = new Document();
		PdfWriter.getInstance(document, new FileOutputStream(new File("E:\\pdfTest\\mypdfdoc.pdf")));
		BaseFont baseFont = BaseFont.createFont("E:\\testFile\\msyh.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
		// 文字如果要加粗,修改第三个参数
		Font msya = new Font(baseFont, 12, Font.NORMAL);
		
		//设置文档属性
		createAttr(document);
		//设置页眉页脚
		createHeaderFooter(document, msya);
		
		document.open();
		
		document.add(new Paragraph("Table 样例", msya));
		//创建表格及设置表格样式
		Table table = createTable(4, Element.ALIGN_CENTER, 2);
		
		//创建单元格并设置样式,看自身情况可使用循环
		table.addCell(createCell("姓名", msya));
//		table.addCell(createCell("性别", msya, Element.ALIGN_LEFT), Element.ALIGN_TOP);
		table.addCell(createCell("性别", msya));
		table.addCell(createCell("籍贯", msya));
		table.addCell(createCell("生日", msya));
		
		table.addCell(createCellWithSpan("
  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值