|
1 | 1 | # GroupDocs.Viewer Cloud SDK for Java |
2 | 2 |
|
3 | | -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. |
| 3 | +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. |
4 | 4 |
|
5 | 5 | ## Requirements |
6 | 6 |
|
@@ -54,33 +54,111 @@ dependencies { |
54 | 54 | * Build and execute |
55 | 55 | * Explore more samples at [GitHub](https://github.com/groupdocs-viewer-cloud/groupdocs-viewer-cloud-java-samples) |
56 | 56 |
|
57 | | -Example: |
| 57 | +Below is an example demonstrating how to preview document using GroupDocs.Viewer Cloud SDK for Java: |
58 | 58 |
|
59 | 59 | ```java |
60 | | -import com.groupdocs.cloud.viewer.client.*; |
| 60 | +import com.groupdocs.cloud.viewer.api.ViewApi; |
| 61 | +import com.groupdocs.cloud.viewer.client.Configuration; |
| 62 | +import com.groupdocs.cloud.viewer.model.requests.ConvertAndDownloadRequest; |
| 63 | + |
| 64 | +import java.io.File; |
| 65 | +import java.io.FileInputStream; |
| 66 | +import java.io.InputStream; |
| 67 | +import java.io.OutputStream; |
| 68 | +import java.io.FileOutputStream; |
| 69 | + |
| 70 | +public class Example { |
| 71 | + |
| 72 | + public static void main(String[] args) { |
| 73 | + // For complete examples and data files, please go to https://github.com/groupdocs-viewer-cloud/groupdocs-viewer-cloud-java-samples |
| 74 | + String myClientId = ""; // Get Client Id from https://dashboard.groupdocs.cloud |
| 75 | + String myClientSecret = ""; // Get Client Secret from https://dashboard.groupdocs.cloud |
| 76 | + |
| 77 | + Configuration configuration = new Configuration(myClientId, myClientSecret); |
| 78 | + ViewApi viewApi = new ViewApi(configuration); |
| 79 | + |
| 80 | + String format = "jpg"; |
| 81 | + try (InputStream fileStream = new FileInputStream("myfile.docx")) { |
| 82 | + ConvertAndDownloadRequest request = new ConvertAndDownloadRequest(format, fileStream); |
| 83 | + InputStream result = viewApi.convertAndDownload(request); |
| 84 | + |
| 85 | + // Save the resulting stream (a *.jpg file) for your purpose |
| 86 | + try (OutputStream outStream = new FileOutputStream("output.jpg")) { |
| 87 | + byte[] buffer = new byte[8192]; |
| 88 | + int bytesRead; |
| 89 | + while ((bytesRead = result.read(buffer)) != -1) { |
| 90 | + outStream.write(buffer, 0, bytesRead); |
| 91 | + } |
| 92 | + } |
| 93 | + result.close(); |
| 94 | + } catch (Exception e) { |
| 95 | + System.err.println("Failed to convert and download the document"); |
| 96 | + e.printStackTrace(); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +And here is an example demonstrating how to upload the document, render it, and download the result using GroupDocs.Viewer Cloud SDK for Java: |
| 103 | + |
| 104 | +```java |
| 105 | +import com.groupdocs.cloud.viewer.api.ViewApi; |
| 106 | +import com.groupdocs.cloud.viewer.api.FileApi; |
| 107 | +import com.groupdocs.cloud.viewer.client.Configuration; |
61 | 108 | import com.groupdocs.cloud.viewer.model.*; |
62 | | -import com.groupdocs.cloud.viewer.api.InfoApi; |
| 109 | +import com.groupdocs.cloud.viewer.model.requests.*; |
63 | 110 |
|
64 | | -import java.util.*; |
| 111 | +import java.io.FileInputStream; |
| 112 | +import java.io.InputStream; |
| 113 | +import java.io.OutputStream; |
| 114 | +import java.io.FileOutputStream; |
65 | 115 |
|
66 | | -public class ApiExample { |
| 116 | +public class Example { |
67 | 117 |
|
68 | 118 | public static void main(String[] args) { |
69 | | - //TODO: Get your AppSID and AppKey at https://dashboard.groupdocs.cloud (free registration is required). |
70 | | - String appSid = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"; |
71 | | - String appKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; |
| 119 | + // For complete examples and data files, please go to https://github.com/groupdocs-viewer-cloud/groupdocs-viewer-cloud-java-samples |
| 120 | + String myClientId = ""; // Get Client Id from https://dashboard.groupdocs.cloud |
| 121 | + String myClientSecret = ""; // Get Client Secret from https://dashboard.groupdocs.cloud |
72 | 122 |
|
73 | | - Configuration configuration = new Configuration(appSid, appKey); |
74 | | - |
75 | | - InfoApi infoApi = new InfoApi(configuration); |
| 123 | + Configuration configuration = new Configuration(myClientId, myClientSecret); |
76 | 124 |
|
77 | 125 | try { |
78 | | - FormatsResult response = infoApi.getSupportedFileFormats(); |
79 | | - for (Format format : response.getFormats()) { |
80 | | - System.out.println(format.getFileFormat()); |
| 126 | + // Upload a file to cloud storage |
| 127 | + FileApi fileApi = new FileApi(configuration); |
| 128 | + try (InputStream uploadStream = new FileInputStream("myfile.docx")) { |
| 129 | + UploadFileRequest uploadRequest = new UploadFileRequest("myfile.docx", uploadStream, null); |
| 130 | + fileApi.uploadFile(uploadRequest); |
| 131 | + } |
| 132 | + |
| 133 | + // Render it to HTML |
| 134 | + ViewApi viewApi = new ViewApi(configuration); |
| 135 | + FileInfo fileInfo = new FileInfo(); |
| 136 | + fileInfo.setFilePath("myfile.docx"); |
| 137 | + |
| 138 | + ViewOptions viewOptions = new ViewOptions(); |
| 139 | + viewOptions.setFileInfo(fileInfo); |
| 140 | + viewOptions.setViewFormat(ViewOptions.ViewFormatEnum.HTML); |
| 141 | + viewOptions.setOutputPath("myfile.html"); |
| 142 | + |
| 143 | + CreateViewRequest viewRequest = new CreateViewRequest(viewOptions); |
| 144 | + viewApi.createView(viewRequest); |
| 145 | + |
| 146 | + // Download the result |
| 147 | + DownloadFileRequest downloadRequest = new DownloadFileRequest("myfile.html", null, null); |
| 148 | + InputStream result = fileApi.downloadFile(downloadRequest); |
| 149 | + |
| 150 | + // Use resulting stream (a *.html file in this example) for your purpose |
| 151 | + try (OutputStream outStream = new FileOutputStream("output.html")) { |
| 152 | + byte[] buffer = new byte[8192]; |
| 153 | + int bytesRead; |
| 154 | + while ((bytesRead = result.read(buffer)) != -1) { |
| 155 | + outStream.write(buffer, 0, bytesRead); |
| 156 | + } |
81 | 157 | } |
82 | | - } catch (ApiException e) { |
83 | | - System.err.println("Failed to get supported file formats"); |
| 158 | + result.close(); |
| 159 | + |
| 160 | + } catch (Exception e) { |
| 161 | + System.err.println("Failed to upload, render, or download the document"); |
84 | 162 | e.printStackTrace(); |
85 | 163 | } |
86 | 164 | } |
|
0 commit comments