Generating page preview for WordProcessing document

On this page

Starting from the version 23.9 the GroupDocs.Editor for Java allows to generate a preview for arbitrary page in the loaded WordProcessing document, like DOC, DOCX, DOCM, RTF, ODT etc. The preview is generated in the SVG format, which is a vector graphics format and is supported in the numerous image viewers and also by any modern browser.

Using this feature the users can view and inspect any document of the imported WordProcessing document without actually editing it. This preview cannot be edited by the GroupDocs.Editor itself, it can be only obtained in SVG format and saved as usual - to the byte stream or file.

This feature is working regardless of the licensing mode of the GroupDocs.Editor for Java: it works the same for both trial and licensed mode, there are no trial limitations for this feature. While generating the pages preview, the GroupDocs.Editor doesn’t write off the consumed bytes or credits.

With this feature was introduced, the GroupDocs.Editor for Java finally supports generating a preview for all three major office format families: WordProcessing, Spreadsheet and Presentation.

For generating the pages preview for a particular WordProcessing document the user must perform the next steps:

The code sample below shows opening an unprotected WordProcessing DOCX file, obtaining a number of all pages inside this DOCX file, and then generating the previews for every page in a loop. Then these previews are saved to the disk. Please note that the GroupDocs.Editor is working in the trial mode, but page preview is generated for all the pages, and no trial copyright marks are present on these previews.

// Obtain a valid full path to the WordProcessing file
String inputPath = "SampleDoc.docx";

// Load WordProcessing file to the Editor constructor
Editor editor = new Editor(inputPath);
{
	// Get document info for this file
	IDocumentInfo infoUncasted = editor.getDocumentInfo(null);

	//Make sure it is really a WordProcessing of DOCX format
	Assert.assertEquals(WordProcessingFormats.Docx, infoUncasted.getFormat());

	// Cast this document info to the WordProcessingDocumentInfo type
	WordProcessingDocumentInfo infoWordProcessing = (WordProcessingDocumentInfo)infoUncasted;

	// Get the number of all pages
	int pagesCount = infoWordProcessing.getPageCount();	

	//Iterate through all pages and generate the preview on every iteration
	for (int pageIndex = 0; pageIndex < pagesCount; pageIndex++)
	{
		// Generate one preview as SVG image by page index
		SvgImage oneSvgPreview = infoWordProcessing.generatePreview(pageIndex);

		// Save SVG preview to the file
		oneSvgPreview.save("outputFolder" + oneSvgPreview.getFilenameWithExtension());
	}
}

All WordProcessing formats are able to store the raster images. So when the specific page of some loaded document has one or more raster images, and for this page the SVG preview is generated, this/these raster image(s) will be embedded inside SVG in base64 format using the data URI scheme.

Concluding, the new page preview feature is by its essence a new public method in the existing WordProcessingDocumentInfo struct, that obtains a page index and returns an instance of SvgImage class. If the end-user needs to obtain a preview of the page in a raster format, but not in the vector, he can use the saveToPng() method in the SvgImage class — this method converts the SVG content of the current SvgImage to the PNG format and saves it to the specified java.io.OutputStream.

On this page