OpenXML4J Project

Office Open XML File Format library for Java

Samples (Open Packaging Convention)

Here are some OpenXML4J samples concerning the use of the Open Packaging Convention component (low level structure of Open XML documents) provided by OpenXML4J.

Create a (very) basic wordprocessing document :

This sample demonstrates how to create a wordproccessing document containing only a 'Hello Open XML !' paragraph :

  public static void main(String[] argsthrows Exception {
    File outputDocument = new File("your path/sample.docx");

    // Create a package
    Package pkg = Package.create(outputDocument);

    /* Main part */

    PackagePartName corePartName = PackagingURIHelper
        .createPartName("/word/document.xml");

    // Create main part relationship
    pkg.addRelationship(corePartName, TargetMode.INTERNAL,
        PackageRelationshipTypes.CORE_DOCUMENT, "rId1");

    // Create main document part
    PackagePart corePart = pkg
        .createPart(
            corePartName,
            "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml");

    // Create main document part content
    Document doc = DocumentHelper.createDocument();
    Namespace nsWordprocessinML = new Namespace("w",
        "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
    Element elDocument = doc.addElement(new QName("document",
        nsWordprocessinML));
    Element elBody = elDocument.addElement(new QName("body",
        nsWordprocessinML));
    Element elParagraph = elBody.addElement(new QName("p",
        nsWordprocessinML));
    Element elRun = elParagraph
        .addElement(new QName("r", nsWordprocessinML));
    Element elText = elRun.addElement(new QName("t", nsWordprocessinML));
    elText.setText("Hello Open XML !");

    // Save the XML structure into the part
    StreamHelper.saveXmlInStream(doc, corePart.getOutputStream());

    // Save package
    pkg.close();
  }

Extract some core properties

In this sample, you'll extract some document core properties like the creator, the title, ...

  public static void main(String[] argsthrows Exception {
    try {
      // Open the package
      Package p = Package.open(args[0], PackageAccess.READ);

      // Gets the core properties
      PackageProperties props = p.getPackageProperties();
      DemoCore.getLogger().info(
          "Title: " + props.getTitleProperty().getValue());
      DemoCore.getLogger().info(
          "Creator: " + props.getCreatorProperty().getValue());
      DemoCore.getLogger().info(
          "Creation date: " + props.getCreatedProperty().getValue());
      DemoCore.getLogger().info(
          "Status: " + props.getContentStatusProperty().getValue());

      p.revert();
    catch (OpenXML4JException e) {
      ...
    }
  }

Set document core properties

This sample sets some document core properties like the author and the description (but much more properties are availbale via the PackageProperties object) :

public static void main(String[] args) {
    // Destination file
    File destFile = new File("sample_output.docx");

    // Open package
    Package pkg;
    try {
      pkg = Package.open(demoCore.getTestRootPath() "sample.docx",
          PackageAccess.READ_WRITE);

      PackageProperties coreProps = pkg.getPackageProperties();
      coreProps.setCreatorProperty("Test Creator");
      coreProps.setDescriptionProperty("A new description !");
      coreProps
          .setTitleProperty("Demo : change document core properties");

      // Save document
      pkg.save(destFile);
    catch (Exception e) {
      e.printStackTrace();
      return;
    }
  }

Retrieve document extended properties

This sample retrieves extended properties by extracting the content of the extended properties part and parsing the XML :

  public static void main(String[] args) {
    // Open the package
    Package p;
    try {
      p = Package.open("sample.docx",
          PackageAccess.READ);

      // Retrieves extended properties part relationship
      PackageRelationship extendedPropertiesRelationship = p
          .getRelationshipsByType(
              PackageRelationshipTypes.EXTENDED_PROPERTIES)
          .getRelationship(0);

      // Retrieves extended properties part
      PackagePart extPropsPart = p
          .getPart(extendedPropertiesRelationship);
      System.out.println(extPropsPart.getPartName() " -> "
          + extPropsPart.getContentType());

      // Get the input stream from the extended properties part
      InputStream inStream = extPropsPart.getInputStream();

      // Parse the XML content
      SAXReader xmlReader = new SAXReader();
      Document extPropsDoc = xmlReader.read(inStream);

      // Print some values
      System.out.println("Document generated with "
          + extPropsDoc.getRootElement().element("Application")
              .getStringValue()
          " version "
          + extPropsDoc.getRootElement().element("AppVersion")
              .getStringValue());

      // Print more values
      System.out.println("The document have "
          + extPropsDoc.getRootElement().element("Words")
              .getStringValue()
          " words  "
          + extPropsDoc.getRootElement().element("Characters")
              .getStringValue()
          " charaters, and "
          + extPropsDoc.getRootElement().element("Lines")
              .getStringValue() " lines");

      inStream.close();
    catch (Exception ioe) {
      System.err
          .println("Fail to extract application properties of the document ! :(");
    }
  }

Get core part

This sample retrieves the core document part in a package :

  public PackagePart getCorePartUri(String fileName) {
    try {
      // Open the package
      Package p = Package.open(fileName, PackageAccess.READ);

      // Gets the core part relationship
      PackageRelationship coreDocRelationship = p.getRelationshipsByType(
          PackageRelationshipTypes.CORE_DOCUMENT).getRelationship(0);

      // Get core part
      PackagePart corePart = p.getPart(coreDocRelationship);
      return corePart;
    catch (OpenXML4JException e) {
      DemoCore.getLogger().debug(e.getMessage());
      return null;
    }
  }

  public static void main(String[] args) {
    // Retrieves core part
    PackagePart corePart = demo.getCorePartUri("Sample.docx");
    if (corePart != null)
      System.out.println(corePart.getPartName() " -> "
          + corePart.getContentType());
    else
      System.out.println("The specified file is not valid !");
  }

Modify existing content (wordprocessing document)

This sample retrieves the main content of a wordprocessing document, then modify its XML structure and finally save back the content in the part. See how to append a paragraph to WordprocessingML based document:

public static void main(String[] argsthrows Exception {
    // Open the package
    Package pkg = Package.open("sample.docx",
        PackageAccess.READ_WRITE);

    // Get documents core document part relationship
    PackageRelationship coreDocumentRelationship = pkg
        .getRelationshipsByType(PackageRelationshipTypes.CORE_DOCUMENT)
        .getRelationship(0);

    // Get core document part from the relationship.
    PackagePart coreDocumentPart = pkg.getPart(coreDocumentRelationship);

    InputStream inStream = coreDocumentPart.getInputStream();
    SAXReader docReader = new SAXReader();
    Document doc = docReader.read(inStream);

    Namespace namespaceWordProcessingML = new Namespace("w",
        "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
    Element bodyElement = doc.getRootElement().element(
        new QName("body", namespaceWordProcessingML));

    // Retrieves paragraph childs from body element
    List paragraphs = bodyElement.content();

    // Build a new paragraph element
    Element paragraph = DocumentHelper.createElement(new QName("p",
        namespaceWordProcessingML));
    Element run = paragraph.addElement(new QName("r",
        namespaceWordProcessingML));
    Element text = run
        .addElement(new QName("t", namespaceWordProcessingML));
    text.setText("New paragraph added with OpenXML4J !");

    // Add the newly created paragraph at the last position of paragraph
    // elements, just before the w:sectPr element
    paragraphs.add(paragraphs.size() 1, paragraph);

    // Save back the content into the part
    StreamHelper.saveXmlInStream(doc, coreDocumentPart.getOutputStream());

    pkg.save(new File("sample_output.docx"));
  }