Javafx-图像到 PDF 转换使用 PDFBox 显示一半图像

标签 java javafx 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 的实际图像: enter image description here

这是图像的 pdf 格式: enter image description here

最佳答案

首选大小属性及其最小/最大对应属性都不允许您可靠地确定区域的大小。这些只是指标,计算值可能不匹配。此外,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");

这可以使用 HostServices available via the Application instance 独立于平台来完成.

示例

@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();
}

关于Javafx-图像到 PDF 转换使用 PDFBox 显示一半图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52801080/

相关文章:

css - 如何在javafx中设置边框间距

java - 用PDFBOX写阿拉伯字符

java - PDFBox API : How to change font to handle Cyrillic values in an AcroForm field

java - 很少有人扫描条形码,但很少有人不使用 Zxing

java - Android ListView 中的最大项目长度?

java - 在JavaFx中,如何通过ScrollPane包裹BorderPane

JavaFX 错误 : ANOMALY: meaningless REX prefix used

java - Spring MVC 自定义类属性编辑器

Java Webdevelopment Javascript验证工作流程?

java - 计算器对所有提出的问题返回 0.0