net.sf.jasperreports.export.SimpleGraphics2DReportConfiguration Java Examples

The following examples show how to use net.sf.jasperreports.export.SimpleGraphics2DReportConfiguration. 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: 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 #3
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 #4
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 #5
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 #6
Source File: JRViewerPanel.java    From jasperreports with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected void paintPage(Graphics2D grx)
{
	if (pageError)
	{
		paintPageError(grx);
		return;
	}
	
	try
	{
		if (exporter == null)
		{
			exporter = getGraphics2DExporter();
		}
		else
		{
			exporter.reset();
		}

		exporter.setExporterInput(new SimpleExporterInput(viewerContext.getJasperPrint()));

		SimpleGraphics2DReportConfiguration configuration = new SimpleGraphics2DReportConfiguration();
		configuration.setPageIndex(viewerContext.getPageIndex());
		configuration.setZoomRatio(realZoom);
		configuration.setOffsetX(1); //lblPage border
		configuration.setOffsetY(1);
		exporter.setConfiguration(configuration);

		SimpleGraphics2DExporterOutput output = new SimpleGraphics2DExporterOutput();
		Graphics2D g = (Graphics2D)grx.create();
		output.setGraphics2D(g);
		exporter.setExporterOutput(output);

		try
		{
			exporter.exportReport();
		}
		finally
		{
			g.dispose();
		}
	}
	catch(Exception e)
	{
		if (log.isErrorEnabled())
		{
			log.error("Page paint error.", e);
		}

		pageError = true;
		
		paintPageError(grx);
		SwingUtilities.invokeLater(new Runnable()
		{
			@Override
			public void run()
			{
				JOptionPane.showMessageDialog(JRViewerPanel.this, viewerContext.getBundleString("error.displaying"));
			}
		});
	}

}
 
Example #7
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 #8
Source File: JRViewer.java    From nordpos with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 
 */
protected void paintPage(Graphics2D grx)
{
	if (pageError)
	{
		paintPageError(grx);
		return;
	}
	
	try
	{
		if (exporter == null)
		{
			exporter = getGraphics2DExporter();
		}
		else
		{
			exporter.reset();
		}

		exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
		SimpleGraphics2DExporterOutput output = new SimpleGraphics2DExporterOutput();
		output.setGraphics2D((Graphics2D)grx.create());
		exporter.setExporterOutput(output);
		SimpleGraphics2DReportConfiguration configuration = new SimpleGraphics2DReportConfiguration();
		configuration.setPageIndex(pageIndex);
		configuration.setZoomRatio(realZoom);
		configuration.setOffsetX(1); //lblPage border
		configuration.setOffsetY(1);
		exporter.setConfiguration(configuration);
		exporter.exportReport();
	}
	catch(Exception e)
	{
		if (log.isErrorEnabled())
		{
			log.error("Page paint error.", e);
		}
		pageError = true;
		
		paintPageError(grx);
		SwingUtilities.invokeLater(new Runnable()
		{
			public void run()
			{
				JOptionPane.showMessageDialog(JRViewer.this, getBundleString("error.displaying"));
			}
		});
	}

}