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";
}

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