当前位置: 首页 > 工具软件 > PDFBox > 使用案例 >

pdfbox使用

司徒嘉祥
2023-12-01

前期准备

在Java项目中导入comons-logging-1.2.jar、fontbox-2.0.13.jar以及pdfbox-2.0.13.jar。

功能点

创建空白pdf

 public static void createPDF(String targetPath){
        PDDocument doc = new PDDocument();
        PDPage page = new PDPage(PDRectangle.A4);
        doc.addPage(page);
        try {
            doc.save(targetPath);
            doc.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

支持中文

public static void createPDF(String targetPath){
        PDDocument doc = new PDDocument();
        PDPage page = new PDPage(PDRectangle.A4);
        doc.addPage(page);
        try {
            TrueTypeCollection ttc = new TrueTypeCollection(new File("C:/Windows/Fonts/simsun.ttc"));
            PDFont font = PDType0Font.load(doc, ttc.getFontByName("SimSun"), true);
            PDPageContentStream contentStream = new PDPageContentStream(doc, page);
            contentStream.setFont(font, 14);
            contentStream.beginText();
            contentStream.newLineAtOffset(20, 700);
            contentStream.showText("你好,Java");
            contentStream.endText();
            contentStream.close();
            doc.save(targetPath);
            doc.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

画表格

private static void drawTable(PDPageContentStream contentStream,float x,float y,float tableWidth,float tableHeight){
        try {
            contentStream.setStrokingColor(Color.GRAY);
            drawLine(contentStream,x,y,x,y+tableHeight);
            drawLine(contentStream,x,y+tableHeight,x+tableWidth,y+tableHeight);
            drawLine(contentStream,x+tableWidth,y+tableHeight,x+tableWidth,y);
            drawLine(contentStream,x+tableWidth,y,x,y);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

private static void drawLine(PDPageContentStream contentStream,float startX,float startY,float endX,float endY){
        try {
            contentStream.moveTo(startX,startY);
            contentStream.lineTo(endX,endY);
            contentStream.stroke();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

文字超长时,换行

将文字拆分成多个,并存放在List中。

private static List<String> getTextContent(float tableWidth, PDFont font, float fontSize, String content){
        List<String> contents = new ArrayList<String>();
        try {
            int stringWidth=(int)(fontSize*font.getStringWidth(content)/1000);
            if(stringWidth <= tableWidth-5){
                contents.add(content);
                return contents;
            }
            int length = content.length();
            StringBuilder sb = new StringBuilder();
            char c;
            for(int i=0;i<length;i++){
                c = content.charAt(i);
                sb.append(c);
                stringWidth = (int)(fontSize*font.getStringWidth(sb.toString())/1000);
                if(stringWidth > tableWidth-5){
                    sb.delete(sb.length()-1,sb.length());
                    contents.add(sb.toString());
                    sb = new StringBuilder();
                    sb.append(c);
                     if(i == length-1){
                    contents.add(sb.toString());
                    break;
                }
                }else if(i == length-1){
                    contents.add(sb.toString());
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return contents;
    }

未完待续。。。

 类似资料: