将JavaFX ScrollPane内容保存到PDF文件 [英] Save JavaFX ScrollPane content to PDF file

查看:157
本文介绍了将JavaFX ScrollPane内容保存到PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码将JavaFx应用程序中ScrollPane的内容保存到PDF文件中.

I'm using the below code to save the content of a ScrollPane in my JavaFx Application to a PDF file.

button.setOnMouseClicked(new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {

File pdfFile = fileChooser.showSaveDialog(primaryStage);

try {
    BufferedImage bufImage = SwingFXUtils.fromFXImage(scrollPane.snapshot(new SnapshotParameters(), null), null);
    FileOutputStream out = new FileOutputStream(new File("../temp.jpg"));
    javax.imageio.ImageIO.write(bufImage, "jpg", out);
    out.flush();
    out.close();

    com.itextpdf.text.Image image =com.itextpdf.text.Image.getInstance("../temp.jpg");
    Document doc = new Document(new com.itextpdf.text.Rectangle(image.getScaledWidth(), image.getScaledHeight()));
    FileOutputStream fos = new FileOutputStream(pdfFile);
    PdfWriter.getInstance(doc, fos);
    doc.open();
    doc.newPage();
    image.setAbsolutePosition(0, 0);
    doc.add(image);
    fos.flush();
    doc.close();
    fos.close();


}
catch(Exception e)
{
     e.printStackTrace();
}

} });

在滚动窗格中,我有一个非常长的VBox,其中包含将近40-50个标签.因此,此代码最初将其保存到jpg文件,然后将其添加到pdf文件.

In the scrollpane, I have a very long VBox which contains almost 40-50 labels. So, this code initially saves it to a jpg file and then adds it to a pdf file.

最初创建temp.jpg时,由于其长度,jpg文件看起来非常薄.应该放大以查看实际内容.

When the temp.jpg is created initially, due to its length, the jpg file looks very thin. It should be zoomed to see the actual content.

写pdf文件时,它是空白的,只是它很长,就像将jpg真正转换为PDF文件时一样.

When the pdf file is written, it was blank except that it was lengthy as it would have been when the jpg is really converted to a PDF file.

有人可以帮我解决这个问题吗?将ScrollPane的快照以其实际大小/比例转换为PDF?

Can anyone help me fix this ? to take the snapshot of the ScrollPane to PDF with its actual size/scale ?

推荐答案

我首先缩放了图像,然后使用缩放后的PageSize创建了文档.这解决了问题

I have first scaled the image and then created document with scaled PageSize. This fixed the issue

com.itextpdf.text.Image image =com.itextpdf.text.Image.getInstance("../temp.jpg");
image.scalePercent(1);
Document doc = new Document(new com.itextpdf.text.Rectangle(image.getScaledWidth(), image.getScaledHeight()));
doc.open();
doc.add(image);

这篇关于将JavaFX ScrollPane内容保存到PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