John Smith
John Smith

Reputation: 851

How to replace an image in PPT presentation with Apache POI

I'd like to know, if there any way to replace images in ppt presentaions via Apache POI?

I have a template, where I've placed the elements (text fields and images) and I found out how to replace text, but didn't find anything for images.

Upvotes: 1

Views: 3786

Answers (1)

kiwiwings
kiwiwings

Reputation: 3446

Replacing images can be done in two ways:

Simply replace the image inside pptx-file, which is a zip file, under the path (/ppt/media). Checkout this post howto do it ..

or the POI method is to remove the file and add a new one ... and maybe change few other image properties (width, height, ...) - see below for an example

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;

import javax.xml.namespace.QName;

import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackagePartName;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.openxml4j.opc.PackagingURIHelper;
import org.apache.poi.openxml4j.opc.TargetMode;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSheet;
import org.apache.xmlbeans.XmlCursor;

public class ReplaceImageInPptx {
    public static void main(String[] args) throws Exception {
        FileInputStream fis = new FileInputStream("test2.pptx");
        XMLSlideShow pptx = new XMLSlideShow(fis);
        fis.close();

        String blipNS[] = {
            "http://schemas.openxmlformats.org/drawingml/2006/main",
            "http://schemas.openxmlformats.org/presentationml/2006/main"
        };

        for (XSLFSheet slide : pptx.getSlides()) {
            PackagePart packPart = slide.getPackagePart();

            for (String ns : blipNS) {
                XmlCursor picCur = slide.getXmlObject().newCursor();
                picCur.selectPath("declare namespace p='"+ns+"' .//p:blip"); // or blipFill

                while (picCur.toNextSelection()) {

                    // ... doesn't work for all namespaces ...
                    // CTBlipFillProperties blipFill = (CTBlipFillProperties)picCur.getObject();
                    // CTBlip blip = blipFill.getBlip();
                    // String relId = blip.getEmbed();
                    QName relName = new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "embed");
                    String relId = picCur.getAttributeText(relName);

                    // remove old media file and reference
                    PackageRelationship packRel = packPart.getRelationship(relId);
                    PackagePartName oldPartName = PackagingURIHelper.createPartName(packRel.getTargetURI());
                    packPart.getPackage().removePart(oldPartName);

                    // add something new
                    PackagePartName partName = PackagingURIHelper.createPartName("/ppt/media/smiley.jpg");
                    PackagePart part = pptx.getPackage().createPart(partName, "image/jpeg");
                    OutputStream partOs = part.getOutputStream();
                    FileInputStream fis2 = new FileInputStream("src/test/resources/smiley.jpg");
                    byte buf[] = new byte[1024];
                    for (int readBytes; (readBytes = fis2.read(buf)) != -1; partOs.write(buf, 0, readBytes));
                    fis2.close();
                    partOs.close();

                    PackageRelationship prs = slide.getPackagePart().addRelationship(partName, TargetMode.INTERNAL, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image");
                    // blip.setEmbed(prs.getId());
                    picCur.setAttributeText(relName, prs.getId());

                    // maybe change the size a bit
                    // blipFill.getStretch().getFillRect().setL(<left padding in % (+/-)>)
                }

                picCur.dispose();
            }
        }

        FileOutputStream fos = new FileOutputStream("test3.pptx");
        pptx.write(fos);
        fos.close();
    }
}

Upvotes: 2

Related Questions