I am trying to export a timeseries XY linechart into a pptx document using JFreechart-FreeHEP vector graphics-apache poi XSLF. The logic i follow is as below
The problem is with the way in which lines are rendered in the EMF image. please see the image below. The lines rendered are not exactly straight, they are kind of zig-zagged. The horizontal lines are rendered straight, but the vertical lines are not rendered as straight lines, they are like -> /\/\/\/\/\/\/\/\/\ . Appreciate any suggestions towards getting the lines to be rendered as straight lines.
This issue is only with EMF format, I tried the same with PNG,JPG format and the lines are rendered as straight lines. The reason why i am using EMF is because , after exporting to pptx, other picture formats (JPEG/PNG/TIF/BMP) seem to lose their quality - clarity and brightness.
code i am using is as below
chart = createChart(createDataset());
ChartPanel panel = new ChartPanel(chart);
EMFGraphics2D emffile = new EMFGraphics2D(new File(
"C:\\Users\\out\\chart.emf"),
new Dimension(1500, 600));
emffile.setDeviceIndependent(true);
emffile.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
emffile.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
RenderingHints.VALUE_STROKE_NORMALIZE);
emffile.startExport();
chart.draw((Graphics2D) emffile.create(), new Rectangle(1500, 600));
emffile.endExport();
emffile.closeStream();
XMLSlideShow pptx = new XMLSlideShow();
XSLFSlide slide = pptx.createSlide();
byte[] pptxpic = IOUtils.toByteArray(new FileInputStream(
"C:\\Users\\out\\chart.emf"));
int idx = pptx.addPicture(pptxpic, XSLFPictureData.PICTURE_TYPE_EMF);
XSLFPictureShape pic = slide.createPicture(idx);
pic.setAnchor(new java.awt.Rectangle(0, 25, 720, 500));
pptx.write(new FileOutputStream(
"C:\\Users\\out\\poitest002.pptx"));
Fixed this issue by saving the file in higher resolution and then crop to a lower one when inserting in the slide. The chart was saved as a png file before inserting that in the slide.
byte[] pptxpic = null;
ChartUtilities.saveChartAsPNG(pngfile, chart, 1000, 800);
pptxpic = IOUtils.toByteArray(new FileInputStream(pngfile));
int idx = pptxTemplate.addPicture(pptxpic,
XSLFPictureData.PICTURE_TYPE_PNG);
XSLFPictureShape pic = slidedips.createPicture(idx);
pic.setAnchor(new java.awt.Rectangle(0, 60, 960, 630));