Page 1 of 1

Adding background

PostPosted: Thu Jan 19, 2012 5:00 am
by mil
Hi, i am currently trying to add an image as a background. I played with docx4j and now i understand the syntax in the xml files. Buf when im looking at the part in the xml file that concern the background its not talking about the image its using in the background. like there is no place talking about the image.jpg. Id like to have an example of someone using a background in a pptx with docx4j or tell me what to do to put a background.

Re: Adding background

PostPosted: Thu Jan 19, 2012 10:23 am
by jason
I added a picture background to a slide (in Powerpoint 2010).

From running the result through PartsList, it is easy to see the image is a rel of the slide:

Code: Select all
    Part /ppt/presentation.xml [org.docx4j.openpackaging.parts.PresentationML.MainPresentationPart] http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument containing JaxbElement:org.pptx4j.pml.Presentation

        Part /ppt/presProps.xml [org.docx4j.openpackaging.parts.PresentationML.PresentationPropertiesPart] http://schemas.openxmlformats.org/officeDocument/2006/relationships/presProps containing JaxbElement:org.pptx4j.pml.PresentationPr

        Part /ppt/slides/slide1.xml [org.docx4j.openpackaging.parts.PresentationML.SlidePart] http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide containing JaxbElement:org.pptx4j.pml.Sld

            Part /ppt/media/image1.jpg [org.docx4j.openpackaging.parts.WordprocessingML.ImageJpegPart] http://schemas.openxmlformats.org/officeDocument/2006/relationships/image


Looking then at /ppt/slides/slide1.xml, we see:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
<p:sld 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:cSld>
    <p:bg>
      <p:bgPr>
        <a:blipFill dpi="0" rotWithShape="1">
          <a:blip r:embed="rId2">
            <a:lum/>
          </a:blip>
          <a:srcRect/>
          <a:stretch>
            <a:fillRect/>
          </a:stretch>
        </a:blipFill>
        <a:effectLst/>
      </p:bgPr>
    </p:bg>
    <p:spTree>
       :
 
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


So, you need to add the image as a rel to your slide:

1. create a Part for your image (eg ImageJpegPart)
2. add it, using Relationship rel = slide.addTargetPart(imagePart)
3. then set p:bg using getJaxbElement().getCSld().setBg(value). When you construct the background value, you'll need to use the rel id (obtained for rel in step 2 above) for @r:embed

Re: Adding background

PostPosted: Tue Jan 24, 2012 5:57 am
by mil
i tryed to do .setBg(rel.getId()); but its asking a CTBackground object for setBG.Im not so sure how to build the background. Here is the code i tryed.

Code: Select all
private static String BACKGROUND="<p:bg><p:bgPr><a:blipFill dpi=\"0\" rotWithShape=\"1\"><a:blip r:embed=\"${rID}\" cstate=\"print\"><a:lum/></a:blip><a:srcRect/><a:stretch><a:fillRect/></a:stretch></a:blipFill><a:effectLst/></p:bgPr></p:bg>";
   

   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"));

      // Create slide page titre
      SlidePart slidePart = presentationMLPackage.createSlidePart(pp,
            layoutPart, new PartName("/ppt/slides/slide4.xml"));

      //background
      
      File file = new File(
            "C:/Documents and Settings/guenetMO/Desktop/RapportTest/background.jpg");

      BinaryPartAbstractImage imagePartBG = BinaryPartAbstractImage
            .createImagePart(presentationMLPackage, slidePart, file);
      
      Relationship rel = slidePart.addTargetPart(imagePartBG);
      
      java.util.HashMap<String, String> mappingsBG = new java.util.HashMap<String, String>();

      mappingsBG.put("rID", rel.getId());
      
      Object oBG = org.docx4j.XmlUtils.unmarshallFromTemplate(BACKGROUND,
            mappingsBG, Context.jcPML, Pic.class);
      
      slidePart.getJaxbElement().getCSld().setBg((CTBackground) oBG);
   

Re: Adding background

PostPosted: Tue Jan 24, 2012 6:28 am
by mil
Thank you it worked i managed to make it work here is the code working :



Code: Select all
   private static String BACKGROUND="<p:bg 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:bgPr><a:blipFill dpi=\"0\" rotWithShape=\"1\"><a:blip r:embed=\"${rID}\" cstate=\"print\"><a:lum/></a:blip><a:srcRect/><a:stretch><a:fillRect/></a:stretch></a:blipFill><a:effectLst/></p:bgPr></p:bg>";
   

   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"));

      // Create slide page titre
      SlidePart slidePart = presentationMLPackage.createSlidePart(pp,
            layoutPart, new PartName("/ppt/slides/slide4.xml"));

      //background
      
      File file = new File(
            "C:/Documents and Settings/guenetMO/Desktop/RapportTest/background.jpg");

      BinaryPartAbstractImage imagePartBG = BinaryPartAbstractImage
            .createImagePart(presentationMLPackage, slidePart, file);
      
      Relationship rel = slidePart.addTargetPart(imagePartBG);
      
      java.util.HashMap<String, String> mappingsBG = new java.util.HashMap<String, String>();

      mappingsBG.put("rID", rel.getId());
      
      Object oBG = org.docx4j.XmlUtils.unmarshallFromTemplate(BACKGROUND,
            mappingsBG, Context.jcPML, CTBackground.class);
      

      slidePart.getJaxbElement().getCSld().setBg((CTBackground) oBG);