亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Javafx-Image To PDF 轉換使用 PDFBox 顯示圖像的一半

Javafx-Image To PDF 轉換使用 PDFBox 顯示圖像的一半

Helenr 2021-11-17 14:41:46
我已成功將圖像轉換為 Pdf。我的問題是 pdf 顯示寬度的一半我的代碼:@FXMLprivate 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:
查看完整描述

1 回答

?
蕭十郎

TA貢獻1815條經驗 獲得超13個贊

首選大小屬性及其對應的最小/最大屬性都不允許您可靠地確定 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");

這可以HostServices通過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();

}


查看完整回答
反對 回復 2021-11-17
  • 1 回答
  • 0 關注
  • 478 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號