net.sf.jasperreports.engine.export.JRGraphics2DExporter Java Examples

The following examples show how to use net.sf.jasperreports.engine.export.JRGraphics2DExporter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JRPrinterAWT.java    From nordpos with GNU General Public License v3.0 6 votes vote down vote up
/**
 *
 */
public Image printPageToImage(int pageIndex, float zoom) throws JRException
{
	PrintPageFormat pageFormat = jasperPrint.getPageFormat(pageIndex);
	
	Image pageImage = new BufferedImage(
		(int)(pageFormat.getPageWidth() * zoom) + 1,
		(int)(pageFormat.getPageHeight() * zoom) + 1,
		BufferedImage.TYPE_INT_RGB
		);

	JRGraphics2DExporter exporter = new JRGraphics2DExporter(jasperReportsContext);
	exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
	SimpleGraphics2DExporterOutput output = new SimpleGraphics2DExporterOutput();
	output.setGraphics2D((Graphics2D)pageImage.getGraphics());
	exporter.setExporterOutput(output);
	SimpleGraphics2DReportConfiguration configuration = new SimpleGraphics2DReportConfiguration();
	configuration.setPageIndex(pageIndex);
	configuration.setZoomRatio(zoom);
	exporter.setConfiguration(configuration);
	exporter.exportReport();
	
	return pageImage;
}
 
Example #2
Source File: CVElementGraphics2DHandler.java    From jasperreports with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void exportElement(
	JRGraphics2DExporterContext exporterContext,
	JRGenericPrintElement element,
	Graphics2D grx,
	Offset offset
	)
{
	try
	{
		JRGraphics2DExporter exporter = (JRGraphics2DExporter) exporterContext.getExporterRef();
		ImageDrawer imageDrawer = exporter.getDrawVisitor().getImageDrawer();

		imageDrawer.draw(
			grx,
			CVElementImageProvider.getInstance().getImage(exporterContext.getJasperReportsContext(), element),
			offset.getX(),
			offset.getY()
			);
	}
	catch (Exception e)
	{
		throw new RuntimeException(e);
	}
}
 
Example #3
Source File: MapElementGraphics2DHandler.java    From jasperreports with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void exportElement(
		JRGraphics2DExporterContext exporterContext, 
		JRGenericPrintElement element, 
		Graphics2D grx, 
		Offset offset)
{
	try
	{
		JRGraphics2DExporter exporter = (JRGraphics2DExporter)exporterContext.getExporterRef();
		ImageDrawer imageDrawer = exporter.getDrawVisitor().getImageDrawer();
		
		imageDrawer.draw(
				grx,
				MapElementImageProvider.getImage(exporterContext.getJasperReportsContext(), element), 
				offset.getX(), 
				offset.getY()
				);
	}
	catch (Exception e)
	{
		throw new RuntimeException(e);
	}
}
 
Example #4
Source File: PrintDrawVisitor.java    From jasperreports with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void visit(JRGenericPrintElement printElement, Offset offset)
{
	GenericElementGraphics2DHandler handler = 
		(GenericElementGraphics2DHandler)GenericElementHandlerEnviroment.getInstance(jasperReportsContext).getElementHandler(
				printElement.getGenericType(), 
				JRGraphics2DExporter.GRAPHICS2D_EXPORTER_KEY
				);

	if (handler != null)
	{
		handler.exportElement(this.frameDrawer.getExporterContext(), printElement, grx, offset);
	}
	else
	{
		if (log.isDebugEnabled())
		{
			log.debug("No Graphics2D generic element handler for " 
					+ printElement.getGenericType());
		}
	}
}
 
Example #5
Source File: JRDesignViewerPanel.java    From jasperreports with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
protected JRGraphics2DExporter getGraphics2DExporter() throws JRException
{
	return 
		new JRGraphics2DExporter(viewerContext.getJasperReportsContext())
		{
			@Override
			protected void initReport()
			{
				super.initReport();
				drawVisitor.setClip(true);//FIXMENOW thick border of margin elements is clipped
			}
			@Override
			protected RenderersCache getRenderersCache()
			{
				return viewerContext.getRenderersCache();
			}
		};
}
 
