How to view Word documents using Java

Word document formats are documents formats of Microsoft Word. It might contain text, images, excel diagrams, and also OLE (Object Linking and Embedding) objects. In this article, we will discuss how to convert Word to pdf and other formats in Java applications.

The following topics are covered below in brief:

Java API for rendering Word files

GroupDocs.Viewer for Java provides API to render Word document formats to PNG, PDF, JPEG, and HTML. Supported formats are DOC, DOCX, DOCM, DOT, DOTM, DOTX, RTF, TXT documents, and many others.

Follow one of the ways as described in the Installation section to install and reference GroupDocs.Viewer for Java.

Here is a sample document to demonstrate rendering results:

Source Word preview

How to render Word files into HTML, JPG, PNG, or PDF

Rendering to HTML with Embedded Resources in Java

To render your file to HTML file(s) with embedded resources do the following steps:

  • With Viewer class load your document.
  • With the forEmbeddedResources method create the HtmlViewOptions instance and type output file name.
  • Call view method to render your document to HTML, resources will be embedded in to file.
try (Viewer viewer = new Viewer("sample.doc"))
{
    HtmlViewOptions viewOptions = 
        HtmlViewOptions.forEmbeddedResources("page_{0}.html");
    viewer.view(viewOptions);
}

Rendering Word to HTML

Rendering to HTML with External Resources in Java

To render your file to HTML file(s) with external resources do the following steps:

  • With Viewer class load your document.

  • with forExternalResources method create HtmlViewOptions instance and type:

    • the output file name mask
    • external resources folder file path mask
    • URL for resources mask format
  • Call view method to render your document to HTML.

Resources will be placed in a separate folder.

Resources placed into separate folder

try (Viewer viewer = new Viewer("sample.doc"))
{
    HtmlViewOptions viewOptions = 
        HtmlViewOptions.forExternalResources("page_{0}.html", "page_{0}/resource_{0}_{1}", "page_{0}/resource_{0}_{1}");

    viewer.view(viewOptions);
}

Rendering Word to JPEG in Java

To render your file to JPEG file do the following steps:

  • With Viewer class load your document.
  • Сreate JpegViewOptions instance and type output file name.
  • Call view method to render your document to JPEG.
try (Viewer viewer = new Viewer("sample.doc"))
{
    JpgViewOptions viewOptions = new JpgViewOptions("output_{0}.jpg");
    viewer.view(viewOptions);
}

Rendering Word to JPEG

Rendering to PNG in Java

To render your file to PNG file do the following steps:

  • With Viewer class load your document.
  • Сreate PngViewOptions instance and type output file name.
  • Call view method to render your document to PNG.
try (Viewer viewer = new Viewer("sample.doc"))
{
    PngViewOptions viewOptions = new PngViewOptions("output_{0}.png");
    viewer.view(viewOptions);
}

Rendering Word to PNG

Rendering Word to PDF in Java

You can render your Word documents such as DOCX, RTF or DOC to pdf. To render your file to a PDF file do the following steps:

  • With Viewer class load your document.
  • Сreate PngViewOptions instance and type output file name.
  • Call view method to render your document to PDF.
try (Viewer viewer = new Viewer("sample.doc"))
{
    PdfViewOptions viewOptions = new PdfViewOptions("output.pdf");
    viewer.view(viewOptions);
}

Get a Free API License

In order to use the API without evaluation limitations, you can get a free temporary license.

Conclusion

In conclusion, I hope you now know how to view Word files in Java applications, in this article you have seen how to convert Word to PDF, how to convert Word to jpg, and other formats in your application. Also, you can use Online apps to view your files these applications are built with GroupDocs.Viewer.

You can learn how to use GroupDocs.Viewer in your applications with documentation and if you have any questions or issues you feel free to send these via our forum.

See also

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