This repository contains GroupDocs.Viewer Cloud SDK for Java source code. This SDK allows you to work with GroupDocs.Viewer Cloud REST APIs in your Java applications. This allows rendering any type of documents - Word, Excel, Presentation, Cad, Email, etc, to HTML, image or PDF format with the flexibility to render the whole document or custom range of pages.
- Java SE Development Kit 8
Add following repository and dependency to your project's POM
<repository>
<id>groupdocs-artifact-repository</id>
<name>GroupDocs Artifact Repository</name>
<url>https://repository.groupdocs.cloud/repo</url>
</repository><dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-viewer-cloud</artifactId>
<version>26.6</version>
<scope>compile</scope>
</dependency>Add following repository and dependency to your build.gradle:
repositories{maven{url"https://repository.groupdocs.cloud/repo/"}}
...
dependencies{
...
implementation'com.groupdocs:groupdocs-viewer-cloud:26.6'}- Please follow the installation instruction
- Get your AppSID and AppKey at Dashboard and use them in your code
- Build and execute
- Explore more samples at GitHub
Below is an example demonstrating how to preview document using GroupDocs.Viewer Cloud SDK for Java:
importcom.groupdocs.cloud.viewer.api.ViewApi;
importcom.groupdocs.cloud.viewer.client.Configuration;
importcom.groupdocs.cloud.viewer.model.requests.ConvertAndDownloadRequest;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.InputStream;
importjava.io.OutputStream;
importjava.io.FileOutputStream;
publicclassExample {
publicstaticvoidmain(String[] args) {
// For complete examples and data files, please go to https://github.com/groupdocs-viewer-cloud/groupdocs-viewer-cloud-java-samplesStringmyClientId = ""; // Get Client Id from https://dashboard.groupdocs.cloudStringmyClientSecret = ""; // Get Client Secret from https://dashboard.groupdocs.cloudConfigurationconfiguration = newConfiguration(myClientId, myClientSecret);
ViewApiviewApi = newViewApi(configuration);
Stringformat = "jpg";
try (InputStreamfileStream = newFileInputStream("myfile.docx")) {
ConvertAndDownloadRequestrequest = newConvertAndDownloadRequest(format, fileStream);
InputStreamresult = viewApi.convertAndDownload(request);
// Save the resulting stream (a *.jpg file) for your purposetry (OutputStreamoutStream = newFileOutputStream("output.jpg")) {
byte[] buffer = newbyte[8192];
intbytesRead;
while ((bytesRead = result.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
}
result.close();
} catch (Exceptione) {
System.err.println("Failed to convert and download the document");
e.printStackTrace();
}
}
}And here is an example demonstrating how to upload the document, render it, and download the result using GroupDocs.Viewer Cloud SDK for Java:
importcom.groupdocs.cloud.viewer.api.ViewApi;
importcom.groupdocs.cloud.viewer.api.FileApi;
importcom.groupdocs.cloud.viewer.client.Configuration;
importcom.groupdocs.cloud.viewer.model.*;
importcom.groupdocs.cloud.viewer.model.requests.*;
importjava.io.FileInputStream;
importjava.io.InputStream;
importjava.io.OutputStream;
importjava.io.FileOutputStream;
publicclassExample {
publicstaticvoidmain(String[] args) {
// For complete examples and data files, please go to https://github.com/groupdocs-viewer-cloud/groupdocs-viewer-cloud-java-samplesStringmyClientId = ""; // Get Client Id from https://dashboard.groupdocs.cloudStringmyClientSecret = ""; // Get Client Secret from https://dashboard.groupdocs.cloudConfigurationconfiguration = newConfiguration(myClientId, myClientSecret);
try {
// Upload a file to cloud storageFileApifileApi = newFileApi(configuration);
try (InputStreamuploadStream = newFileInputStream("myfile.docx")) {
UploadFileRequestuploadRequest = newUploadFileRequest("myfile.docx", uploadStream, null);
fileApi.uploadFile(uploadRequest);
}
// Render it to HTMLViewApiviewApi = newViewApi(configuration);
FileInfofileInfo = newFileInfo();
fileInfo.setFilePath("myfile.docx");
ViewOptionsviewOptions = newViewOptions();
viewOptions.setFileInfo(fileInfo);
viewOptions.setViewFormat(ViewOptions.ViewFormatEnum.HTML);
viewOptions.setOutputPath("myfile.html");
CreateViewRequestviewRequest = newCreateViewRequest(viewOptions);
viewApi.createView(viewRequest);
// Download the resultDownloadFileRequestdownloadRequest = newDownloadFileRequest("myfile.html", null, null);
InputStreamresult = fileApi.downloadFile(downloadRequest);
// Use resulting stream (a *.html file in this example) for your purposetry (OutputStreamoutStream = newFileOutputStream("output.html")) {
byte[] buffer = newbyte[8192];
intbytesRead;
while ((bytesRead = result.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
}
result.close();
} catch (Exceptione) {
System.err.println("Failed to upload, render, or download the document");
e.printStackTrace();
}
}
}Building the API client library requires Maven to be installed. Refer to the official documentation for more information.
At first generate the JAR by executing following command in "/src" working directory:
mvn package -D maven.test.skip=trueThen manually install the following JARs:
- target/groupdocs-viewer-cloud-26.6.jar
- target/lib/*.jar
All GroupDocs.Viewer Cloud SDKs are licensed under MIT License.
Your feedback is very important to us. Please feel free to contact us using our Support Forums.