繁体   English   中英

如何使用iText将Swing组件绘制为PDF文件?

[英]How do I paint Swing Components to a PDF file with iText?

我想通过iText将我的Swing JComponent打印到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文件中没有显示任何内容。 你知道如何解决这个问题吗?

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

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

我需要打电话

com.addNotify()
com.setSize()
com.validate()

我对iText知之甚少,但是......你确实在某个时候关闭了PdfWriter ,对吧?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM