private void prepare ( ) throws IOException , DocumentException , PrintingException { if ( baos == null ) { baos = new ByteArrayOutputStream ( ) ; // let it grow as much as needed } baos . reset ( ) ; boolean resize = false ; if ( page . getConstraint ( ) . getWidth ( ) == 0 || page . getConstraint ( ) . getHeight ( ) == 0 ) { resize = true ; } // Create a document in the requested ISO scale. Document document = new Document ( page . getBounds ( ) , 0 , 0 , 0 , 0 ) ; PdfWriter writer ; writer = PdfWriter . getInstance ( document , baos ) ; // Render in correct colors for transparent rasters writer . setRgbTransparencyBlending ( true ) ; // The mapView is not scaled to the document, we assume the mapView // has the right ratio. // Write document title and metadata document . open ( ) ; PdfContext context = new PdfContext ( writer ) ; context . initSize ( page . getBounds ( ) ) ; // first pass of all children to calculate size page . calculateSize ( context ) ; if ( resize ) { // we now know the bounds of the document // round 'm up and restart with a new document int width = ( int ) Math . ceil ( page . getBounds ( ) . getWidth ( ) ) ; int height = ( int ) Math . ceil ( page . getBounds ( ) . getHeight ( ) ) ; page . getConstraint ( ) . setWidth ( width ) ; page . getConstraint ( ) . setHeight ( height ) ; document = new Document ( new Rectangle ( width , height ) , 0 , 0 , 0 , 0 ) ; writer = PdfWriter . getInstance ( document , baos ) ; // Render in correct colors for transparent rasters writer . setRgbTransparencyBlending ( true ) ; document . open ( ) ; baos . reset ( ) ; context = new PdfContext ( writer ) ; context . initSize ( page . getBounds ( ) ) ; } // int compressionLevel = writer.getCompressionLevel(); // For testing // writer.setCompressionLevel(0); // Actual drawing document . addTitle ( "Geomajas" ) ; // second pass to layout page . layout ( context ) ; // finally render (uses baos) page . render ( context ) ; document . add ( context . getImage ( ) ) ; // Now close the document document . close ( ) ; }
Prepare the document before rendering .