首页 > 解决方案 > Javafx-图像到PDF转换使用PDFBox显示一半图像

问题描述

我已成功将图像转换为 Pdf。我的问题是 pdf 显示宽度的一半我的代码:

@FXML
private void print() {
    try {
        WritableImage nodeshot = stackPane.snapshot(new SnapshotParameters(), null);
        File file = new File("C:/Users/Andre Kelvin/Desktop/TheNode.png");
        ImageIO.write(SwingFXUtils.fromFXImage(nodeshot, null), "png", file);

        PDDocument doc = new PDDocument();
        PDPage page = new PDPage();
        PDImageXObject pdimage;
        PDPageContentStream content;

        pdimage = PDImageXObject.createFromFile("C:/Users/Andre Kelvin/Desktop/TheNode.png", doc);
        content = new PDPageContentStream(doc, page);
        content.drawImage(pdimage, 0, 0);
        content.close();
        doc.addPage(page);
        doc.save("C:/Users/Andre Kelvin/Desktop/PDFNode.pdf");
        doc.close();
        file.delete();

        //This Line Automatically Opens the user defualt pdf file viewer
        Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + "C:/Users/Andre Kelvin/Desktop/PDFNode.pdf");
    } catch (Exception e) {
    }
}

我尝试使用以下行获取根节点的宽度和高度:

content.drawImage(pdimage, 0, 0,(float)stackPane.getPrefWidth(),(float)stackPane.getPrefHeight());

还有这个:

content.drawImage(pdimage, 0, 0,(float)stackPane.getMaxWidth(),(float)stackPane.getMaxHeight());

它只会显示一个空白的白页。

这是转换为 pdf 的实际图像: 在此处输入图像描述

这是图像的pdf: 在此处输入图像描述

标签: javajavafxpdfbox

解决方案


首选大小属性和它们的最小/最大对应项都不允许您可靠地确定 a 的大小Region。这些只是指标,计算值可能不匹配。此外,Region可以将尺寸调整为首选尺寸以外的尺寸。最后但并非最不重要的一点是,这些属性可能包含特殊值Region.USE_PREF_SIZE(= Double.NEGATIVE_INFINITY) 和Region.USE_COMPUTED_SIZE(= -1),甚至默认情况下也是如此。

如果您需要获取节点的大小,请使用以下boundsInLocal属性:

Bounds bounds = stackPane.getBoundsInLocal();

在这种情况下,获取快照的大小会更简单。

此外,页面大小PDPage可能不足以包含整个图像。您需要改为缩放图像或更改PDPage.


Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + "C:/Users/Andre Kelvin/Desktop/PDFNode.pdf");

这可以通过instance使用HostServicesavailable 来Application独立于平台完成。

例子

@Override
public void start(Stage primaryStage) {
    Button button = new Button("print");
    StackPane root = new StackPane(button);
    button.setOnAction(evt -> {
        try {
            WritableImage nodeshot = root.snapshot(new SnapshotParameters(), null);
            
            // store image in-memory
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            ImageIO.write(SwingFXUtils.fromFXImage(nodeshot, null), "png", output);
            output.close();

            PDDocument doc = new PDDocument();
            PDPage page = new PDPage();
            PDImageXObject pdimage;
            PDPageContentStream content;

            pdimage = PDImageXObject.createFromByteArray(doc, output.toByteArray(), "png");
            content = new PDPageContentStream(doc, page);

            // fit image to media box of page
            PDRectangle box = page.getMediaBox();
            double factor = Math.min(box.getWidth() / nodeshot.getWidth(), box.getHeight() / nodeshot.getHeight());

            float height = (float) (nodeshot.getHeight() * factor);
            
            // beware of inverted y axis here
            content.drawImage(pdimage, 0, box.getHeight() - height, (float) (nodeshot.getWidth() * factor), height);

            content.close();
            doc.addPage(page);

            File outputFile = new File("C:/Users/Andre Kelvin/Desktop/PDFNode.pdf");

            doc.save(outputFile);
            doc.close();

            getHostServices().showDocument(outputFile.toURI().toString());
        } catch (Exception e) {
        }
    });

    Scene scene = new Scene(root, 300, 300);

    primaryStage.setScene(scene);
    primaryStage.show();
}

推荐阅读