1.pdf打印,另起新的一页

Document document = new Document(PageSize.A4, 50, 50, 22, 50);
document.open();   
// 另起一页 
document.newPage();

2.当PdfPTable的内容过长,页面剩余空白不足以填充时,PdfPCell的整格会自动换到下一页显示,导致上一页尾部一片空白,不美观

PdfPTable tableHead = new PdfPTable(2);
// 内容过长不换页
tableHead.setSplitLate(false);

3.当打印一段文字,但是这段文字的字体格式要求不一样

BaseFont baseFontNewman = BaseFont.createFont("/font/times.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
// 小标题:字体加粗
Font smallTitleFont = new Font(baseFontNewman, 11, Font.BOLD);
// 文本:字体不加粗
Font messageFont = new Font(baseFontNewman, 11, Font.NORMAL);
Paragraph paragraph = new Paragraph();
Chunk chunk11 = new Chunk("1) Insuring Clause:", smallTitleFont);
Chunk chunk12 = new Chunk("balabala", messageFont);
paragraph.add(chunk11);
paragraph.add(chunk12);
// 设置文字居中 0靠左  1,居中  2,靠右
paragraph.setAlignment(0);

4.表格里嵌套很多段落

public void getAccessory(Document document,float[] wid1) throws IOException, DocumentException {
        BaseFont baseFontNewman = BaseFont.createFont("/font/times.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        // 大标题:字体加粗且自大
        Font titleFont = new Font(baseFontNewman, 16, Font.BOLD);
        // 小标题:字体加粗单大小正常
        Font smallTitleFont = new Font(baseFontNewman, 11, Font.BOLD);
        // 文本:不加粗且大小正常
        Font messageFont = new Font(baseFontNewman, 11, Font.NORMAL);
        BaseFont bfChinese = BaseFont.createFont("/font/fangsong_GB2312.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        Font titleTextFont_cn = new Font(bfChinese, 11, Font.BOLD);

        // 右侧总单元格
        Paragraph totalParagraph = new Paragraph();

        //******************右侧单元格内容begin*************************************
        String title = "TITLE";
        Paragraph ptitle = new Paragraph(title, titleFont);
        ptitle.setAlignment(0);
        totalParagraph.add(ptitle);

        Paragraph paragraph1 = new Paragraph();
        String message1 = "Life is like a box of cholocate,you never know what you will go to get to me ,I'll never forget this ! I wish I could have been there with you.   Your were Jenny,I am not a smart man,but I know what is love.---Forrest Gump.Death is a part of life.\n";
        Chunk chunk11 = new Chunk("(1)first:", smallTitleFont);
        Chunk chunk12 = new Chunk(message1, messageFont);
        paragraph1.add(chunk11);
        paragraph1.add(chunk12);
        paragraph1.setAlignment(0);
        totalParagraph.add(paragraph1);

        String message2 = "     Tara! Home! I'll go home, and I'll think of some way to get him back. After all, tomorrow is another day.";
        Paragraph paragraph2 = new Paragraph();
        Chunk chunk21 = new Chunk("(2)second:", smallTitleFont);
        Chunk chunk22 = new Chunk(message2, messageFont);
        paragraph2.add(chunk21);
        paragraph2.add(chunk22);
        paragraph2.setAlignment(0);
        totalParagraph.add(paragraph2);

        String message21 = "    Jack: Listen, Rose. You're going to get out of here. You're going to go on. You're going to make lots of " +
                "babies, and you're going to watch them grow. You' re going to die and old.";
        Paragraph paragraph21 = new Paragraph(message21, messageFont);
        paragraph21.setAlignment(0);
        totalParagraph.add(paragraph21);
        String message22 = "    Winning that ticket (for Titanic at a poker game) Rose, was the best thing that ever happened to me.";
        Paragraph paragraph22 = new Paragraph(message22, messageFont);
        paragraph22.setAlignment(0);
        totalParagraph.add(paragraph22);
        //******************右侧单元格内容end*************************************

        // 2列表格
        PdfPTable tableHead = new PdfPTable(2);
        // 避免:内容过长,页面剩余空白不足以填充时,PdfPCell的整格会自动换到下一页显示
        tableHead.setSplitLate(false);
        // 设置表格大小为可用空白区域的100%
        tableHead.setWidthPercentage(100);
        // 传参:{30f, 70f} 两列的表格是30和70
        tableHead.setWidths(wid1);
        // 设置表格的边框
        tableHead.getDefaultCell().setBorderWidth(1);
        // 添加单元格(左边单元格)
        tableHead.addCell(new Paragraph("测试测试", titleTextFont_cn));
        // 添加单元格(右边单元格)
        tableHead.addCell(totalParagraph);
        document.add(tableHead);
    }

 

更多推荐

java pdf打印的几个小问题