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();
    }
  }
Beispiel #2
0
  public void saveRawXmlPart(ZipOutputStream out, Part part, String zipEntryName)
      throws Docx4JException {

    try {

      if (part instanceof org.docx4j.openpackaging.parts.JaxbXmlPart) {

        // From docx4j 2.7.1, support setting some basic metadata
        // from docx4j.properties
        if (part instanceof DocPropsCorePart) {

          boolean dcWrite =
              Boolean.parseBoolean(
                  Docx4jProperties.getProperties().getProperty("docx4j.dc.write", "false"));
          if (dcWrite) {
            CoreProperties cp = ((DocPropsCorePart) part).getJaxbElement();

            // Only set creator if not already present
            String creator =
                Docx4jProperties.getProperties().getProperty("docx4j.dc.creator.value", "docx4j");
            if (cp.getCreator() == null) {
              org.docx4j.docProps.core.dc.elements.ObjectFactory of =
                  new org.docx4j.docProps.core.dc.elements.ObjectFactory();
              cp.setCreator(of.createSimpleLiteral());
              cp.getCreator().getContent().add(creator);
            }

            String modifier =
                Docx4jProperties.getProperties()
                    .getProperty("docx4j.dc.lastModifiedBy.value", "docx4j");
            cp.setLastModifiedBy(modifier);
          }
        }
        if (part instanceof DocPropsExtendedPart) {
          boolean appWrite =
              Boolean.parseBoolean(
                  Docx4jProperties.getProperties().getProperty("docx4j.App.write", "false"));
          if (appWrite) {
            Properties cp = ((DocPropsExtendedPart) part).getJaxbElement();

            cp.setApplication(
                Docx4jProperties.getProperties().getProperty("docx4j.Application", "docx4j"));

            String version = Docx4jProperties.getProperties().getProperty("docx4j.AppVersion");
            if (version != null) {
              cp.setAppVersion(version);
            }
          }
        }

        // Add ZIP entry to output stream.
        out.putNextEntry(new ZipEntry(zipEntryName));

        ((org.docx4j.openpackaging.parts.JaxbXmlPart) part).marshal(out);

        // Complete the entry
        out.closeEntry();
        log.info("success writing part: " + zipEntryName);

      } else if (part instanceof org.docx4j.openpackaging.parts.CustomXmlDataStoragePart) {

        // Add ZIP entry to output stream.
        out.putNextEntry(new ZipEntry(zipEntryName));

        ((org.docx4j.openpackaging.parts.CustomXmlDataStoragePart) part)
            .getData()
            .writeDocument(out);

        // Complete the entry
        out.closeEntry();
        log.info("success writing part: " + zipEntryName);

      } else if (part instanceof org.docx4j.openpackaging.parts.XmlPart) {

        // Add ZIP entry to output stream.
        out.putNextEntry(new ZipEntry(zipEntryName));

        Document doc = ((org.docx4j.openpackaging.parts.XmlPart) part).getDocument();

        /*
        * With Crimson, this gives:
        *
        Exception in thread "main" java.lang.AbstractMethodError: org.apache.crimson.tree.XmlDocument.getXmlStandalone()Z
        	at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.setDocumentInfo(DOM2TO.java:373)
        	at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:127)
        	at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.parse(DOM2TO.java:94)
        	at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:662)
        	at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:708)
        	at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:313)
        	at org.docx4j.model.datastorage.CustomXmlDataStorageImpl.writeDocument(CustomXmlDataStorageImpl.java:174)
        *
        */
        DOMSource source = new DOMSource(doc);
        XmlUtils.getTransformerFactory().newTransformer().transform(source, new StreamResult(out));

        // Complete the entry
        out.closeEntry();
        log.info("success writing part: " + zipEntryName);

      } else {
        // Shouldn't happen, since ContentTypeManagerImpl should
        // return an instance of one of the above, or throw an
        // Exception.

        log.error("PROBLEM - No suitable part found for: " + zipEntryName);
      }

    } catch (Exception e) {
      e.printStackTrace();
      log.error("", e);
      throw new Docx4JException("Problem saving part " + zipEntryName, e);
    }
  }