1. Products
  2.   Editor
  3.   Java
  4.   Docx4J
 
  

DOCX4J - Open Source Java SDK for working with DOC & DOCX Files

Free & Powerful Open Source Java API to create, edit and convert DOC & DOCX documents.

What is Docx4J API for Java?

Docx4J is an open-source Java API designed for manipulating Microsoft Word documents in the DOCX format. Built on top of the Open XML standard, Docx4J allows Java developers to create, modify, and generate Word documents programmatically without relying on Microsoft Office. With support for handling complex elements like tables, images, headers, footers, and more, it enables seamless document processing in a variety of use cases, including automated report generation, document conversions, and template-based document creation. The API integrates well with JAXB, providing an efficient approach to work with XML-based document structures.

DOCX4J for Java API - Key Features

Following are some of the main features of Docx4J API:

  • Document Creation and Modification: Docx4J allows developers to create new DOCX files from scratch, as well as modify existing ones by adding or changing text, images, tables, and other elements.
  • Open XML Format Support: Since DOCX is based on the Open XML standard, Docx4J provides a way to work directly with the underlying XML of Word documents, offering fine-grained control over document content.
  • Handling Complex Document Elements: The API supports various complex document elements, such as headers and footers, paragraphs, sections, lists, tables, images, hyperlinks, and footnotes.
  • Styles and Formatting: Docx4J enables developers to apply custom styles and formatting to text, paragraphs, and other elements within the document, including fonts, colors, and alignment.
  • Template-Based Document Generation: It supports the use of DOCX templates, allowing developers to create dynamic content by filling placeholders in pre-existing templates.
  • Document Conversion: The API offers capabilities to convert DOCX documents to other formats, such as PDF, HTML, and images (e.g., PNG), leveraging other libraries or formats.
  • Support for Digital Signatures: Docx4J allows embedding and validating digital signatures in DOCX documents, adding an extra layer of security and authentication.
  • JAXB Integration: Since Docx4J is built around the JAXB (Java Architecture for XML Binding), it provides a seamless method to marshal and unmarshal XML content, making it easier to manipulate document parts programmatically.
  • Manipulating Metadata: The API enables working with document metadata, such as author information, document properties, and revision history.
  • Merge and Split Documents: Docx4J includes features for merging multiple Word documents into a single one or splitting a document into separate parts based on sections or pages.
  • Track Changes: It supports handling documents with "track changes" enabled, giving you the ability to inspect or manage revisions.
GitHub

GitHub Stats

Name:
Language:
Stars:
Forks:
License:
Repository was last updated at

Getting Started with DocX4J for Java API

For getting started with Docx4J API, you need to install the API first in your Java project.

Installation

To install Docx4J, you need to add its dependency to your project using Maven:

<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-JAXB-Internal</artifactId>
<version>8.0.0</version>
</dependency>

<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-JAXB-ReferenceImpl</artifactId>
<version>8.0.0</version>
</dependency>

<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-JAXB-MOXy</artifactId>
<version>8.0.0</version>
</dependency>

Code Examples for Docx4J API for Java

DocX is a powerful API that provides rich features for working with Document features. You can find C# examples for creating Word document, update an exisitng DOCX file, Insert Header and Footer in the document and work with images in this section.

How to Create a blank DOCX file using DocX4J API for Java?

You can create a blank DOCX document using the DocX4J API. It can then be saved to disc as DOCX file in the Microsoft Word's 2007+ format as shown in the following code sample:

How to Open an Existing DOCX File in Java and Modify it in Java?

You can read an existing DOCX file using DocX4J API and update its contents. Once you make your changes, you can save the document back to the disc similar to the "Save As" option available in Microsoft Word.

How to Add Headers and Footers to Word Document in Java?

DocX4J API lets you add header and footer to a Word document from within your Java application. You can add header to the Word file, modify its contents, add footer and update its contents as shown in code sample below.

How to Insert Image in a Word DOCX File in Java?

If you want to insert an image in a Word DOCX file using Java, the DocX4J API lets Java developers to programmatically manipulate images embedded in Word DOCX documents. The following code sample shows how to insert an image in a DOCX file in Java.

Conclusion

Docx4J is a powerful and versatile Java API that allows developers to programmatically create, manipulate, and manage DOCX documents. Its deep integration with the Open XML format enables fine-grained control over document elements such as text, tables, images, headers, footers, and metadata. With built-in support for document conversion, styling, digital signatures, and handling complex elements, Docx4J provides a comprehensive solution for a wide range of document-related tasks. Whether you're generating dynamic reports, modifying existing Word documents, or converting formats, Docx4J offers the flexibility and functionality needed to efficiently work with Word documents in Java applications.

Similar Products