Example #6
Source File: JRViewerPanel.java    From jasperreports with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected JRGraphics2DExporter getGraphics2DExporter() throws JRException
{
	return 
		new JRGraphics2DExporter(viewerContext.getJasperReportsContext())
		{
			@Override
			protected RenderersCache getRenderersCache()
			{
				return viewerContext.getRenderersCache();
			}
		};
}
 
Example #7
Source File: JRPrinterAWT.java    From nordpos with GNU General Public License v3.0 5 votes vote down vote up
/**
 *
 */
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException
{
	if (Thread.interrupted())
	{
		throw new PrinterException("Current thread interrupted.");
	}

	pageIndex += pageOffset;

	if ( pageIndex < 0 || pageIndex >= jasperPrint.getPages().size() )
	{
		return Printable.NO_SUCH_PAGE;
	}

	try
	{
		JRGraphics2DExporter exporter = new JRGraphics2DExporter(jasperReportsContext);
		exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
		SimpleGraphics2DExporterOutput output = new SimpleGraphics2DExporterOutput();
		output.setGraphics2D((Graphics2D)graphics);
		exporter.setExporterOutput(output);
		SimpleGraphics2DReportConfiguration configuration = new SimpleGraphics2DReportConfiguration();
		configuration.setPageIndex(pageIndex);
		exporter.setConfiguration(configuration);
		exporter.exportReport();
	}
	catch (JRException e)
	{
		if (log.isDebugEnabled())
		{
			log.debug("Print failed.", e);
		}

		throw new PrinterException(e.getMessage()); //NOPMD
	}

	return Printable.PAGE_EXISTS;
}
 
Example #8
Source File: JRImageExporter.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
protected List generateReportImages(JasperReport report, JasperPrint jasperPrint) {
	List bufferedImages = new ArrayList();
	try{
		int height = report.getPageHeight();
		int width = report.getPageWidth();
		boolean export = true;
		int index = 0;
		while(export==true){
			BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
			Graphics2D gr2 = image.createGraphics();
			JRExporter exporter = new JRGraphics2DExporter();
			exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
			exporter.setParameter(JRGraphics2DExporterParameter.GRAPHICS_2D, gr2 );
			exporter.setParameter(JRGraphics2DExporterParameter.PAGE_INDEX, new Integer(index));
			try{
				exporter.exportReport();
			} catch(Exception e) {
				export = false;
				continue;
			}
			index++;
			bufferedImages.add(image);	
		}
	} catch (Throwable t) {
		throw new RuntimeException("Impossible to generate image", t);
	}
	return bufferedImages;
}
 
Example #9
Source File: FrameDrawer.java    From jasperreports with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 *
 */
private void draw(Collection<JRPrintElement> elements) throws JRException
{
	if (elements != null && elements.size() > 0)
	{
		Shape clipArea = grx.getClip();
		for(Iterator<JRPrintElement> it = elements.iterator(); it.hasNext();)
		{
			JRPrintElement element = it.next();
			boolean isGenericElement = element instanceof JRGenericPrintElement;
			JRGenericPrintElement genericElement =  isGenericElement ? (JRGenericPrintElement)element : null;
			GenericElementGraphics2DHandler handler = isGenericElement 
					? (GenericElementGraphics2DHandler)GenericElementHandlerEnviroment.getInstance(getJasperReportsContext()).getElementHandler(genericElement.getGenericType(), JRGraphics2DExporter.GRAPHICS2D_EXPORTER_KEY)
					: null;
			
			boolean isGenericElementToExport = isGenericElement && handler != null && handler.toExport(genericElement); 
			
			if (
				(filter != null && !filter.isToExport(element))
				|| !clipArea.intersects(
					element.getX() + elementOffset.getX() - ELEMENT_RECTANGLE_PADDING, 
					element.getY() + elementOffset.getY() - ELEMENT_RECTANGLE_PADDING, 
					element.getWidth() + 2 * ELEMENT_RECTANGLE_PADDING, 
					element.getHeight() + 2 * ELEMENT_RECTANGLE_PADDING)
				)
			{
				continue;
			}
			else if(isGenericElementToExport)
			{
				handler.exportElement(exporterContext, genericElement, grx, elementOffset);
			}
			else
			{
				element.accept(drawVisitor, elementOffset);
			}
		}
	}
}
 
