Page 1 of 1

How to add\replace image in an existing PPTX file

PostPosted: Tue Jun 26, 2018 12:02 am
by Saranya
Hi Team,

Actually thanks for docx4j for the replaceVariable method which has made my life easier 8-) . Now I have an PPTX template in which i would like to add\replace an image based on some condition.
I already used variableReplace() and replaced all my text content. Now please please let me know how can i add an image to the pptx or replace it in an easier way. I'm eagerly waiting for an positive response.

I tried to use the same text replacement method but i got the below error,

19403 [main] WARN org.docx4j.XmlUtils - Invalid key 'Schedule</a:t></a:r><a:r><a:rPr lang="en-US" altLang="de-DE" sz="1000" smtClean="0"><a:solidFill><a:srgbClr val="000000"/></a:solidFill></a:rPr><a:t>' or key not mapped to a value


Code,

File image = new File("C:\\Work\\24Jun2018\\IT\\images\\adobe.gif");

String str = FileUtils.readFileToString(image);

HashMap<String, String> mappings = new HashMap<String, String>();

mappings.put("Schedule", str);

slide.variableReplace(mappings);


Please let me know how to proceed.

PPT file attached for reference


With Regards,
Saranya

Re: How to add\replace image in an existing PPTX file

PostPosted: Tue Jun 26, 2018 11:38 pm
by Saranya
Hi @Jason:
Could you please help me out how to replace an image??
Alternatively i tried traversing the PPT and convert into xml and replace value. But I'm getting error,


14285 [main] WARN org.docx4j.jaxb.JaxbValidationEventHandler - [ERROR] : unexpected element (uri:"http://schemas.openxmlformats.org/presentationml/2006/main", local:"cSld"). Expected elements a
14285 [main] WARN org.docx4j.jaxb.JaxbValidationEventHandler - Column is 967 at line number 1
14285 [main] INFO org.docx4j.jaxb.JaxbValidationEventHandler - shouldContinue is set to false
Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"http://schemas.openxmlformats.org/presentationml/2006/main", local:"cSld"). Expected elements are <{http://schemas.openxmlformats.org/presentationml/2006/main}spPr>,<{http://schemas.openxmlformats.org/presentationml/2006/main}extLst>,<{http://schemas.openxmlformats.org/presentationml/2006/main}blipFill>,<{http://schemas.openxmlformats.org/presentationml/2006/main}nvPicPr>,<{http://schemas.openxmlformats.org/presentationml/2006/main}style>
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:697)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:243)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:238)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:105)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.childElement(Loader.java:90)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StructureLoader.childElement(StructureLoader.java:236)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:534)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:516)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:125)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:379)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2786)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:117)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:648)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:203)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:181)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:235)
at org.docx4j.XmlUtils.unmarshalString(XmlUtils.java:537)
at org.docx4j.XmlUtils.unmarshallFromTemplate(XmlUtils.java:655)
at psr.report.dao.ImageReplace.createPicture(ImageReplace.java:80)
at psr.report.dao.ImageReplace.main(ImageReplace.java:57)

Re: How to add\replace image in an existing PPTX file

PostPosted: Wed Jun 27, 2018 12:33 am
by Saranya
Please find my source code header,

private static String SAMPLE_PICTURE = "<p:sld xmlns:comp=\"http://schemas.openxmlformats.org/drawingml/2006/compatibility\" xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\" xmlns:cSld=\"http://schemas.openxmlformats.org/presentationml/2006/main\" xmlns:xdr=\"http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing\" xmlns:c=\"http://schemas.openxmlformats.org/drawingml/2006/chart\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:p=\"http://schemas.openxmlformats.org/presentationml/2006/main\" xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" xmlns:lc=\"http://schemas.openxmlformats.org/drawingml/2006/lockedCanvas\" xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\" xmlns:cdr=\"http://schemas.openxmlformats.org/drawingml/2006/chartDrawing\" xmlns:wp14=\"http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing\" xmlns:dgm=\"http://schemas.openxmlformats.org/drawingml/2006/diagram\" xmlns:spPr=\"http://schemas.openxmlformats.org/presentationml/2006/main\" xmlns:extLst=\"http://schemas.openxmlformats.org/presentationml/2006/main\" xmlns:blipFill=\"http://schemas.openxmlformats.org/presentationml/2006/main\" xmlns:nvPicPr=\"http://schemas.openxmlformats.org/presentationml/2006/main\" xmlns:style=\"http://schemas.openxmlformats.org/presentationml/2006/main\">"