iText生成PDF

本文详细介绍如何使用Java生成PDF文档,包括设置文档属性、字体、段落及表格等元素,特别针对中文显示问题提供了具体解决方案。
Document document = null;
PdfWriter writer = null;
try{
    // A4横向
    document = new Document(new Rectangle(842.0F, 595.0F), 20F, 20F, 50F, 50F);
    writer = PdfWriter.getInstance(document, new FileOutputStream(...));
    document.open();
    
    // 解决不支持中文的问题
    BaseFont baseFont = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
    
    // 字体
    // 标题:粗体、黑色、大小21
    Font titleFont = new Font(baseFont, 21F, Font.BOLD, BaseColor.BLACK);
    // 正文字体
    Font contentFont = new Font(baseFont, 16F);
    // 字体:下划线
    Font titleFont = new Font(baseFont, 16F, Font.UNDERLINE);
    
    // 短句,document.add(..)不换行
    Phrase phrase;
    // 段落:document.add(..)换行
    Paragraph paragraph;

    // 标题
    paragraph = new Paragraph("YangNing", titleFont);
    paragraph.setLeading(30F);
    paragraph.setsetAlignment(Element.ALIGN_CENTER);
    document.add(paragraph);

    // 第一段
    phrase = new Phrase("文字A", contentFont);
    phrase .setLeading(30F);
    document.add(phrase);

    phrase = new Phrase("文字B\r\n", contentFont);
    phrase .setLeading(30F);
    document.add(phrase);

    // 第二段
    paragraph = new Paragraph("xxxx", contentFont);
    paragraph.setLeading(30F);
    document.add(paragraph);

    // 增加表格
    document.add(createTable(baseFont));
} catch (Exception e){
    // handle exception
} finally {
    if(document != null){
        document.close;
    }
    if(writer != null){
        writer.close;
    }
}
private PdfPTable createTable(BaseFont baseFont){
    Font tableFont = new Font(baseFont, 9F);
    // 两列
    PdfPTable table = new PdfPTable(2)
    table.setWidthPercentage(100F);
    table.setSpacingBefore(10F);
    table.setSpacingAfter(10F);
    table.serWidths(new float[]{4F, 5F});

    PdfPCell child;

    child = new PdfPCell(new Phrase("姓名", tableFont));
    child.setMinimunHeight(30F)
    child.setHorizontalAlignment(Element.ALIGN_CERTER);
    child.setVerticalAlignment(Element.ALIGN_MIDDLE);
    table.addCell(child);

    child = new PdfPCell(new Phrase("年龄", tableFont));
    child.setMinimunHeight(30F)
    child.setHorizontalAlignment(Element.ALIGN_CERTER);
    child.setVerticalAlignment(Element.ALIGN_MIDDLE);
    table.addCell(child);

    child = new PdfPCell(new Phrase("yangxuyue", tableFont));
    child.setMinimunHeight(30F)
    child.setHorizontalAlignment(Element.ALIGN_CERTER);
    child.setVerticalAlignment(Element.ALIGN_MIDDLE);
    table.addCell(child);

    child = new PdfPCell(new Phrase("24", tableFont));
    child.setMinimunHeight(30F)
    child.setHorizontalAlignment(Element.ALIGN_CERTER);
    child.setVerticalAlignment(Element.ALIGN_MIDDLE);
    table.addCell(child);

    return table;
}

 

转载于:https://www.cnblogs.com/yang21/p/9949361.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值