private void addMetadata() {
    try {
      DocPropsCorePart docPropsCorePart = wordMLPackage.getDocPropsCorePart();
      CoreProperties coreProps = (CoreProperties) docPropsCorePart.getContents();

      org.docx4j.docProps.core.ObjectFactory CorePropsfactory =
          new org.docx4j.docProps.core.ObjectFactory();
      org.docx4j.docProps.core.dc.elements.ObjectFactory dcElfactory =
          new org.docx4j.docProps.core.dc.elements.ObjectFactory();

      SimpleLiteral desc = dcElfactory.createSimpleLiteral();
      desc.getContent()
          .add("Created by NextReports Designer" + ReleaseInfoAdapter.getVersionNumber());
      coreProps.setDescription(dcElfactory.createDescription(desc));

      SimpleLiteral title = dcElfactory.createSimpleLiteral();
      title.getContent().add(getDocumentTitle());
      coreProps.setTitle(dcElfactory.createTitle(title));

      SimpleLiteral author = dcElfactory.createSimpleLiteral();
      author.getContent().add(ReleaseInfoAdapter.getCompany());
      coreProps.setCreator(author);

      SimpleLiteral subject = dcElfactory.createSimpleLiteral();
      subject
          .getContent()
          .add("Created by NextReports Designer" + ReleaseInfoAdapter.getVersionNumber());
      coreProps.setSubject(subject);

      coreProps.setKeywords(ReleaseInfoAdapter.getHome());
    } catch (Docx4JException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
Esempio n. 2
0
  public static WordprocessingMLPackage createPackage(PageSizePaper sz, boolean landscape)
      throws InvalidFormatException {

    // Create a package
    WordprocessingMLPackage wmlPack = new WordprocessingMLPackage();

    // Create main document part
    MainDocumentPart wordDocumentPart = new MainDocumentPart();

    // Create main document part content
    org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory();
    org.docx4j.wml.Body body = factory.createBody();
    org.docx4j.wml.Document wmlDocumentEl = factory.createDocument();

    wmlDocumentEl.setBody(body);

    // Create a basic sectPr using our Page model
    PageDimensions page = new PageDimensions();
    page.setPgSize(sz, landscape);

    SectPr sectPr = factory.createSectPr();
    body.setSectPr(sectPr);
    sectPr.setPgSz(page.getPgSz());
    sectPr.setPgMar(page.getPgMar());

    // Put the content in the part
    wordDocumentPart.setJaxbElement(wmlDocumentEl);

    // Add the main document part to the package relationships
    // (creating it if necessary)
    wmlPack.addTargetPart(wordDocumentPart);

    // Create a styles part
    Part stylesPart = new org.docx4j.openpackaging.parts.WordprocessingML.StyleDefinitionsPart();
    try {
      ((org.docx4j.openpackaging.parts.WordprocessingML.StyleDefinitionsPart) stylesPart)
          .unmarshalDefaultStyles();

      // Add the styles part to the main document part relationships
      // (creating it if necessary)
      wordDocumentPart.addTargetPart(stylesPart); // NB - add it to main doc part, not package!		

    } catch (Exception e) {
      // TODO: handle exception
      // e.printStackTrace();
      log.error(e.getMessage(), e);
    }

    // Metadata: docx4j 2.7.1 can populate some of this from docx4j.properties
    // See SaveToZipFile
    DocPropsCorePart core = new DocPropsCorePart();
    org.docx4j.docProps.core.ObjectFactory coreFactory =
        new org.docx4j.docProps.core.ObjectFactory();
    core.setJaxbElement(coreFactory.createCoreProperties());
    wmlPack.addTargetPart(core);

    DocPropsExtendedPart app = new DocPropsExtendedPart();
    org.docx4j.docProps.extended.ObjectFactory extFactory =
        new org.docx4j.docProps.extended.ObjectFactory();
    app.setJaxbElement(extFactory.createProperties());
    wmlPack.addTargetPart(app);

    // Return the new package
    return wmlPack;
  }