OpenXML4J Project
Office Open XML File Format library for Java
Getting started
According to the alpha status of OpenXML4J, this documentation may change in the future. For more information about Open XML or the Open Packaging Convention, you may read the ECMA White Paper. The following content assumes that you use Eclipse as your daily Java IDE, if you want to know more about Eclipse IDE, go to the official website. You must have at least Java 5 or later on your machine (Java 1.4 will be supported later), to download the latest JDK, go to Java Download page.
Your first example
In this example, we will first enable your Java project to use the OpenXML4J library, and then we'll write some examples to illustrate the use of the API.
To begin with this tutorial, you have to download the latest version or nightly build from Sourceforge.
1. Create a new Java project (File -> New -> Project... -> Java project, then follow the Wizard and be sure you have a compiler compliance level to Java 5)
2. Configure the classpath (inside the Wizard or with the project Properties) by adding an external Jar to the build path. Select the OpenXML4J jar you've previously downloaded, Dom4J (you can download it here) and Log4J (you can download it here) librairies and then click Finish.
3. Now that your project is configure to use OpenXML4J, let's play with some code ! First create a class named Demo1 with the class Wizard (File -> New -> Class).
4. Add the following import statements :
import org.openxml4j.opc.PackageAccess;
import org.openxml4j.opc.PackagePart;
import org.openxml4j.opc.Package;
5. Create a main method that trhows all potential exception (this just for demonstration purpose, else you MUST correctly catch all exceptions) :
public static void main(String[] args) throws Exception {
// Code to put here
}
6. We want to list all the parts contain in a package, to do this, first we open an existing package, then we iterate throw the package parts collection return by the getParts() method. Put the following code inside the main method
Package p = Package.open("Sample.docx", PackageAccess.READ);
for (PackagePart part : p.getParts())
System.out.println(part.getUri() + " ->
" + part.getContentType());
7. Copy this wordprocessing document (you can use all Open Packaging Convention based documents : WordprocessingML, SpreadsheetML, PresentationML and even XML Paper Specification) at the root of your project.
8. Run the program :
/_rels/.rels ->application/vnd.openxmlformats-package.relationships+xml
/docProps/app.xml ->application/xml
/docProps/core.xml ->application/xml
/word/_rels/document.xml.rels ->application/vnd.openxmlformats-
package.relationships+xml
/word/document.xml ->application/xml
/word/fontTable.xml ->application/xml
/word/media/image1.gif ->image/gif /word/settings.xml -
>application/xml /word/styles.xml ->application/xml
/word/theme/theme1.xml ->application/xml
/word/webSettings.xml ->application/xml
9. Congratulations, you've just run your first OpenXML4J project !
10. You want to go further ? Try this code to retrieve the document part of the package (replace the code in the main method with the following, and put import statements at the right place) :
...
import org.openxml4j.opc.PackageRelationship;
import org.openxml4j.opc.PackageRelationshipTypes;
...
// Open the package
Package p = Package.open("Sample.docx", PackageAccess.READ);
// Get documents core properties part relationship
PackageRelationship corePropertiesRelationship = p .getRelationshipsByType( PackageRelationshipTypes.CORE_DOCUMENT)
.getRelationship(0);
// Get core properties part from the relationship.
PackagePart coreDocument = p.getPart(corePropertiesRelationship);
// Retrieve and display the URI and the content type of the core
// properties part of the document.
System.out.println(coreDocument.getUri() + " -> " + coreDocument.getContentType());
11. Run the program and see the result (try with a SpreadsheetML and PresentationML to see the difference) :
/docProps/core.xml -> application/vnd.openxmlformats-package.core-properties+xml