The code to compress DOCX in Java

High-performance Java library to optimize DOCX content and size retaining the original quality and baseline data

This is a professional solution to compress DOCX using Java. Use our DOCX optimization API to create portable, robust and secure software in Java. Try it online for free!

View code snippet

Compress DOCX file in Java

Make large DOCX files smaller without loss of quality. Delete unnecessary and unused data. Shrink the size of a DOCX file in code. The Java library provides developers with an integrated API to optimize DOCX content.

Reduce the size of DOCX files using Java

Java library is a stand-alone solution and doesn't require any third party software installed.

Lossless compression and deep content optimization made easy with 'Aspose.Words for Java'. The following example demonstrates how to optimize the content of a DOCX file in Java:

Code example in Java to compress DOCX files
Upload a file you want to optimize/compress
Run code
Select the target format from the list
// Repository path: https://releases.aspose.com/java/repo/
// Maven, where 'ver' - Aspose.Words version number, for example, 24.4.
<dependency>
  <groupId>com.aspose</groupId>
  <artifactId>aspose-words</artifactId>
  <version>ver</version>
  <classifier>jdk17</classifier>
</dependency>
Copy
// Repository path: https://releases.aspose.com/java/repo/
// Gradle, where 'ver' - Aspose.Words version number, for example, 24.4.
compile(group: 'com.aspose', name: 'aspose-words', version: 'ver', classifier: 'jdk17')
Copy
// Repository path: https://releases.aspose.com/java/repo/
// Ivy, where 'ver' - Aspose.Words version number, for example, 24.4.
<dependency org="com.aspose" name="aspose-words" rev="ver">
 <artifact name="aspose-words" m:classifier="jdk17" ext="jar"/>
</dependency>
Copy
// Repository path: https://releases.aspose.com/java/repo/
// Sbt, where 'ver' - Aspose.Words version number, for example, 24.4.
libraryDependencies += "com.aspose" % "aspose-words" % "ver"
Copy
import com.aspose.words.*;

Document doc = new Document("Input.docx");
doc.cleanup();

NodeCollection nodes = doc.getChildNodes(NodeType.SHAPE, true);
for (Shape shape : (Iterable<Shape>) nodes)
{
    if (shape.isImage())
    {
        // It's up to developer to choose the library for image compression.
        BufferedImage image = ImageIO.read(shape.getImageData().toStream());

        // ...
        // Compress image and set it back to the shape.
        shape.getImageData().setImage("yourCompressedImage");
    }
}

OoxmlSaveOptions saveOptions = new OoxmlSaveOptions();
saveOptions.setCompressionLevel(CompressionLevel.MAXIMUM);

doc.save("Output.docx", saveOptions);
import com.aspose.words.*; Document doc = new Document("Input.docx"); doc.cleanup(); NodeCollection nodes = doc.getChildNodes(NodeType.SHAPE, true); for (Shape shape : (Iterable<Shape>) nodes) { if (shape.isImage()) { // It's up to developer to choose the library for image compression. BufferedImage image = ImageIO.read(shape.getImageData().toStream()); // ... // Compress image and set it back to the shape. shape.getImageData().setImage("yourCompressedImage"); } } OoxmlSaveOptions saveOptions = new PdfSaveOptions(); saveOptions.setCacheBackgroundGraphics(true); doc.save("Output.docx", saveOptions); import com.aspose.words.*; Document doc = new Document("Input.docx"); doc.cleanup(); NodeCollection nodes = doc.getChildNodes(NodeType.SHAPE, true); for (Shape shape : (Iterable<Shape>) nodes) { if (shape.isImage()) { // It's up to developer to choose the library for image compression. BufferedImage image = ImageIO.read(shape.getImageData().toStream()); // ... // Compress image and set it back to the shape. shape.getImageData().setImage("yourCompressedImage"); } } doc.save("Output.docx"); import com.aspose.words.*; Document doc = new Document("Input.docx"); doc.cleanup(); NodeCollection nodes = doc.getChildNodes(NodeType.SHAPE, true); for (Shape shape : (Iterable<Shape>) nodes) { if (shape.isImage()) { // It's up to developer to choose the library for image compression. BufferedImage image = ImageIO.read(shape.getImageData().toStream()); // ... // Compress image and set it back to the shape. shape.getImageData().setImage("yourCompressedImage"); } } OoxmlSaveOptions saveOptions = new OoxmlSaveOptions(); saveOptions.setCompressionLevel(CompressionLevel.MAXIMUM); doc.save("Output.docx", saveOptions); import com.aspose.words.*; Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Shape shape = builder.insertImage("Input.docx"); ImageSaveOptions saveOptions = new ImageSaveOptions(SaveFormat.DOCX); shape.getShapeRenderer().save("Output.docx", saveOptions);
Run code

How to compress DOCX in Java

  1. Install Aspose.Words for Java.
  2. Add a library reference (import the library) to your Java project.
  3. Open the source DOCX file in Java.
  4. Compress DOCX without losing quality.
  5. Call the Document.cleanup() method. The resulting code may vary depending on the output file format.
  6. Get the compressed result as a DOCX file.

Java library to optimize and compress DOCX files

We host our Java packages in Maven repositories. 'Aspose.Words for Java' is a common JAR file containing byte-code. Please follow the step-by-step instructions on how to install it to your Java developer environment.

System Requirements

Java SE 7 and more recent Java versions are supported. We also provide a separate package for Java SE 6 in case you are obliged to use this outdated JRE.

Our Java package is cross-platform and runs on all operating systems with JVM implementation, including Microsoft Windows, Linux, macOS, Android and iOS.

For information on optional package dependencies, such as JogAmp JOGL, Harfbuzz font engine, Java Advanced Imaging JAI, please refer to Product Documentation.

Other supported file format optimizations

You can optimize files in many other file formats:

5%

Subscribe to Aspose Product Updates

Get monthly newsletters and offers directly delivered to your mailbox.

© Aspose Pty Ltd 2001-2024. All Rights Reserved.