Search code examples
javaarraysarraylistdocx4j

How can i create a list of docx4j objects in java?


I am trying to create a list of objects of pptx4j slides from the docx4j library of Java. I downloaded a sample code from http://www.docx4java.org/ and i have tried modifying it so instead of printing one slide it can print as many as i want by adding a little for loop that creates each slide.

This is the code for creating one slide:

SlidePart slidePart = presentationMLPackage.createSlidePart(pp, layoutPart, 
            new PartName("/ppt/slides/slide1.xml"));
Shape sample = ((Shape)XmlUtils.unmarshalString(SAMPLE_SHAPE, Context.jcPML) );
slidePart.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().add(sample);

so i tried doing a for loop that repeats this for each slide but i don't know how to create the objects and adding them to the array, i tried adding the object directly but it gives me errors, this is my for:

ArrayList<Object[]> lista =new ArrayList<>();
   Object[] diapositivas = new Object[3];
   for (int i=0;i<3;i++){       
      diapositivas[i] =(SlidePart) presentationMLPackage.createSlidePart(pp, layoutPart, 
      new PartName("/ppt/slides/slide[i+1].xml"));
      Shape sample = ((Shape)XmlUtils.unmarshalString(SAMPLE_SHAPE, Context.jcPML) );
      diapositivas[i].setJaxbElement( SlidePart.createSld() );
      lista.add(diapositivas);
     }

i know i'm failing at something basic here but i haven't created arrays of objects before, i tried declaring the full object every iteration but then i couldn't change the name of the object for each object so it didn't work.

it gives me a cannot find symbol error for the method getJaxbElement()and when i run the program it gives me these errors in my output:

ant -f "C:\\Documents and Settings\\Administrador\\Mis documentos\\NetBeansProjects\\JavaApplication2" -Dignore.failing.tests=true -Dnb.wait.for.caches=true -Dnb.internal.action.name=test test
init:
deps-jar:
Updating property file: C:\Documents and Settings\Administrador\Mis documentos\NetBeansProjects\JavaApplication2\build\built-jar.properties
init:
deps-clean:
Updating property file: C:\Documents and Settings\Administrador\Mis documentos\NetBeansProjects\JavaApplication2\build\built-clean.properties
Deleting directory C:\Documents and Settings\Administrador\Mis documentos\NetBeansProjects\JavaApplication2\build
clean:
Created dir: C:\Documents and Settings\Administrador\Mis documentos\NetBeansProjects\JavaApplication2\build\classes
Created dir: C:\Documents and Settings\Administrador\Mis documentos\NetBeansProjects\JavaApplication2\build\empty
Created dir: C:\Documents and Settings\Administrador\Mis documentos\NetBeansProjects\JavaApplication2\build\generated-sources\ap-source-output
Compiling 7 source files to C:\Documents and Settings\Administrador\Mis documentos\NetBeansProjects\JavaApplication2\build\classes
C:\Documents and Settings\Administrador\Mis documentos\NetBeansProjects\JavaApplication2\src\org\docx4j\samples\CreateHelloWorld.java:55: error: cannot find symbol
                        diapositivas[i].getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().add(sample);
  symbol:   method getJaxbElement()
  location: class Object
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
1 error
C:\Documents and Settings\Administrador\Mis documentos\NetBeansProjects\JavaApplication2\nbproject\build-impl.xml:923: The following error occurred while executing this line:
C:\Documents and Settings\Administrador\Mis documentos\NetBeansProjects\JavaApplication2\nbproject\build-impl.xml:263: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 7 seconds)

Solution

  • PresentationMLPackage's createSlidePart is deprecated. As per the Javadoc, use MainPresentationPart's addSlide method instead.

    That aside, getJaxbElement().getCSld() will return null unless you'ved populated the content model.

    SlidePart contains:

    public static Sld createSld() throws JAXBException {
    
        ObjectFactory factory = Context.getpmlObjectFactory(); 
        Sld sld = factory.createSld();
        sld.setCSld( 
                (CommonSlideData)XmlUtils.unmarshalString(COMMON_SLIDE_DATA, Context.jcPML, CommonSlideData.class) );
    
        return sld;     
    }
    

    so you can use that to

    diapositivas[i].setJaxbElement( SlidePart.createSld() );