Hi,
I am using Aspose.Slides 22.10 API to insert charts in SVG format into PowerPoint presentation.
It appears in some cases (in particular when the chart has underscores in X-axis, Y-axis, legends etc) the result does not look the same as the source chart - the underscores do not extend for the full length of the words “X-axis” and “Primary Y-axis”.
See attached Word document that captured the differences.
Kindly find the sample code below and look into the issue. Please let me know if you need more information.
Thank you,
Yan.
Sample test code:
final static String CHART_LEGEND_UNDERSCORE_SVG = "/scratch/tmp/ChartUnderscore.svg";
final static String CHART_LEGEND_PPTX = "/scratch/tmp/ChartUnderscore.pptx";
Presentation imageToPptx = new Presentation();
ISlide iSlide = imageToPptx.getSlides().get_Item(0);
byte[] imagebytes = null;
// Try block to check for exceptions
try {
imagebytes = IOUtils.toByteArray(new FileInputStream(new File(CHART_LEGEND_UNDERSCORE_SVG)));
} catch (IOException e) {
// Print and display the exceptions
System.out.println(e);
}
final IPPImage imgx;
if (isSvgImage(imagebytes)) {
imagebytes = convertSvgImageToEmf(imagebytes, pptx);
}
imgx = pptx.getImages().addImage(imagebytes);
// Add an Image inside the images collection of the presentation
IPPImage imageForSlide = imageToPptx.getImages().addImage(imgx);
int imageWidth = imageForSlide.getWidth(); // this is in pixels
int imageHeight = imageForSlide.getHeight(); // this is in pixels
double slideWidth = imageToPptx.getSlideSize().getSize().getWidth(); // this will be in dots
// we get imageWidth in pixels,
// hence slideWidth needs to be adjusted to pixels
// there are 72 dots per inch and aspose uses a standard 96 dpi
slideWidth = (96.0d/72)*slideWidth;
float newWidth = (float)(imageWidth > slideWidth ? slideWidth : imageWidth);
// Insert a picture frame with image in the shapes collection of the slide
IPictureFrame pf = iSlide.getShapes().addPictureFrame(ShapeType.Rectangle, 10, 20,
newWidth, imageHeight * (newWidth/imageWidth), imageForSlide);
// Save the presentation with added image on the disk
imageToPptx.save(CHART_LEGEND_PPTX, SaveFormat.Pptx);
}
private byte[] convertSvgImageToEmf(byte[] imagebytes, IPresentation pptx) {
ISvgImage svgImage = new SvgImage(imagebytes);
// Not the addImage() will not save the svgImage into given presentation pptx when it is persisted.
IPPImage tmpImage = pptx.getImages().addImage(svgImage);
return tmpImage.getBinaryData();
}
ChartWithUnderscoresInXandYaxis.docx (275.1 KB)