List of usage examples for com.lowagie.text.pdf PdfWriter setRgbTransparencyBlending
public void setRgbTransparencyBlending(boolean rgbTransparencyBlending)
From source file:org.geomajas.plugin.print.document.SinglePageDocument.java
License:Open Source License
/** * Prepare the document before rendering. * //www . ja v a 2s . c om * @param outputStream output stream to render to, null if only for layout * @param format format * @throws DocumentException oops * @throws IOException oops * @throws PrintException oops */ private void doRender(OutputStream outputStream, Format format) throws IOException, DocumentException, PrintException { // first render or re-render for different layout if (outputStream == null || baos == null || null != format) { if (baos == null) { baos = new ByteArrayOutputStream(); // let it grow as much as needed } baos.reset(); boolean resize = false; if (page.getBounds().getWidth() == 0 || page.getBounds().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(); // convert to non-pdf format switch (format) { case PDF: break; case PNG: case JPG: BufferedImage bufferedImage = null; // Use JPedal lib for converting the PDF to PNG or JPG /** instance of PdfDecoder to convert PDF into image */ PdfDecoder decodePdf = new PdfDecoder(true); /** set mappings for non-embedded fonts to use */ PdfDecoder.setFontReplacements(decodePdf); decodePdf.useHiResScreenDisplay(true); decodePdf.getDPIFactory().setDpi(2 * 72); decodePdf.setPageParameters(1, 1); try { decodePdf.openPdfArray(baos.toByteArray()); /** get page 1 as an image */ bufferedImage = decodePdf.getPageAsImage(1); /** close the pdf file */ decodePdf.closePdfFile(); } catch (PdfException e) { throw new PrintException(e, PrintException.DOCUMENT_RENDER_PROBLEM); } baos.reset(); // Update the DPI to DPI_FOR_PNG_OUTPUT of bufferedImage, output written to baos final String formatName = format.getExtension(); boolean convertedDPI = false; for (Iterator<ImageWriter> iw = ImageIO.getImageWritersByFormatName(formatName); iw.hasNext();) { ImageWriter writer1 = iw.next(); ImageWriteParam writeParam = writer1.getDefaultWriteParam(); ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier .createFromBufferedImageType(BufferedImage.TYPE_INT_ARGB); IIOMetadata metadata = writer1.getDefaultImageMetadata(typeSpecifier, writeParam); if (metadata.isReadOnly() || !metadata.isStandardMetadataFormatSupported()) { continue; } setDPI(metadata); // Convert bufferedImage to baos final ImageOutputStream stream = ImageIO.createImageOutputStream(baos/*output*/); try { writer1.setOutput(stream); writer1.write(metadata, new IIOImage(bufferedImage/*input*/, null/* No thumbnails*/, metadata), writeParam); convertedDPI = true; } finally { stream.flush(); stream.close(); } break; } if (!convertedDPI) { baos.reset(); //ImageIO.setUseCache(false); ImageIO.write(bufferedImage, format.getExtension(), baos); } break; default: throw new IllegalStateException( "Oops, software error, need to support extra format at end of render" + format); } if (outputStream != null) { try { baos.writeTo(outputStream); } catch (IOException e) { throw e; } } } else { baos.writeTo(outputStream); } }
From source file:org.geomajas.plugin.printing.document.SinglePageDocument.java
License:Open Source License
/** * Prepare the document before rendering. * //w ww. j a v a2 s . c om * @param outputStream output stream to render to, null if only for layout * @param format format * @throws DocumentException oops * @throws IOException oops * @throws PrintingException oops */ private void doRender(OutputStream outputStream, Format format) throws IOException, DocumentException, PrintingException { // first render or re-render for different layout if (outputStream == null || baos == null || null != format) { if (baos == null) { baos = new ByteArrayOutputStream(); // let it grow as much as needed } baos.reset(); boolean resize = false; if (page.getBounds().getWidth() == 0 || page.getBounds().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(); // convert to non-pdf format switch (format) { case PDF: break; case PNG: case JPG: BufferedImage bufferedImage = null; // Use JPedal lib for converting the PDF to PNG or JPG /** instance of PdfDecoder to convert PDF into image */ PdfDecoder decodePdf = new PdfDecoder(true); /** set mappings for non-embedded fonts to use */ PdfDecoder.setFontReplacements(decodePdf); decodePdf.useHiResScreenDisplay(true); decodePdf.getDPIFactory().setDpi(2 * 72); decodePdf.setPageParameters(1, 1); try { decodePdf.openPdfArray(baos.toByteArray()); /** get page 1 as an image */ bufferedImage = decodePdf.getPageAsImage(1); /** close the pdf file */ decodePdf.closePdfFile(); } catch (PdfException e) { throw new PrintingException(e, PrintingException.DOCUMENT_RENDER_PROBLEM); } baos.reset(); // Update the DPI to DPI_FOR_PNG_OUTPUT of bufferedImage, output written to baos final String formatName = format.getExtension(); boolean convertedDPI = false; for (Iterator<ImageWriter> iw = ImageIO.getImageWritersByFormatName(formatName); iw.hasNext();) { ImageWriter writer1 = iw.next(); ImageWriteParam writeParam = writer1.getDefaultWriteParam(); ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier .createFromBufferedImageType(BufferedImage.TYPE_INT_ARGB); IIOMetadata metadata = writer1.getDefaultImageMetadata(typeSpecifier, writeParam); if (metadata.isReadOnly() || !metadata.isStandardMetadataFormatSupported()) { continue; } setDPI(metadata); // Convert bufferedImage to baos final ImageOutputStream stream = ImageIO.createImageOutputStream(baos/*output*/); try { writer1.setOutput(stream); writer1.write(metadata, new IIOImage(bufferedImage/*input*/, null/* No thumbnails*/, metadata), writeParam); convertedDPI = true; } finally { stream.flush(); stream.close(); } break; } if (!convertedDPI) { baos.reset(); //ImageIO.setUseCache(false); ImageIO.write(bufferedImage, format.getExtension(), baos); } break; default: throw new IllegalStateException( "Oops, software error, need to support extra format at end of render" + format); } if (outputStream != null) { try { baos.writeTo(outputStream); } catch (IOException e) { throw e; } } } else { baos.writeTo(outputStream); } }