Generate document pages preview

GroupDocs.Merger provides PreviewOptions class to specify different options to manage preview of document pages.

Here are the steps to generate document preview with GroupDocs.Merger:

  • Create new instance of Merger class and pass source document path as a constructor parameter.
  • Instantiate the PreviewOptions object with:
    • delegate for each page output stream creation (see event handler CreatePageStream); 
    • image preview format - PNG / JPG / BMP;
    • page numbers to process;
    • custom size of preview images (if needed). 
  • Call generatePreview method of Merger class instance and pass PreviewOptions to it.

CreatePageStream delegate implementation

GroupDocs.Merger expects CreatePageStream delegate to obtain each page stream for image preview generation process

private static OutputStream createPageStream(int pageNumber)
{
    try{
        String imageFilePath = new File("GeneratePreviewFolder", "image-" + pageNumber + ".jpg").getPath();
        return new FileOutputStream(imageFilePath);
    }catch (Exception e){
        throw new GroupDocsException(e.getMessage());
    }
}

ReleasePageStream delegate implementation

private static void releasePageStream(int pageNumber, OutputStream pageStream) 
{
    try {
        pageStream.close();
        String imageFilePath = new File("GeneratePreviewFolder", "image-" + pageNumber + ".jpg").getPath();
        System.out.print("Image file "+imageFilePath+" is ready for preview.");
    }catch (Exception e){
        throw new GroupDocsException(e.getMessage());
    }
}

Generate document preview from file on local disk

public static void getPreview()
{   
 	string filePath = "c:\sample.xlsx";
    Merger merger = new Merger(filePath);

	IPreviewOptions previewOption = new PreviewOptions(new CreatePageStream() {
    	@Override
    	public OutputStream invoke(int pageNumber) {
        	return createPageStream(pageNumber);
    	}
	}, PreviewMode.JPEG);
	merger.generatePreview(previewOption);
}
 
private static OutputStream createPageStream(int pageNumber)
{
    try{
        String imageFilePath = new File("GeneratePreviewFolder", "image-" + pageNumber + ".jpg").getPath();
        return new FileOutputStream(imageFilePath);
    }catch (Exception e){
        throw new GroupDocsException(e.getMessage());
    }

}

Generate document preview from stream with custom stream releasing delegate

public static void getPreview()
{
    string filePath = "c:\sample.xlsx";
	Merger merger = new Merger(filePath);

	IPreviewOptions previewOption = new PreviewOptions(new CreatePageStream() {
 	   @Override
	    public OutputStream invoke(int pageNumber) {
 	       return createPageStream(pageNumber);
 	   }
	}, new ReleasePageStream() {
	    @Override
  	  public void invoke(int pageNumber, OutputStream pageStream) {
  	      releasePageStream(pageNumber, pageStream);
  	  }
	}, PreviewMode.JPEG);
	merger.generatePreview(previewOption);
}
 

private static OutputStream createPageStream(int pageNumber)
{
    try{
        String imageFilePath = new File("GeneratePreviewFolder", "image-" + pageNumber + ".jpg").getPath();
        return new FileOutputStream(imageFilePath);
    }catch (Exception e){
        throw new GroupDocsException(e.getMessage());
    }

}

private static void releasePageStream(int pageNumber, OutputStream pageStream)
{
    try {
        pageStream.close();
        String imageFilePath = new File("GeneratePreviewFolder", "image-" + pageNumber + ".jpg").getPath();
        System.out.print("Image file "+imageFilePath+" is ready for preview.");
    }catch (Exception e){
        throw new GroupDocsException(e.getMessage());
    }
}
private static String getImagePath(int pageNumber)
{
    return Constants.OutputPath +"GeneratePreview\\image-"+pageNumber+".jpg";
}

More resources

Advanced Usage Topics 

To learn more about document merging features, please refer the advanced usage section.

GitHub Examples 

You may easily run the code above and see the feature in action in our GitHub examples:

Free Online App 

Along with full featured Java library we provide simple, but powerful free Apps. You are welcome to merge your documents with our free online GroupDocs Merger App.