1. Products
  2.   Word Processing
  3.   Java
  4.   Aspose.Words for Java

Aspose.Words for Java

 
 

Java API to Process Microsoft Word Documents

Cross-platform Java library to create, modify, convert, render, and print Word processing documents without Microsoft Word, or Office Automation

Aspose.Words for Java is a very powerful Java library that helps software professionals to create applications for handling Word document processing tasks. That library can generate, edit, convert, render, and print Word processing documents without any third-party software like Microsoft Word or Office Automation. The library is designed to perform impressively on both the server as well as the client side. The library can be used on almost all Java development environments and deployment platforms.

The library has included support for some of the leading documents file formats such as DOC, DOCX, RTF, DOT, DOTX, DOTM, PDF, PDF/A, XPS, ODT, OTT, WordML, HTML, MHTML, TIFF, JPEG, PNG, BMP, SVG, EMF, GIF and many more. . The library has included support for over 35 popular file formats. The library can very consistently and efficiently convert documents from one popular format to another with a high degree of precision and accuracy.

Aspose.Words for Java is a very features-rich document processing API that has incorporated support for several advanced features such as rendering complete documents or some particular page, designing reports in Microsoft Word, Mail merge fields in reports, managing fonts, inserting and managing images, printing documents programmatically, printing multiple pages on a sheet, insert text to documents, 3D Effects Rendering, create, and modify paragraphs, Join and split documents. Copy and move documents and many more. It can be used to develop applications for a wide range of operating systems, such as Windows, Linux, Mac OS, Android, and various platforms.

Previous Next

Getting Started with Aspose.Words for Java

The recommend way to install Aspose.Words for Java is via Maven repository. You can easily use Aspose.Words for Java API directly in your Maven Projects with simple configurations:.

Aspose.Words for Java Maven Dependency

 //Define the Aspose.Words for Java API dependency in your pom.xml as follows
<dependencies>
	<dependency>
	<groupId>com.aspose</groupId>
	<artifactId>aspose-words</artifactId>
	<version>22.11</version>
	</dependency>

	<dependency>
	<groupId>com.aspose</groupId>
	<artifactId>aspose-words</artifactId>
	<version>22.11</version>
	<classifier>javadoc</classifier>
	</dependency>
</dependencies>
You can download the directly from Aspose.Words Release page

Document Creation & Loading via Java API

Aspose.Words for Java allows software developers to programmatically create a new blank document or add document contents inside their own Java applications. To create a blank word document you just need to call the Document constructor without a parameter. It is very easy to load an existing document, just need to pass the document name or the stream into one of the Document constructors. The library recognizes the format of the loaded file by its extension. Once the document is created you can easily add text, images, shapes, fonts, define styles and formatting, set page size, insert tables and charts, add headers/footers, and so on.

Create Word Document via Java API

 
// The path to the documents directory.
String dataDir = Utils.getDataDir(CreateDocument.class);

// Load the document.
Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("hello world");

doc.save(dataDir + "output.docx");

Word Document Rendering via Java API

Aspose.Words for Java library gives software developers the power to render Word documents or part of the document inside their own Java applications. The library has included very powerful rendering features, such as rendering a document to fixed-layout formats, exporting document or selected pages into PDF, XPS, HTML, XAML, PostScript, and PCL formats, rendering a document into a multi-page TIFF document, converting any page into a raster image ( BMP, PNG, JPEG), document page conversion into SVG image, and many more.

Save a Document to JPEG Format via Java API

 
Document doc = new Document(dataDir + "Rendering.doc");
// Save as a JPEG image file with default options
doc.save(dataDir + "Rendering.JpegDefaultOptions.jpg");

// Save document to stream as a JPEG with default options
OutputStream docStream = new FileOutputStream(dataDir + "Rendering.JpegOutStream.jpg");
doc.save(docStream, SaveFormat.JPEG);

// Save document to a JPEG image with specified options.
// Render the third page only and set the JPEG quality to 80%
// In this case we need to pass the desired SaveFormat to the ImageSaveOptions
// constructor
// to signal what type of image to save as.
ImageSaveOptions imageOptions = new ImageSaveOptions(SaveFormat.JPEG);
imageOptions.setPageSet(new PageSet(2, 1));
imageOptions.setJpegQuality(80);
doc.save(dataDir + "Rendering.JpegCustomOptions.jpg", imageOptions);

Join & Split Word Documents via Java Library

CIt is open needed to join various documents into a single document or split a large file into smaller ones. Aspose.Words for Java library has provided various useful features for combining and splitting documents using Java library. It allows developers to insert the content of another document to a newly created document or append a document only at the end of another document. The library has provided various ways for inserting a document to other files such as inserting a file during mail merge operation, inserting a document at a bookmark, adding a document to the end of another one, importing and inserting nodes manually, and so on. Same like joining the library also included several functions for splitting documents such as splitting a document by headings, splitting a document by sections, splitting a document page by page, splitting a multi-page document page by page, and so on.

Split a Document Page by Page via C# API

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document(dataDir + "TestFile (Split).docx");

int pageCount = doc.getPageCount();

// Save each page as a separate document.
for (int page = 0; page <= pageCount; page++)
{
	Document extractedPage = doc.extractPages(page, 1);
	extractedPage.save(dataDir + "SplitDocumentPageByPageOut_" + (page + 1) + ".docx");
}

Print Word Documents inside Java Apps

Aspose.Words for Java enables software developers to print various types of documents inside their own Java applications. The library has provided support for print preview dialog to visually examine how the document will appear and select a needed print option. Using the MultipagePrintDocument class programmers can print multiple pages of a document on a single sheet of paper.  

Printing Multiple Pages on One Sheet via Java API


Document doc = new Document(dataDir + "TestFile.doc");

// Create a print job to print our document with.
PrinterJob pj = PrinterJob.getPrinterJob();

// Initialize an attribute set with the number of pages in the document.
PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
attributes.add(new PageRanges(1, doc.getPageCount()));

// Pass the printer settings along with the other parameters to the print document.
MultipagePrintDocument awPrintDoc = new MultipagePrintDocument(doc, 4, true, attributes);

// Pass the document to be printed using the print job.
pj.setPrintable(awPrintDoc);

pj.print();
 English