我正在生成图表,我必须将其导出到 powerPoint 中。我想出了如何将图形转换为 png 并将其插入到 powerPoint 中。然而,如果您想放大图表,这并不理想。这是代码:
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.apache.batik.svggen.*;
import org.apache.poi.sl.usermodel.PictureData.PictureType;
import org.apache.poi.xslf.usermodel.XSLFPictureData;
import org.apache.poi.xslf.usermodel.XSLFPictureShape;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.batik.svggen.SVGGraphics2D;
/**
*
* @author vsolotyc
*/
public class JFreeChartToPpt {
public void createPpt() throws Exception{
// Create a new PPTX file
XMLSlideShow ppt = new XMLSlideShow();
// Create a new slide
XSLFSlide slide = ppt.createSlide();
// Generate a graph using JFreeChart
JFreeChart chart = ChartFactory.createLineChart(
"Graph Title", // chart title
"X Axis", // x axis label
"Y Axis", // y axis label
null, // dataset
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips
false // urls
);
// Convert the graph to a BufferedImage
BufferedImage image = chart.createBufferedImage(500, 300);
// Add the image to the slide
byte[] imageData = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "png", baos);
imageData = baos.toByteArray();
XSLFPictureData pd = ppt.addPicture(imageData, PictureType.PNG);
XSLFPictureShape pic = slide.createPicture(pd);
pic.setAnchor(new Rectangle(50, 50, 500, 300));
// Save the PPTX file
FileOutputStream out = new FileOutputStream("example.pptx");
ppt.write(out);
out.close();
}
}
将图形导出为pdf时,图形将转换为矢量图形。
问题:将图形作为矢量图形(类似于 pdf)导出到 powerPoint 中的最佳方法是什么。
(这里是显示 pdf 导出如何工作的链接:https://thinktibits.blogspot.com/2011/05/pdf-add-chart-itext-example-jfreechart.html)
我最终将 jFreeChart 图形转换为 svg,然后将其添加到 pptx。它非常快并且使 pptx 相当小。
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.io.FileOutputStream;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.plot.PlotOrientation;
import org.apache.poi.xslf.usermodel.XSLFPictureData;
import org.apache.poi.xslf.usermodel.XSLFPictureShape;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.jfree.graphics2d.svg.SVGGraphics2D;
import org.jfree.chart.JFreeChart;
import org.apache.poi.sl.usermodel.PictureData;
import org.jfree.data.category.DefaultCategoryDataset;
public class JFreeChartToPpt {
int WIDTH = 600;
int HEIGHT = 450;
public void createPptFromFreeChartViaSvg() throws Exception{
// Create a new PPTX file
XMLSlideShow ppt = new XMLSlideShow();
// Create a new slide
XSLFSlide slide = ppt.createSlide();
// Generate a graph using JFreeChart
JFreeChart chart = generateChart();
// Convert the graph to a vector graphic
SVGGraphics2D g2 = new SVGGraphics2D(WIDTH, HEIGHT);
Rectangle r = new Rectangle(0, 0, WIDTH, HEIGHT);
chart.draw(g2, r);
String svgXml = g2.getSVGDocument().toString();
byte[] svgBytes = svgXml.getBytes();
// adjust size and position
Dimension slideSize = ppt.getPageSize();
Rectangle2D anchor = new Rectangle2D.Double(
slideSize.getWidth()/2 - WIDTH/2, // Center horizontally
slideSize.getHeight()/2 - HEIGHT/2, // Center vertically
WIDTH, // Width
HEIGHT // Height
);
// Add the SVG image to a PowerPoint presentation
XSLFPictureData pictureData = ppt.addPicture(svgBytes, PictureData.PictureType.SVG);
XSLFPictureShape pictureShape = slide.createPicture(pictureData);
pictureShape.setAnchor(anchor);
// Save the updated PowerPoint presentation
FileOutputStream out = new FileOutputStream("exampleSvg_"+WIDTH+"x"+HEIGHT+".pptx");
ppt.write(out);
out.close();
}
}