Page 1 of 1

PPT corrupts after creating Image

PostPosted: Wed Jun 27, 2018 9:35 pm
by Saranya
Hi PPTX Team,

Could you please help me in getting this issue fix. I would like to add/replace an image in an particular place. Hence i followed the code provided but im geting an corrupted ppt and image also not available.

Source Code:
------------------
package psr.report.dao;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.bind.JAXBException;




import org.docx4j.openpackaging.io.SaveToZipFile;
import org.docx4j.openpackaging.packages.PresentationMLPackage;
import org.docx4j.openpackaging.parts.PartName;
import org.docx4j.openpackaging.parts.PresentationML.MainPresentationPart;
import org.docx4j.openpackaging.parts.PresentationML.SlideLayoutPart;
import org.docx4j.openpackaging.parts.PresentationML.SlidePart;
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage;
import org.pptx4j.jaxb.Context;
import org.pptx4j.pml.Pic;
//import org.pptx4j.pml.CTGraphicalObjectFrame;




/**
* @author jharrop
*
*/
public class ImageReplace {


@SuppressWarnings("deprecation")
public static void main(String[] args) throws Exception {

// Where will we save our new .pptx?
String inputfilepath = "C:\\Work\\24Jan2018_CheckOut\\SampleTemplate.pptx";

PresentationMLPackage presentationMLPackage = PresentationMLPackage.createPackage();

MainPresentationPart pp = (MainPresentationPart)presentationMLPackage.getParts().getParts().get(new PartName("/ppt/presentation.xml"));
SlideLayoutPart layoutPart = (SlideLayoutPart)presentationMLPackage.getParts().getParts().get(new PartName("/ppt/slideLayouts/slideLayout1.xml"));

// OK, now we can create a slide
SlidePart slidePart = presentationMLPackage.createSlidePart(pp, layoutPart, new PartName("/ppt/slides/slide1.xml"));

// Add image part
File file = new File("C:\\Users\\saranyac\\QUERIES\\Estimation\\PPT-PSR\\Saranya-TL\\G.png" );
BinaryPartAbstractImage imagePart
= BinaryPartAbstractImage.createImagePart(presentationMLPackage, slidePart, file);


// Add p:pic to slide
slidePart.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().add(createPicture(imagePart.getSourceRelationship().getId()));

String outputfilepath = "C:\\Work\\24Jan2018_CheckOut\\PPT-TRAILS\\Success.pptx";
//presentationMLPackage.save(new java.io.File(outputfilepath));

SaveToZipFile saver = new SaveToZipFile(presentationMLPackage);
saver.save(outputfilepath);

System.out.println("\n\n done .. saved " + outputfilepath);

}

private static Object createPicture(String relId) throws JAXBException {

// Create p:pic
java.util.HashMap<String, String>mappings = new java.util.HashMap<String, String>();

mappings.put("id1", "4");
mappings.put("name", "Picture 3");
mappings.put("descr", "G.png");
mappings.put("Red", relId );
mappings.put("offx", Long.toString(4214812));
mappings.put("offy", Long.toString(3071812));
mappings.put("extcx", Long.toString(714375));
mappings.put("extcy", Long.toString(714375));

return org.docx4j.XmlUtils.unmarshallFromTemplate(SAMPLE_PICTURE,mappings, Context.jcPML, Pic.class ) ;

}


private static String SAMPLE_PICTURE = "<p:pic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:p=\"http://schemas.openxmlformats.org/presentationml/2006/main\"> "
+ "<p:nvPicPr>"
+ "<p:cNvPr id=\"${id1}\" name=\"${name}\" descr=\"${descr}\"/>"
+ "<p:cNvPicPr>"
+ "<a:picLocks noChangeAspect=\"1\"/>"
+ "</p:cNvPicPr>"
+ "<p:nvPr/>"
+ "</p:nvPicPr>"
+ "<p:blipFill>"
+ "<a:blip r:embed=\"${Red}\" cstate=\"print\"/>"
+ "<a:stretch>"
+ "<a:fillRect/>"
+ "</a:stretch>"
+ "</p:blipFill>"
+ "<p:spPr>"
+ "<a:xfrm>"
+ "<a:off x=\"${offx}\" y=\"${offy}\"/>"
+ "<a:ext cx=\"${extcx}\" cy=\"${extcy}\"/>"
+ "</a:xfrm>"
+ "<a:prstGeom prst=\"rect\">"
+ "<a:avLst/>"
+ "</a:prstGeom>"
+ "</p:spPr>"
+ "</p:pic>";


}


Error when opening powerpoint:

Powerpoint could not read content and removed it.


Appreciate your quick help.

Attached is my ppt where in place of the images i would like to replace another image.

With Regards,
Saranya

Re: PPT corrupts after creating Image

PostPosted: Wed Jun 27, 2018 9:44 pm
by Saranya
Hi PPTX team,

for the above code it just replaces the whole template with the single image. I would be very happy and appreciate you help if it could replace only an image inside the provided template.

Please let me know or suggest me any at earliest as my project for ppt rendering has come close to deadline and im stuck with image replacement. Many many thanks in advance.

With Regards,
Saranya

Re: PPT corrupts after creating Image

PostPosted: Sat Jun 30, 2018 10:24 am
by jason
Answered at your cross post at https://stackoverflow.com/questions/510 ... ing-pptx4j

In future, please don't cross post. It creates annoying extra work for me, and doesn't make it any more likely you'll get an answer.