Free Support Forum - groupdocs.com

Migrating to Document Viewer API for Java 20.1

Hello,
I am migrating from 19.11 version to 20.1. As pointed here all classes I have been using are now deprecated. I would like to know/confirm the following:

  • I was using a single ViewerHtmlHandler object for all rendering tasks in all threads, I would need to create a new Viewer instance everytime instead, right?
  • Do these methods clearTempFiles and clearCache still exist?
  • Does this exception FileTypeNotSupportedException or any equivalent exist?

Any help is appreciated :slight_smile:

Jorge

1 Like

@jorgeeflorez,

We are investigating these points. Your investigation ticket ID is VIEWERJAVA-2174. As there’s any further update, you’ll be notified.

@jorgeeflorez,

Yes, from now you need one instance of Viewer per file. Please don’t forget to call close() method on Viewer object to release the resources.

  • clearTempFiles - this method will be added in some upcoming releases of the API, we’ll notify you as the release is available
  • clearCache - this method should be implemented as per your consumption. To use caching feature you can start with File Cache and extend it with clearCache method that will handle cache removal

API will throw generic GroupDocsViewer exception. All the supported file types can be listed with FileType.getSupportedFileTypes() method and you can predict if the file is supported or not (of course this is true in case you know what is you file type).
Please let us know if there is anything unclear.

@atirtahir3, thank you for your reply.

should I expect many temporal files or large space consumption in System.getProperty(“java.io.tmpdir”) directory?

1 Like

@atirtahir3,
in previous version I set the encoding to the different options object.

    HtmlOptions options = new HtmlOptions();
	options.setEmbedResources(true);
	options.getCellsOptions().setEncoding(encoding);
    options.getWordsOptions().setEncoding(encoding);
    options.getEmailOptions().setEncoding(encoding);

is it still done now?

after calling view(), how can I get the generated html? do I have to read the generated file? does PageHtml no longer exist?

1 Like

@jorgeeflorez,

No, you can call ViewerHtmlHanlder.clearTempFiles this method is still available and works as before. So, calling this method from time to time will reduce space consumption.

You can still set the encoding. Settings has been moved to LoadOptions class. See the example below:

LoadOptions loadOptions = new LoadOptions();    
loadOptions.setCharset(Charset.forName("UTF-8"));

Viewer viewer = new Viewer("sample.csv", loadOptions);
viewer.view(HtmlViewOptions.forEmbeddedResources());     
viewer.close();

It basically depends on your use-case. By default, we’re saving pages to disk, but you can also save pages into a stream as shown below:

Viewer viewer = new Viewer("sample.docx");
viewer.view(HtmlViewOptions.forEmbeddedResources(new PageStreamFactory() {

    @Override
    public OutputStream createPageStream(int pageNumber) {
        return new ByteArrayOutputStream();
    }

    @Override
    public void closePageStream(int pageNumber, OutputStream pageStream) {
        ByteArrayOutputStream outputStream = (ByteArrayOutputStream)pageStream;
        byte[] bytes = outputStream.toByteArray();

        String pageHtml = new String(bytes);
        System.out.println("Page " + pageNumber);
        System.out.println("HTML " + pageHtml);
    }

}));     
viewer.close();

Yes, PageHtml class no longer exists. It was just a wrapper around output result, so it’s quite simple to create one based on the above example.