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();
}
@oraspose,
Your sample code contains an unknown variable pptx and I was unable to use this code. I have also not managed to reproduce the issue on my end. Please share the following data and information:
compilable code example
SVG image file (that is not inserted in Word document)
Hi Andrey.
Thanks for the quick response.
Here is the information requested:
OS: Oracle Linux 7 (x86-64) UEK Release 4 ( https://yum.oracle.com/repo/OracleLinux/OL7/UEKR4/archive/x86_64/index.html )
java full version “1.8.0_331-b09”
SVG image can be copied directly from the Word doc, if it is opened with 7-zip , WinRar, WinZip or any other file archiver. It is located under Word/media/image2.svg
Full compileable source is below.
Thank you.
import java.util.Arrays;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import org.apache.commons.io.IOUtils;
import com.aspose.slides.License;
import com.aspose.slides.Presentation;
import com.aspose.slides.ISlide;
import com.aspose.slides.ISvgImage;
import com.aspose.slides.IPPImage;
import com.aspose.slides.IPictureFrame;
import com.aspose.slides.IPresentation;
import com.aspose.slides.SaveFormat;
import com.aspose.slides.ShapeType;
import com.aspose.slides.SvgImage;
import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class ImageToPresentation {
final static String CHART_LEGEND_UNDERSCORE_SVG = "/scratch/tmp/ChartUnderscore.svg";
final static String CHART_LEGEND_PPTX = "/scratch/tmp/ChartUnderscore.pptx";
private static final Predicate<String> SVG_XML = Pattern.compile("<svg .+", Pattern.CASE_INSENSITIVE).asPredicate();
public static void main(String[] args) throws Exception {
applyLicense("./Aspose.Slides.lic");
IPresentation 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 imageForSlide;
if (isSvgImage(imagebytes)) {
imagebytes = convertSvgImageToEmf(imagebytes, imageToPptx);
}
// Add an Image inside the images collection of the presentation
imageForSlide = imageToPptx.getImages().addImage(imagebytes);
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 static boolean isSvgImage(byte[] imagebytes) {
return (imagebytes.length > 100 && SVG_XML.test(new String(Arrays.copyOf(imagebytes, 100))));
}
private static 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();
}
public static void applyLicense(String fileName) throws Exception {
InputStream licenseStream = null;
try {
// Try to load the license file from the classpath.
licenseStream = ImageToPresentation.class.getResourceAsStream(fileName);
if (licenseStream != null) {
License wordsLicense = new License();
wordsLicense.setLicense(licenseStream);
}
} finally {
// close quietly
if (licenseStream != null) {
try {
licenseStream.close();
licenseStream = null;
} catch (Exception ex) {}
}
}
}
}
@oraspose,
Thank you for the additional information.
I’ve reproduced the problem with underlines when importing the SVG image into a presentation and added a ticket with ID SLIDESJAVA-38955 to our issue-tracking system. We apologize for any inconvenience. Our development team will investigate the case. You will be notified when a new release of Aspose.Slides with a fix is published.