Example #10
Source File: JRPrinterAWT.java    From jasperreports with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 *
 */
public Image printPageToImage(int pageIndex, float zoom) throws JRException
{
	PrintPageFormat pageFormat = jasperPrint.getPageFormat(pageIndex);
	
	int rasterWidth = (int) Math.ceil(pageFormat.getPageWidth() * zoom);
	int rasterHeight = (int) Math.ceil(pageFormat.getPageHeight() * zoom);
	Image pageImage = new BufferedImage(
		rasterWidth,
		rasterHeight,
		BufferedImage.TYPE_INT_RGB
		);
	
	Graphics imageGraphics = pageImage.getGraphics();
	Graphics graphics = imageGraphics.create();
	//filling the image background here because JRGraphics2DExporter.exportPage uses the page size
	//which can be smaller than the image size due to Math.ceil above
	graphics.setColor(Color.white);
	graphics.fillRect(0, 0, rasterWidth, rasterHeight);

	JRGraphics2DExporter exporter = new JRGraphics2DExporter(jasperReportsContext);
	exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
	SimpleGraphics2DExporterOutput output = new SimpleGraphics2DExporterOutput();
	output.setGraphics2D((Graphics2D) imageGraphics);
	exporter.setExporterOutput(output);
	SimpleGraphics2DReportConfiguration configuration = new SimpleGraphics2DReportConfiguration();
	configuration.setPageIndex(pageIndex);
	configuration.setZoomRatio(zoom);
	configuration.setWhitePageBackground(false);
	exporter.setConfiguration(configuration);
	exporter.exportReport();
	
	return pageImage;
}
 
Example #11
Source File: JRPrinterAWT.java    From jasperreports with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException
{
	if (Thread.interrupted())
	{
		throw new PrinterException("Current thread interrupted.");
	}

	pageIndex += pageOffset;

	if ( pageIndex < 0 || pageIndex >= jasperPrint.getPages().size() )
	{
		return Printable.NO_SUCH_PAGE;
	}

	try
	{
		JRGraphics2DExporter exporter = new JRGraphics2DExporter(jasperReportsContext);
		exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
		SimpleGraphics2DExporterOutput output = new SimpleGraphics2DExporterOutput();
		output.setGraphics2D((Graphics2D)graphics);
		exporter.setExporterOutput(output);
		SimpleGraphics2DReportConfiguration configuration = new SimpleGraphics2DReportConfiguration();
		configuration.setPageIndex(pageIndex);
		exporter.setConfiguration(configuration);
		exporter.exportReport();
	}
	catch (JRException e)
	{
		if (log.isDebugEnabled())
		{
			log.debug("Print failed.", e);
		}

		throw new PrinterException(e.getMessage()); //NOPMD
	}

	return Printable.PAGE_EXISTS;
}
 
Example #12
Source File: HtmlElementHandlerBundle.java    From jasperreports with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public GenericElementHandler getHandler(String elementName,
		String exporterKey)
{
	if (NAME.equals(elementName)
			&& JRPdfExporter.PDF_EXPORTER_KEY.equals(exporterKey))
	{
		return new HtmlElementPdfHandler();
	}
	else if (NAME.equals(elementName)
			&& HtmlExporter.HTML_EXPORTER_KEY.equals(exporterKey))
	{
		return new HtmlElementHtmlHandler();
	}
	else if (NAME.equals(elementName)
			&& JRXlsExporter.XLS_EXPORTER_KEY.equals(exporterKey))
	{
		return new HtmlElementXlsHandler();
	}
	else if (NAME.equals(elementName)
			&& JRGraphics2DExporter.GRAPHICS2D_EXPORTER_KEY.equals(exporterKey))
	{
		return new HtmlElementGraphics2DHandler();
	}		
	else if (NAME.equals(elementName)
			&& JRDocxExporter.DOCX_EXPORTER_KEY.equals(exporterKey))
	{
		return new HtmlElementDocxHandler();
	}		
	else if (NAME.equals(elementName)
			&& JRPptxExporter.PPTX_EXPORTER_KEY.equals(exporterKey))
	{
		return new HtmlElementPptxHandler();
	}
	else if (NAME.equals(elementName)
			&& JRXlsxExporter.XLSX_EXPORTER_KEY.equals(exporterKey))
	{
		return new HtmlElementXlsxHandler();
	}
	else if (NAME.equals(elementName)
			&& JRRtfExporter.RTF_EXPORTER_KEY.equals(exporterKey))
	{
		return new HtmlElementRtfHandler();
	}
	else if (NAME.equals(elementName)
			&& JROdtExporter.ODT_EXPORTER_KEY.equals(exporterKey))
	{
		return new HtmlElementOdtHandler();
	}
	else if (NAME.equals(elementName)
			&& JROdsExporter.ODS_EXPORTER_KEY.equals(exporterKey))
	{
		return new HtmlElementOdsHandler();
	}		
	return null;
}
 
