PDFBox不会将我想要的消息写入页面

时间:2018-11-12 14:56:30

标签: java javafx pdfbox

我正在寻找至少一个小时的代码,但找不到该错误。我正在使用PDFBox创建PDF(PDFBox HelloWorld Example)。要了解PDFBox的工作原理,我只想创建一些带有“ hello world”和页码的页面,例如“ hello world 1”,“ hello world 2”等等。如您所见,我创建了一个for循环来创建六个页面。

private void drawPDF(PDDocument doc, File file) throws IOException {
    for (int pageIndex = 0; pageIndex < 6; pageIndex++) {
        PDPage page = new PDPage(PDRectangle.A4);
        doc.addPage(page);
        PDFont font = PDType1Font.HELVETICA_BOLD;
        String message = "hello world " + (pageIndex + 1);
        float stringHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() * FONT_SIZE;
        PDRectangle pageSize = page.getMediaBox();
        try (PDPageContentStream contents = new PDPageContentStream(doc, page)) {
            contents.beginText();
            contents.setFont(font, FONT_SIZE);
            contents.setTextMatrix(Matrix.getTranslateInstance(0, pageSize.getHeight() - stringHeight / 1000f));
            contents.showText(message);
            System.out.println(message + " - " + doc.getNumberOfPages());
            contents.endText();
        }
    }
    doc.save(file);
}

在我的控制台中,我得到以下输出(第一个数字是pageIndex,第二个数字是doc.getNumberOfPages()):

hello world 1 - 1
hello world 2 - 2
hello world 3 - 3
hello world 4 - 4
hello world 5 - 5
hello world 6 - 6

这是我查看PDF的加载函数。

private final ObservableList<Image> pdfFilePages = FXCollections.observableArrayList();


private void loadFile(File file) throws FileNotFoundException, IOException {
    if (file != null) {
        ByteBuffer buffer = null;
        try (RandomAccessFile raf = new RandomAccessFile(file, "r"); FileChannel channel = raf.getChannel()) {
            buffer = ByteBuffer.allocate((int) channel.size());
            channel.read(buffer);
            buffer.flip();

            PDFFile pdfFile = new PDFFile(buffer);
            List<Image> pages = new ArrayList<>();
            for (int i = 0; i < pdfFile.getNumPages(); i++) {
                PDFPage page = pdfFile.getPage(i, true);
                System.out.println("page: " + i + " - " + pdfFile.getNumPages());
                java.awt.geom.Rectangle2D bbox = page.getBBox();
                java.awt.geom.Rectangle2D rect = new Rectangle(0, 0, (int) bbox.getWidth(), (int) bbox.getHeight());
                BufferedImage buffImage = new BufferedImage((int) (bbox.getWidth() * 2d),
                        (int) (bbox.getHeight() * 2d), BufferedImage.TYPE_INT_RGB);
                java.awt.Image awtImage = page.getImage((int) (bbox.getWidth() * 2.0),
                        (int) (bbox.getHeight() * 2.0), rect, null, true, true);
                java.awt.Graphics2D bufImageGraphics = buffImage.createGraphics();
                bufImageGraphics.drawImage(awtImage, 0, 0, null);

                Image image = SwingFXUtils.toFXImage(buffImage, null);
                pages.add(image);
            }
            pdfFilePages.addAll(pages);
        }
    }
}

这是我在控制台中看到的:

page: 0 - 6
page: 1 - 6
page: 2 - 6
page: 3 - 6
page: 4 - 6
page: 5 - 6

当我加载pdf文件以在我的应用程序中显示内容时,第一页和第二页显示“ hello world 1-1”。以下页面具有“ hello world 2-2”到“ hello world 5-5”。我不明白为什么会得到两页“ hello world 1-1”。我希望有人可以向我解释我犯错的地方。

1 个答案:

答案 0 :(得分:0)

借助mkl,我在代码中找到了错误。 for循环必须从1开始并运行,直到索引等于pdfFile.getNumPages()。当索引为0时,方法getPage()返回第一页。由于在第二次迭代中索引为1,因此第一页将被传递两次。由于循环已完成,因此不会传递最后一页。这似乎是正确的方法。

private void loadFile(File file) throws FileNotFoundException, IOException {
    if (file != null) {
        ByteBuffer buffer = null;
        try (RandomAccessFile raf = new RandomAccessFile(file, "r"); FileChannel channel = raf.getChannel()) {
            buffer = ByteBuffer.allocate((int) channel.size());
            channel.read(buffer);
            buffer.flip();

            PDFFile pdfFile = new PDFFile(buffer);
            List<Image> pages = new ArrayList<>();
            for (int i = 1; i <= pdfFile.getNumPages(); i++) {
                PDFPage page = pdfFile.getPage(i, true);
                System.out.println("page: " + i + " - " + pdfFile.getNumPages());
                java.awt.geom.Rectangle2D bbox = page.getBBox();
                java.awt.geom.Rectangle2D rect = new Rectangle(0, 0, (int) bbox.getWidth(), (int) bbox.getHeight());
                BufferedImage buffImage = new BufferedImage((int) (bbox.getWidth() * 2d),
                        (int) (bbox.getHeight() * 2d), BufferedImage.TYPE_INT_RGB);
                java.awt.Image awtImage = page.getImage((int) (bbox.getWidth() * 2.0),
                        (int) (bbox.getHeight() * 2.0), rect, null, true, true);
                java.awt.Graphics2D bufImageGraphics = buffImage.createGraphics();
                bufImageGraphics.drawImage(awtImage, 0, 0, null);

                Image image = SwingFXUtils.toFXImage(buffImage, null);
                pages.add(image);
            }
            pdfFilePages.addAll(pages);
        }
    }
}