如何使用iText将Swing组件绘制为PDF文件? [英] How do I paint Swing Components to a PDF file with iText?

查看:228
本文介绍了如何使用iText将Swing组件绘制为PDF文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过iText将我的Swing JComponent打印到pdf。

I would like to print my Swing JComponent via iText to pdf.

JComponent com = new JPanel();
com.add( new JLabel("hello") );

PdfWriter writer = PdfWriter.getInstance( document, new FileOutputStream( dFile ) );
document.open( );

PdfContentByte cb = writer.getDirectContent( );
PdfTemplate tp = cb.createTemplate( pageImageableWidth, pageImageableHeight );
Graphics2D g2d = tp.createGraphics( pageImageableWidth, pageImageableHeight, new DefaultFontMapper( ) );
g2d.translate( pf.getImageableX( ), pf.getImageableY( ) );
g2d.scale( 0.4d, 0.4d );
com.paint( g2d );
cb.addTemplate( tp, 25, 200 );
g2d.dispose( );

不幸的是,PDF文件中没有显示任何内容。
你知道如何解决这个问题吗?

Unfortunately nothing is shown in the PDF file. Do you know how to solve this problem?

推荐答案

我已经想出了添加addNotify和验证帮助。

I have figured it out adding addNotify and validate helps.


    com.addNotify( );
    com.validate( );

这篇关于如何使用iText将Swing组件绘制为PDF文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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