|
1 | | -<div>cia:<embed type="text/plain" src="https://raw.githubusercontent.com/ConvertAPI/convertapi-java/master/src/com/convertapi/examples/SimpleConversion.java"></div> |
| 1 | +#### 1. Install the ConvertAPI library |
2 | 2 |
|
3 | | -#### 1. Install the ConvertAPI library from NuGet |
| 3 | +Go to [ConvertAPI Java client](https://github.com/ConvertAPI/convertapi-java) page and download JAR file. |
| 4 | +Place JAR in to your project library directory. |
4 | 5 |
|
5 | | -``` |
6 | | -PM> Install-Package ConvertApi |
7 | | -``` |
8 | 6 |
|
9 | 7 | #### 2.a. Simple conversion methods |
10 | 8 |
|
11 | | -```csharp |
12 | | -//Import |
13 | | -using ConvertApiDotNet; |
| 9 | +```java |
| 10 | +import com.convertapi.*; |
14 | 11 |
|
15 | | -//Get your secret at https://www.convertapi.com/a |
16 | | -var convertApi = new ConvertApi("your-api-secret"); |
| 12 | +Config.setDefaultSecret("your-api-secret"); |
17 | 13 |
|
18 | | -//Excel to PDF API https://www.convertapi.com/xlsx-to-pdf |
19 | | -convertApi.ConvertFile(@"c:\test.xlsx", @"c:\sheet.pdf")); |
| 14 | +// Simplified file to file conversion example |
| 15 | +ConvertApi.convertFile("test.docx", "/tmp/result.pdf"); |
20 | 16 |
|
21 | | -//Web to PDF API https://www.convertapi.com/web-to-pdf |
22 | | -convertApi.ConvertUrl("https://www.google.com", @"c:\google.pdf")); |
| 17 | +// Simplified web site to pdf conversion example |
| 18 | +ConvertApi.convertUrl("http://example.com", "/tmp/example.pdf"); |
23 | 19 |
|
24 | | -//Remote Word to PDF API https://www.convertapi.com/docx-to-pdf |
25 | | -convertApi.ConvertRemoteFile("https://cdn.convertapi.com/cara/testfiles/document.docx", @"c:\document.pdf")); |
| 20 | +// Simplified remote file to local file conversion example |
| 21 | +ConvertApi.convertRemoteFile("https://cdn.convertapi.com/cara/testfiles/document.docx", "/tmp/demo.pdf"); |
26 | 22 | ``` |
27 | 23 |
|
28 | | -#### 2.b. Convert local file |
29 | 24 |
|
30 | | -```csharp |
31 | | -//Import |
32 | | -using ConvertApiDotNet; |
| 25 | +#### 2.b. Convert local file |
33 | 26 |
|
34 | | -//Convert Word document |
35 | | -const string sourceFile = @"c:\test.docx"; |
| 27 | +```java |
| 28 | +import com.convertapi.*; |
36 | 29 |
|
37 | | -//Get your secret at https://www.convertapi.com/a |
38 | | -var convertApi = new ConvertApi("your-api-secret"); |
| 30 | +Config.setDefaultSecret("your-api-secret"); |
39 | 31 |
|
40 | 32 | //Set input and output formats and pass file parameter. |
41 | 33 | //Word to PDF API. Read more https://www.convertapi.com/docx-to-pdf |
42 | | -var convertToPdf = convertApi.ConvertAsync("docx", "pdf", new ConvertApiFileParam(sourceFile)); |
43 | | -//Save PDF file |
44 | | -convertToPdf.Result.SaveFiles(@"c:\output"); |
| 34 | +ConvertApi.convert("docx", "pdf", |
| 35 | + new Param("file", Paths.get("test.docx")) |
| 36 | +).get().saveFile(Paths.get("/tmp")).get(); |
45 | 37 | ``` |
46 | 38 |
|
47 | | -#### 2.c. Convert remote file and set additional parameters |
48 | 39 |
|
49 | | -```csharp |
50 | | -//Import |
51 | | -using ConvertApiDotNet; |
| 40 | +#### 2.c. Convert remote file and set additional parameters |
52 | 41 |
|
53 | | -//Convert PowerPoint document |
54 | | -var sourceFile = new Uri("https://github.com/Baltsoft/CDN/raw/master/cara/testfiles/presentation2.pptx"); |
| 42 | +```java |
| 43 | +import com.convertapi.*; |
55 | 44 |
|
56 | | -//Get your secret at https://www.convertapi.com/a |
57 | | -var convertApi = new ConvertApi("your-api-secret"); |
| 45 | +Config.setDefaultSecret("your-api-secret"); |
58 | 46 |
|
59 | 47 | //Set input and output formats and pass file parameter. |
60 | 48 | //PowerPoint to PNG API. Read more https://www.convertapi.com/pptx-to-png |
61 | | -var createThumbnails = convertApi.ConvertAsync("pptx", "png", |
62 | | - new ConvertApiFileParam(sourceFile) |
63 | | - new ConvertApiParam("ScaleImage", "true"), |
64 | | - new ConvertApiParam("ScaleProportions", "true"), |
65 | | - new ConvertApiParam("ImageHeight", "500"), |
66 | | - new ConvertApiParam("ImageWidth", "500") |
67 | | -); |
68 | | -//Save PNG files |
69 | | -createThumbnails.Result.SaveFiles(@"c:\output"); |
| 49 | +ConvertApi.convert("pdf", "png", |
| 50 | + new Param("file", "https://cdn.convertapi.com/cara/testfiles/presentation.pptx"), |
| 51 | + new Param("scaleimage", "true"), |
| 52 | + new Param("scaleproportions", "true"), |
| 53 | + new Param("imageheight", 300) |
| 54 | +).get().saveFile(Paths.get("/tmp")).get(); |
70 | 55 | ``` |
71 | 56 |
|
72 | | -#### 2.d. Convert from a stream |
73 | 57 |
|
74 | | -```csharp |
75 | | -//Import |
76 | | -using ConvertApiDotNet; |
| 58 | +#### 2.d. Convert from a stream |
77 | 59 |
|
78 | | -//Convert html code |
79 | | -const string htmlString = "<!DOCTYPE html><html><body><h1>My First Heading</h1><p>My first paragraph.</p></body></html>"; |
| 60 | +```java |
| 61 | +import com.convertapi.*; |
80 | 62 |
|
81 | | -//Get your secret at https://www.convertapi.com/a |
82 | | -var convertApi = new ConvertApi("your-api-secret"); |
| 63 | +Config.setDefaultSecret("your-api-secret"); |
83 | 64 |
|
84 | | -//Pass as stream |
85 | | -var stream = new MemoryStream(Encoding.UTF8.GetBytes(htmlString)); |
| 65 | +// Create stream from HTML string |
| 66 | +String streamContent = "<!DOCTYPE html><html><body><h1>My First Heading</h1><p>My first paragraph.</p></body></html>"; |
| 67 | +InputStream stream = new ByteArrayInputStream(streamContent.getBytes()); |
86 | 68 |
|
87 | | -//Html to PDF API. Read more https://www.convertapi.com/html-to-pdf |
88 | | -var convertToPdf = convertApi.ConvertAsync("html", "pdf", |
89 | | - new ConvertApiFileParam(stream, "test.html") |
| 69 | +// Html to PDF API. Read more https://www.convertapi.com/html-to-pdf |
| 70 | +CompletableFuture<ConversionResult> result = ConvertApi.convert("html", "pdf", |
| 71 | + new Param("file", stream, "test.html") |
90 | 72 | ); |
91 | 73 |
|
92 | | -//PDF as stream |
93 | | -var outputStream = convertToPdf.Result.FileStream(); |
| 74 | +// PDF as stream |
| 75 | +InputStream resultStream = result.get().asStream().get(); |
94 | 76 | ``` |
95 | 77 |
|
| 78 | + |
96 | 79 | #### 2.e. Conversions chaining |
97 | 80 |
|
98 | | -```csharp |
99 | | -//Import |
100 | | -using ConvertApiDotNet; |
| 81 | +```java |
| 82 | +// Split PDF document and merge first and last pages to new PDF |
| 83 | +import com.convertapi.*; |
101 | 84 |
|
102 | | -//Split PDF document and merge first and last pages to new PDF |
103 | | -const string sourceFile = @"c:\test.pdf"; |
| 85 | +Config.setDefaultSecret("your-api-secret"); |
104 | 86 |
|
105 | | -//Get your secret at https://www.convertapi.com/a |
106 | | -var convertApi = new ConvertApi("your-api-secret"); |
| 87 | +// Set input and output formats and pass file parameter. |
| 88 | +// Split PDF API. Read more https://www.convertapi.com/pdf-to-split |
| 89 | +CompletableFuture<ConversionResult> splitResult = ConvertApi.convert("pdf", "split", |
| 90 | + new Param("file", Paths.get("test.pdf")) |
| 91 | +); |
107 | 92 |
|
108 | | -//Set input and output formats and pass file parameter. |
109 | | -//Split PDF API. Read more https://www.convertapi.com/pdf-to-split |
110 | | -var splitTask = convertApi.ConvertAsync("pdf", "split", |
111 | | - new ConvertApiFileParam(sourceFile)); |
112 | | - |
113 | | -//Get result of the first chain and move it to Merge conversion. |
114 | | -//Chains are executed on server without moving files. |
115 | | -//Merge PDF API. Read more https://www.convertapi.com/pdf-to-merge |
116 | | -var mergeTask = convertApi.ConvertAsync("pdf", "merge", |
117 | | - new ConvertApiFileParam(splitTask.Result.Files.First()), |
118 | | - new ConvertApiFileParam(splitTask.Result.Files.Last())); |
119 | | - |
120 | | -var saveFiles = mergeTask.Result.SaveFile("c:\merged-pdf.pdf"); |
| 93 | +// Get result of the first conversion and use it in Merge conversion. |
| 94 | +// Chains are executed on server without moving files. |
| 95 | +// Merge PDF API. Read more https://www.convertapi.com/pdf-to-merge |
| 96 | +CompletableFuture<ConversionResult> mergeResult = ConvertApi.convert("pdf", "merge", |
| 97 | + new Param("files", splitResult, 0), |
| 98 | + new Param("files", splitResult, -1) |
| 99 | +); |
| 100 | + |
| 101 | +mergeResult.get().saveFile(Paths.get("/tmp")).get(); |
121 | 102 | ``` |
122 | 103 |
|
123 | | -#### 3. Read account status |
124 | 104 |
|
125 | | -```csharp |
126 | | -//Import |
127 | | -using ConvertApiDotNet; |
| 105 | +#### 3. Read account status |
128 | 106 |
|
129 | | -//Convert Word document |
130 | | -const string sourceFile = @"c:\test.docx"; |
| 107 | +```java |
| 108 | +import com.convertapi.*; |
| 109 | +import com.convertapi.model.*; |
131 | 110 |
|
132 | | -//Get your secret at https://www.convertapi.com/a |
133 | | -var convertApi = new ConvertApi("your-api-secret"); |
| 111 | +Config.setDefaultSecret("your-api-secret"); |
134 | 112 |
|
135 | | -//Read full account data |
136 | | -var user = convertApi.GetUserAsync().Result; |
| 113 | +// Read full account data |
| 114 | +User user = ConvertApi.getUser(); |
137 | 115 |
|
138 | | -//Find out how much seconds left |
139 | | -var secondsLeft = user.SecondsLeft; |
| 116 | +// Find out how much seconds left |
| 117 | +int secondsLeft = user.SecondsLeft; |
140 | 118 | ``` |
141 | 119 |
|
| 120 | + |
142 | 121 | #### 4. Exception handling (asynchronous) |
143 | 122 |
|
144 | | -```csharp |
145 | | -//Import |
146 | | -using ConvertApiDotNet; |
147 | | -using ConvertApiDotNet.Exceptions; |
148 | | - |
149 | | -//Convert PDF document |
150 | | -const string sourceFile = @"c:\test.pdf"; |
151 | | - |
152 | | -//Get your secret at https://www.convertapi.com/a |
153 | | -var convertApi = new ConvertApi("your-api-secret"); |
154 | | - |
155 | | -try |
156 | | -{ |
157 | | - //PDF to Powerpoint API. Read more https://www.convertapi.com/pdf-to-pptx |
158 | | - //Set incorect value for parameter AutoRotate and get exception |
159 | | - var convertToPdf = convertApi.ConvertAsync("pdf", "pptx", |
160 | | - new ConvertApiFileParam(sourceFile), |
161 | | - new ConvertApiParam("AutoRotate","WrongParameter") |
162 | | - ); |
163 | | - var outputFileName = convertToPdf.Result.Files[0]; |
164 | | -} |
165 | | -//Catch exceptions from asynchronous methods |
166 | | -catch (AggregateException e) |
167 | | -{ |
168 | | - //Read exception status code |
169 | | - Console.WriteLine("Status Code: " + (e.InnerException as ConvertApiException)?.StatusCode); |
170 | | - //Read exception detailed description |
171 | | - Console.WriteLine("Response: " + (e.InnerException as ConvertApiException)?.Response); |
| 123 | +```java |
| 124 | +import com.convertapi.*; |
| 125 | + |
| 126 | +Config.setDefaultSecret("your-api-secret"); |
| 127 | + |
| 128 | +// Creating an exception |
| 129 | +CompletableFuture<ConversionResult> resultFuture = ConvertApi.convert("pdf", "pptx", |
| 130 | + new Param("file", Paths.get("test-files/test.pdf")), |
| 131 | + new Param("AutoRotate","WrongParameter") |
| 132 | +).exceptionally(t -> { |
| 133 | + // Handling and exception |
| 134 | + System.out.println("Error message: " + t.getMessage()); |
| 135 | + return null; |
| 136 | +}); |
| 137 | + |
| 138 | +ConversionResult result = resultFuture.get(); |
| 139 | +// Saving file if there was no exception |
| 140 | +if (result != null) { |
| 141 | + result.saveFile(Paths.get("/tmp")).get(); |
172 | 142 | } |
173 | 143 | ``` |
174 | 144 |
|
| 145 | + |
175 | 146 | #### 5. Supported file formats, conversions and actions |
176 | 147 |
|
177 | 148 | https://www.convertapi.com/doc/supported-formats |
178 | 149 |
|
179 | 150 | #### 6. GitHub |
180 | 151 |
|
181 | | -https://github.com/ConvertAPI/convertapi-dotnet |
182 | | - |
| 152 | +https://github.com/ConvertAPI/convertapi-java |
0 commit comments