Java: Need to create PDF from byte-Array

后端 未结 4 1914
逝去的感伤
逝去的感伤 2021-02-02 13:20

From a DB2 table I\'ve got blob which I\'m converting to a byte array so I can work with it. I need to take the byte array and create a PDF out of it.

This

4条回答
  •  轮回少年
    2021-02-02 13:51

    Read from file or string to bytearray.

    byte[] filedata = null;
    String content = new String(bytearray);
    content = content.replace("\r", "").replace("\uf8ff", "").replace("'", "").replace("\"", "").replace("`", "");
    String[] arrOfStr = content.split("\n");
    PDDocument document = new PDDocument();
    PDPage page = new PDPage();
    document.addPage(page);
    try (PDPageContentStream cs = new PDPageContentStream(document, page)) {
        // setting font family and font size
        cs.beginText(); 
        cs.setFont(PDType1Font.HELVETICA, 14);
        cs.setNonStrokingColor(Color.BLACK);
        cs.newLineAtOffset(20, 750);
        for (String str: arrOfStr) {
            cs.newLineAtOffset(0, -15);
            cs.showText(str);
        }
        cs.newLine();
        cs.endText();
    }
    document.save(znaFile);
    document.close();
    

提交回复
热议问题