0

有人知道如何将一张图片引用到任何数量的幻灯片。我知道如何将图像添加到一张幻灯片,例如它的工作原理:

BinaryPartAbstractImage imagePart 
        = BinaryPartAbstractImage.createImagePart(presentationMLPackage, slidePart, file);
.....
  mappings.put("rEmbedId", imagePart.getSourceRelationship().getId() );
  Object o = org.docx4j.XmlUtils.unmarshallFromTemplate(SAMPLE_PICTURE, 
            mappings, Context.jcPML, Pic.class ) ;        
  Part.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().add(o);

我尝试为另一张幻灯片做同样的工作 - 它有效,但在 pptx/media 文件夹中创建了相同的图像。如果我使用imagePartwhich created forslidePart1并添加slidePart2对其执行的引用,但是当我尝试通过 PoverPoint 打开 pptx 时会出现错误。

我需要它,因为我用 docx4j 创建了 pptx 生成器

4

1 回答 1

2

在第二张幻灯片上,您需要明确添加指向图像的关系。(BinaryPartAbstractImage.createImagePart 会为您自动为第一张幻灯片执行此操作)。

这是示例代码:

import java.io.File;

import javax.xml.bind.JAXBException;

import org.apache.log4j.Logger;
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.docx4j.relationships.Relationship;
import org.pptx4j.jaxb.Context;
import org.pptx4j.pml.Pic;


public class InsertPicture  {

    protected static Logger log = Logger.getLogger(InsertPicture.class);

    public static void main(String[] args) throws Exception {

        // Where will we save our new .pptx?
        String outputfilepath = System.getProperty("user.dir") + "/pptx-picture.pptx";

        // Create skeletal package, including a MainPresentationPart and a SlideLayoutPart
        PresentationMLPackage presentationMLPackage = PresentationMLPackage.createPackage(); 

        // Need references to these parts to create a slide
        // Please note that these parts *already exist* - they are
        // created by createPackage() above.  See that method
        // for instruction on how to create and add a part.
        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(System.getProperty("user.dir") + "/src/test/resources/images/greentick.png" );
        BinaryPartAbstractImage imagePart 
            = BinaryPartAbstractImage.createImagePart(presentationMLPackage, slidePart, file);


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


        // Do it again on another slide
        SlidePart slidePart2 = presentationMLPackage.createSlidePart(pp, layoutPart, 
                new PartName("/ppt/slides/slide2.xml"));
        Relationship rel = slidePart2.addTargetPart(imagePart);

        slidePart2.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().add(
                createPicture(rel.getId()));

        // All done: save it
        presentationMLPackage.save(new java.io.File(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", "greentick.png");
        mappings.put("rEmbedId", 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=\"${rEmbedId}\" 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>";


}
于 2013-04-15T01:09:46.693 回答