Example #13
Source File: JasperReportsUtils.java    From JuniperBot with GNU General Public License v3.0 5 votes vote down vote up
public static BufferedImage printToImage(JasperPrint jasperPrint, int pageIndex, float zoom) throws JRException {
    JRGraphEnvInitializer.initializeGraphEnv();
    PrintPageFormat pageFormat = jasperPrint.getPageFormat(pageIndex);

    int rasterWidth = (int) Math.ceil(pageFormat.getPageWidth() * zoom);
    int rasterHeight = (int) Math.ceil(pageFormat.getPageHeight() * zoom);
    BufferedImage pageImage = new BufferedImage(
            rasterWidth,
            rasterHeight,
            BufferedImage.TYPE_INT_ARGB
    );

    Graphics2D graphics = (Graphics2D) pageImage.getGraphics();

    SimpleGraphics2DExporterOutput output = new SimpleGraphics2DExporterOutput();
    output.setGraphics2D(graphics);

    SimpleGraphics2DReportConfiguration configuration = new SimpleGraphics2DReportConfiguration();
    configuration.setPageIndex(pageIndex);
    configuration.setZoomRatio(zoom);
    configuration.setWhitePageBackground(false);

    JRGraphics2DExporter exporter = new JRGraphics2DExporter(DefaultJasperReportsContext.getInstance());
    exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
    exporter.setExporterOutput(output);
    exporter.setConfiguration(configuration);
    exporter.exportReport();

    graphics.dispose();
    return pageImage;
}
 
Example #14
Source File: IconLabelElementGraphics2DHandler.java    From jasperreports with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void exportElement(
		JRGraphics2DExporterContext exporterContext, 
		JRGenericPrintElement element, 
		Graphics2D grx, 
		Offset offset)
{
	try
	{
		JRPrintText labelPrintText = (JRPrintText)element.getParameterValue(IconLabelElement.PARAMETER_LABEL_TEXT_ELEMENT);
		if (labelPrintText == null) //FIXMEINPUT deal with xml serialization
		{
			return;
		}
		
		JRBasePrintFrame frame = new JRBasePrintFrame(element.getDefaultStyleProvider());
		frame.setX(element.getX());
		frame.setY(element.getY());
		frame.setWidth(element.getWidth());
		frame.setHeight(element.getHeight());
		frame.setStyle(element.getStyle());
		frame.setBackcolor(element.getBackcolor());
		frame.setForecolor(element.getForecolor());
		frame.setMode(element.getModeValue());
		JRLineBox lineBox = (JRLineBox)element.getParameterValue(IconLabelElement.PARAMETER_LINE_BOX);
		if (lineBox != null)
		{
			frame.copyBox(lineBox);
		}
		
		frame.addElement(labelPrintText);
		
		JRPrintText iconPrintText = (JRPrintText)element.getParameterValue(IconLabelElement.PARAMETER_ICON_TEXT_ELEMENT);
		if (iconPrintText != null) //FIXMEINPUT deal with xml serialization
		{
			frame.addElement(iconPrintText);
		}

		JRGraphics2DExporter exporter = (JRGraphics2DExporter)exporterContext.getExporterRef();
		
		FrameDrawer frameDrawer = exporter.getDrawVisitor().getFrameDrawer();
		frameDrawer.draw(
			grx, 
			frame, 
			offset.getX(), 
			offset.getY()
			);
	}
	catch (Exception e)
	{
		throw new RuntimeException(e);
	}
}
 
