【问题标题】:How to save scrollable jpanel into pdf or any format如何将可滚动的 jpanel 保存为 pdf 或任何格式
【发布时间】:2013-04-28 10:13:11
【问题描述】:

我在 jscrollpane 中有一个 jpanel,我想将 jpanel 的所有内容(包括一些 labeltextarea)导出为 pdf 格式。目前我在jbutton 上使用此代码:

Document document = new Document(PageSize.A4.rotate(),50,50,50,50);
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\\Users\\Admin\\Desktop\\test1.pdf"));
document.open();
PdfContentByte contentByte = writer.getDirectContent();
//jp is the jpanel
PdfTemplate template = contentByte.createTemplate(jp.getWidth(), jp.getHeight());
Graphics2D g2 = template.createGraphics(jp.getWidth(),jp.getHeight());
g2.scale(0.8, 1);
        jp.printAll(g2); // also tried with jp.paint(g2), same result
        g2.dispose();

        //document.close();
contentByte.addTemplate(template, 5, 60);
} catch (Exception e) {
e.printStackTrace();
}
finally{
if(document.isOpen()){
    document.close();
}

}

输出:我在 pdf 中得到了jpanel 的下部...(我制作了 jpanel 的 sanpshot,但我的声誉不允许发布图像),上部越来越丢弃。我认为最后一部分被导出为使用单页。第一部分被重写(这只是一个猜测!) 为了查看整个jpanel,我们需要滚动...如何将整个面板(从上到下)导出为 pdf 到多个页面,因为 jpanel 的高度超过 A4 页面。请帮忙。提前致谢。

注意:我正在使用netbeans 开发我的gui 和itextpdf-5.4.1。我是java 编程的初学者。

【问题讨论】:

    标签: java swing pdf jpanel itext


    【解决方案1】:

    您需要关闭 PrintWriter 并尝试添加 addNotify,validate

    jp.addNotify();
    jp.setSize();
    jp.validate();
    

    【讨论】:

      猜你喜欢
      • 2014-10-24
      • 1970-01-01
      • 2012-07-07
      • 1970-01-01
      • 2012-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多