Domanda

Vorrei stampare il mio Swing JComponent tramite iText in 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( );

Purtroppo nulla è mostrato nel file PDF. Sai come risolvere questo problema?

È stato utile?

Soluzione

L'ho capito aggiungendo addNotify e validando gli aiuti.

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

Altri suggerimenti

Avevo bisogno di chiamare

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

Non so molto di iText, ma ... hai chiuso il PdfWriter ad un certo punto, giusto?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top