Example #15
Source File: AbstractSvgTest.java    From jasperreports with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
protected void export(JasperPrint print, OutputStream out) throws JRException, IOException
{
	DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
	Document document = domImpl.createDocument(null, "svg", null);
	SVGGraphics2D grx = 
		new SVGGraphics2D(
			SVGGeneratorContext.createDefault(document), 
			false // this is for textAsShapes, but does not seem to have effect in our case
			);
	
	JRGraphics2DExporter exporter = new JRGraphics2DExporter();
	
	exporter.setExporterInput(new SimpleExporterInput(print));
	SimpleGraphics2DExporterOutput output = new SimpleGraphics2DExporterOutput();
	Graphics2D g = (Graphics2D)grx.create();
	output.setGraphics2D(g);
	exporter.setExporterOutput(output);
	
	for (int pageIndex = 0; pageIndex < print.getPages().size(); pageIndex++)
	{
		g.translate(0,  pageIndex * print.getPageHeight());
		
		SimpleGraphics2DReportConfiguration configuration = new SimpleGraphics2DReportConfiguration();
		configuration.setPageIndex(pageIndex);
		exporter.setConfiguration(configuration);

		exporter.exportReport();
	}
	
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	// use OutputStreamWriter instead of StringWriter so that we have "encoding" attribute in <xml> header tag.
	grx.stream(new OutputStreamWriter(baos, "UTF-8"), true);
	
	SVGTranscoder transcoder = new SVGTranscoder();
	transcoder.addTranscodingHint(SVGTranscoder.KEY_NEWLINE, SVGTranscoder.VALUE_NEWLINE_LF);
	try
	{
		transcoder.transcode(
			new TranscoderInput(new InputStreamReader(new ByteArrayInputStream(baos.toByteArray()), "UTF-8")), 
			new TranscoderOutput(new OutputStreamWriter(out, "UTF-8"))
			);
	}
	catch (TranscoderException e)
	{
		throw new JRException(e);
	}
	
	out.close();
}
 
Example #16
Source File: CVComponentExtensionsRegistryFactory.java    From jasperreports with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
@SuppressWarnings("deprecation")
public GenericElementHandler getHandler(String elementName, String exporterKey)
{
	if (CVConstants.COMPONENT_NAME.equals(elementName))
	{
		if (JRGraphics2DExporter.GRAPHICS2D_EXPORTER_KEY.equals(exporterKey))
		{
			return CVElementGraphics2DHandler.getInstance();
		}
		if (HtmlExporter.HTML_EXPORTER_KEY.equals(exporterKey))
		{
			return CVElementHtmlHandler.getInstance();
		}
		else if (JRPdfExporter.PDF_EXPORTER_KEY.equals(exporterKey))
		{
			return CVElementPdfHandler.getInstance();
		}
		else if (JsonExporter.JSON_EXPORTER_KEY.equals(exporterKey))
		{
			return CVElementJsonHandler.getInstance();
		}
		else if (JRXlsExporter.XLS_EXPORTER_KEY.equals(exporterKey))
		{
			return CVElementXlsHandler.getInstance();
		}
		else if (JRXlsxExporter.XLSX_EXPORTER_KEY.equals(exporterKey))
		{
			return CVElementXlsxHandler.getInstance();
		}
		else if (JRDocxExporter.DOCX_EXPORTER_KEY.equals(exporterKey))
		{
			return CVElementDocxHandler.getInstance();
		}
		else if (JRPptxExporter.PPTX_EXPORTER_KEY.equals(exporterKey))
		{
			return CVElementPptxHandler.getInstance();
		}
		else if (JRRtfExporter.RTF_EXPORTER_KEY.equals(exporterKey))
		{
			return CVElementRtfHandler.getInstance();
		}
		else if (JROdtExporter.ODT_EXPORTER_KEY.equals(exporterKey))
		{
			return CVElementOdtHandler.getInstance();
		}
		else if (JROdsExporter.ODS_EXPORTER_KEY.equals(exporterKey))
		{
			return CVElementOdsHandler.getInstance();
		}
	}

	return null;
}
 
Example #17
Source File: JRViewer.java    From nordpos with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 
 */
protected JRGraphics2DExporter getGraphics2DExporter() throws JRException
{
	return new JRGraphics2DExporter(jasperReportsContext);
}