如何在java中创建docx4j对象列表?

时间:2015-05-07 00:39:00

标签: java arrays arraylist docx4j

我正在尝试从Java的docx4j库创建pptx4j幻灯片的对象列表。我从http://www.docx4java.org/下载了一个示例代码,我尝试修改它,所以不是打印一张幻灯片,而是可以通过添加一个创建每张幻灯片的for循环来打印任意多个。

这是创建一张幻灯片的代码:

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

所以我尝试做一个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);
     }

我知道我在这里遇到了一些基本的东西,但我之前没有创建过对象数组,我尝试在每次迭代时声明完整的对象,但后来我无法为每个对象更改对象的名称,所以它没有不行。

它给我找不到方法getJaxbElement()的符号错误,当我运行程序时,它在输出中给出了这些错误:

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)

1 个答案:

答案 0 :(得分:0)

不推荐使用PresentationMLPackage的createSlidePart。根据Javadoc,请使用MainPresentationPart的addSlide方法。

除此之外,getJaxbElement()。getCSld()将返回null,除非您已填充内容模型。

SlidePart包含:

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;     
}

所以你可以用它来

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