Hello Team,
When we insert Chart SVG into a PPT Slide using the below logic programmatically, the text does not render correctly after saving the document as PPTX file. While, when the same SVG is inserted into a PowerPoint Slide using the “Insert Pictures” option, the text gets rendered correctly.
This text rendering issue occurs only with Slides library whereas while performing the same use case with Words library it works fine. We are inserting the SVG into PowerPoint Slide after converting it to EMF image, since inserting SVG image as is into the Slide generates a PNG image copy as well within the PowerPoint document which increases the size.
Code used for inserting SVG into PowerPoint slide:
public void insertSvgIntoSlide() {
IPresentation pres = PresentationFactory.getInstance().createPresentation();
try {
Path svgPath = Paths.get("HorizontalChart.svg");
byte[] svgBytes = Files.readAllBytes(svgPath);
String svgContent = new String(svgBytes, StandardCharsets.UTF_8);
// Add SVG image to temp slide and extract EMF binary data from it
IPresentation tempPres = PresentationFactory.getInstance().createPresentation();
ISvgImage svgImage = new SvgImage(svgContent);
IPPImage emfImage = tempPres.getImages().addImage(svgImage);
byte[] emfBytes = emfImage.getBinaryData();
// Now, add the extracted EMF bytes to original slide
ISlideCollection slides = pres.getSlides();
IPPImage ppImage = pres.getImages().addImage(emfBytes);
slides.get_Item(0).getShapes().addPictureFrame(ShapeType.Rectangle, 50, 50,
ppImage.getWidth(), ppImage.getHeight(), ppImage);
pres.save("HorizontalChart.pptx", SaveFormat.Pptx);
} catch (Exception e) {
throw new IllegalStateException(e);
} finally {
if (pres != null) pres.dispose();
}
}
Code used for inserting SVG into Word Document:
public void insertSvgIntoWord() {
try {
Path svgPath = Paths.get("HorizontalChart.svg");
byte[] svgBytes = Files.readAllBytes(svgPath);
String svgContent = new String(svgBytes, StandardCharsets.UTF_8);
HtmlLoadOptions options = new HtmlLoadOptions();
options.setConvertMetafilesToPng(false);
Document document = new Document();
DocumentBuilder builder = new DocumentBuilder(document);
builder.insertImage(svgBytes);
doc.save("HorizontalChart.docx");
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
Attachment: Horizontal Chart Text Not Rendered Correctly.zip (140.8 KB)