Bootstrap

JAVA开发 使用Apache PDFBox库生成PDF文件,绘制表格

1. 表格位置定点

在这里插入图片描述

2.执行效果展示(截取PDF文件图片)

在这里插入图片描述

3.执行代码

当我们使用Apache PDFBox库在PDF文件中创建带有表格的内容,需要遵循几个步骤。PDFBox本身并没有直接的API来创建表格,但我们可以通过定位文本、绘制线条和单元格矩形来模拟表格的创建。

  1. 添加PDFBox依赖
  2. 编写代码以创建PDF并绘制表格
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

import java.io.File;
import java.io.IOException;

import java.text.SimpleDateFormat;
import java.util.Date;

public class TableInPDFBox {

    public static void main(String[] args) {
        PDDocument document = null;
        String[][] stuData = new String[][]{{"姓名","工号","部门","爱好","优势"}
                                            ,{"张三丰","WD0001","武当派","阅读让我们获得知识和灵感,运动可以让我们保持身体健康。","武当派和三丰派的祖师,会少林武功和九阳真经,武功特点是阴阳互融、刚柔并济,达到了武学的巅峰。"}
                                            ,{"郭  襄","EM0001","峨嵋派","书法有助于修养身心,陶冶情操,既练习了字,也可以修炼沉稳的性格","会峨眉九阳功,擅长黄药师最精妙的剑法之一的落英剑法"}};
        try {
            document = new PDDocument();
            PDPage page = new PDPage();
            document.addPage(page);

            PDPageContentStream contentStream = new PDPageContentStream(document, page);

            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HHmmss");
            String dateFormat = sdf.format(date);

            // 表格参数
            int rows = 3;
            int cols = 5;
            float xPos = 50; // 表格左上角的x坐标
            float yPos = 700; // 表格左上角的y坐标(注意:PDF坐标系统的原点在左下角)
            float cellWidth1 = 50 ,cellWidth2 = 150;
            float cellHeight = 50;

            // 绘制表格线 x轴
            for (int i = 0; i <= rows; i++) {
                float rowY = yPos - i * cellHeight;
                contentStream.moveTo(xPos, rowY);
                contentStream.lineTo( xPos+cellWidth1*3+cellWidth2*2, rowY);
                contentStream.stroke();
                //System.out.println("第"+(i+1)+"条x轴的位置是:("+xPos+","+rowY+")->("+(xPos+cellWidth1*3+cellWidth2*2)+","+rowY+")");
            }
			// 绘制表格线 y轴
            for (int j = 0; j <= cols; j++) {
                float colX ;
                if(j>3){
                    colX = xPos + 3 * cellWidth1  +(j-3)*cellWidth2;
                }else{
                    colX = xPos + (j) * cellWidth1;
                }
                contentStream.moveTo(colX, yPos);
                contentStream.lineTo(colX, yPos - rows * cellHeight);
                contentStream.stroke();
                //System.out.println("第"+(j+1)+"条y轴的位置是:("+colX+","+yPos+")->("+colX+","+(yPos - rows * cellHeight)+")");
            }

            // 写入表格内容
            File fontFile = new File("C:\\Windows\\Fonts\\simhei.ttf"); // 字体文件路径
            PDType0Font font = PDType0Font.load(document, fontFile);
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < cols; j++) {
                    String text = stuData[i][j];
                    float startX =0f;
                    if(j>3){
                        startX = xPos + 3 * cellWidth1  +(j-3)*cellWidth2;
                    }else{
                        startX = xPos + (j) * cellWidth1;
                    }
                    contentStream.beginText();
                    contentStream.setFont(font, 10);
                    contentStream.newLineAtOffset(startX +3, yPos - i * cellHeight-15);  //定点写入
                    contentStream.showText(text);
                    contentStream.endText();
                }
            }

            contentStream.close(); // 不要忘记关闭内容流

            // 保存PDF
            document.save("D:\\OA_Temp\\attachments\\pdf_attachs\\" + dateFormat + " table.pdf");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (document != null) {
                try {
                    document.close(); // 关闭文档
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
;