Page 1 of 1

Update embedded docx in pptx

PostPosted: Fri Nov 15, 2013 12:26 am
by Michelle
Hi,
I have a pptx, in which I've embedded a docx.
Now I update the embedded docx, but the emf which is included in my pptx is not updated.
When I open the modified pptx and double-click the embedded docx, it is updated and the underlying emf will be updated too.

Is there any way to update the embedded docx and also update the emf, so that I can see the update right after opening the pptx?


Code:
Code: Select all
PresentationMLPackage presentation = (PresentationMLPackage) OpcPackage.load(new ByteArrayInputStream(template));
    for (Entry<PartName, Part> part : presentation.getParts().getParts().entrySet()) {
      if (part.getKey().getName().equals("/ppt/embeddings/Microsoft_Word_Document1.docx")) {

        EmbeddedPackagePart embedded = (EmbeddedPackagePart) part.getValue();

         byte[] word = IOUtils.toByteArray(embedded.getBuffer(), embedded.getBuffer().capacity());

        WordprocessingMLPackage docx = (WordprocessingMLPackage) OpcPackage.load(new ByteArrayInputStream(word));
        Document document = docx.getMainDocumentPart().getJaxbElement();
        // do something and modify the docx...
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        SaveToZipFile saver = new SaveToZipFile(docx);
        saver.save(byteArrayOutputStream);
        byteArrayOutputStream.close();
        embedded.setBinaryData(byteArrayOutputStream.toByteArray());
      }
    }
    presentation.save(new java.io.File("c:/users/hombach/desktop/embedded.pptx"));
  }

Re: Update embedded docx in pptx

PostPosted: Fri Nov 15, 2013 6:50 am
by jason
You can create an image of the first page of the docx, by using docx4j's PDF output. This uses FOP, and can create not just PDFs, but also various image formats (TIFF, PNG): http://xmlgraphics.apache.org/fop/1.1/output.html

Or you could create a PDF, and then do PDF to image with a Java PDF library.

The first thing to explore is whether the image has to be an EMF. If not, just use one of these other formats.

Re: Update embedded docx in pptx

PostPosted: Fri Nov 15, 2013 6:04 pm
by Michelle
Thank you Jason.
The EMF is something that Powerpoint internal generates out of the embedded docx, it seems as it must be an emf...

Re: Update embedded docx in pptx

PostPosted: Fri Nov 15, 2013 7:13 pm
by jason
To be sure we are talking about the same thing, you might want to post a sample pptx.

In the absence of that, I assume your slide contains p:oleObj. If that is the case, there is no reason why the associated image couldn't be a png or tiff.

Re: Update embedded docx in pptx

PostPosted: Mon Nov 18, 2013 9:51 pm
by Michelle
Hi Jason,
my task is to fill a pptx template. That works pretty well with docx4j.
But my customer wants to put a table in the template and the table must contain images.
As Powerpoint does not support images in tables, my idea was to embed a docx my template, with a table in the customers design.
Now I managed to update the table inside the template with data, but when I open the generated pptx, I have to double-click into the embedded docx to see the changes.
Reason is, that powerpoint creates an emf which shows the content of the docx and this is only updated when leaving the editing-mode of the embeded object.

Attache you'll find my test - presentation: Double-Click into the word document and you'll see, I replace the text "TEST" with "Hello World".
Now I'm looking for a solution to see the "Hello World" - text without double-clicking into the slide...

Re: Update embedded docx in pptx

PostPosted: Tue Nov 19, 2013 9:46 am
by jason
As I see it, both the options you have identified may still be feasible:

(1) embedded docx, then updating associated image, or

(2) native pptx table, with image in front

Option 1: create a png or tiff image as per my suggestion earlier in the thread.

That will work. You need to replace in slide1:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
        <a:graphic>
          <a:graphicData uri="http://schemas.openxmlformats.org/presentationml/2006/ole">
            <mc:AlternateContent xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
              <mc:Choice xmlns:v="urn:schemas-microsoft-com:vml" Requires="v">
                <p:oleObj imgH="2304340" imgW="6263404" name="Dokument" progId="Word.Document.12" r:id="rId4" spid="_x0000_s1043">
                  <p:embed/>
                </p:oleObj>
              </mc:Choice>
              <mc:Fallback>
                <p:oleObj imgH="2304340" imgW="6263404" name="Dokument" progId="Word.Document.12" r:id="rId4">
                  <p:embed/>
                  <p:pic>
                    <p:nvPicPr>
                      <p:cNvPr id="0" name=""/>
                      <p:cNvPicPr/>
                      <p:nvPr/>
                    </p:nvPicPr>
                    <p:blipFill>
                      <a:blip r:embed="rId5"/>
                      <a:stretch>
                        <a:fillRect/>
                      </a:stretch>
                    </p:blipFill>
                    <p:spPr>
                      <a:xfrm>
                        <a:off x="827088" y="1993900"/>
                        <a:ext cx="6827837" cy="2519363"/>
                      </a:xfrm>
                      <a:prstGeom prst="rect">
                        <a:avLst/>
                      </a:prstGeom>
                    </p:spPr>
                  </p:pic>
                </p:oleObj>
              </mc:Fallback>
            </mc:AlternateContent>
          </a:graphicData>
        </a:graphic>
 
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


with just the mc:Fallback:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
        <a:graphic>
          <a:graphicData uri="http://schemas.openxmlformats.org/presentationml/2006/ole">
                <p:oleObj imgH="2304340" imgW="6263404" name="Dokument" progId="Word.Document.12" r:id="rId4">
                  <p:embed/>
                  <p:pic>
                    <p:nvPicPr>
                      <p:cNvPr id="0" name=""/>
                      <p:cNvPicPr/>
                      <p:nvPr/>
                    </p:nvPicPr>
                    <p:blipFill>
                      <a:blip r:embed="rId5"/>
                      <a:stretch>
                        <a:fillRect/>
                      </a:stretch>
                    </p:blipFill>
                    <p:spPr>
                      <a:xfrm>
                        <a:off x="827088" y="1993900"/>
                        <a:ext cx="6827837" cy="2519363"/>
                      </a:xfrm>
                      <a:prstGeom prst="rect">
                        <a:avLst/>
                      </a:prstGeom>
                    </p:spPr>
                  </p:pic>
                </p:oleObj>
          </a:graphicData>
        </a:graphic>
 
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


and in this, <a:blip r:embed="rId5"/> is what would point to your png image.

One wrinkle is that rather than an image of the entire first page of your docx, you probably want to crop it just to the actual table/image

Option 2: there is nothing to stop you placing an image in front of a table in pptx. The challenge is to size the table cells so that the image appears to be inside the cell you want. How much effort this would be depends how complex the table is.

Good luck!

Re: Update embedded docx in pptx

PostPosted: Wed Nov 27, 2013 9:39 pm
by Michelle
Hi Jason,
creating a png out of the docx and linking it to the embedded object worked for me, even if the pixture is not as beatuiful as the original.
Thank you for your support!
By the way, I've just updated to docx4j 3.0.0 and the Support to create pdf/images with FOP is great, much easier to use as in the version before. Thank you for that.