Skip to content

Commit abb9c12

Browse files
committed
More examples + getting-started
1 parent 7b7f4b0 commit abb9c12

File tree

9 files changed

+241
-142
lines changed

9 files changed

+241
-142
lines changed

GETTING-STARTED.md

Lines changed: 91 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,182 +1,152 @@
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
22

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.
45

5-
```
6-
PM> Install-Package ConvertApi
7-
```
86

97
#### 2.a. Simple conversion methods
108

11-
```csharp
12-
//Import
13-
using ConvertApiDotNet;
9+
```java
10+
import com.convertapi.*;
1411

15-
//Get your secret at https://www.convertapi.com/a
16-
var convertApi = new ConvertApi("your-api-secret");
12+
Config.setDefaultSecret("your-api-secret");
1713

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");
2016

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");
2319

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");
2622
```
2723

28-
#### 2.b. Convert local file
2924

30-
```csharp
31-
//Import
32-
using ConvertApiDotNet;
25+
#### 2.b. Convert local file
3326

34-
//Convert Word document
35-
const string sourceFile = @"c:\test.docx";
27+
```java
28+
import com.convertapi.*;
3629

37-
//Get your secret at https://www.convertapi.com/a
38-
var convertApi = new ConvertApi("your-api-secret");
30+
Config.setDefaultSecret("your-api-secret");
3931

4032
//Set input and output formats and pass file parameter.
4133
//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();
4537
```
4638

47-
#### 2.c. Convert remote file and set additional parameters
4839

49-
```csharp
50-
//Import
51-
using ConvertApiDotNet;
40+
#### 2.c. Convert remote file and set additional parameters
5241

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.*;
5544

56-
//Get your secret at https://www.convertapi.com/a
57-
var convertApi = new ConvertApi("your-api-secret");
45+
Config.setDefaultSecret("your-api-secret");
5846

5947
//Set input and output formats and pass file parameter.
6048
//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();
7055
```
7156

72-
#### 2.d. Convert from a stream
7357

74-
```csharp
75-
//Import
76-
using ConvertApiDotNet;
58+
#### 2.d. Convert from a stream
7759

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.*;
8062

81-
//Get your secret at https://www.convertapi.com/a
82-
var convertApi = new ConvertApi("your-api-secret");
63+
Config.setDefaultSecret("your-api-secret");
8364

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());
8668

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")
9072
);
9173

92-
//PDF as stream
93-
var outputStream = convertToPdf.Result.FileStream();
74+
// PDF as stream
75+
InputStream resultStream = result.get().asStream().get();
9476
```
9577

78+
9679
#### 2.e. Conversions chaining
9780

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.*;
10184

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");
10486

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+
);
10792

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();
121102
```
122103

123-
#### 3. Read account status
124104

125-
```csharp
126-
//Import
127-
using ConvertApiDotNet;
105+
#### 3. Read account status
128106

129-
//Convert Word document
130-
const string sourceFile = @"c:\test.docx";
107+
```java
108+
import com.convertapi.*;
109+
import com.convertapi.model.*;
131110

132-
//Get your secret at https://www.convertapi.com/a
133-
var convertApi = new ConvertApi("your-api-secret");
111+
Config.setDefaultSecret("your-api-secret");
134112

135-
//Read full account data
136-
var user = convertApi.GetUserAsync().Result;
113+
// Read full account data
114+
User user = ConvertApi.getUser();
137115

138-
//Find out how much seconds left
139-
var secondsLeft = user.SecondsLeft;
116+
// Find out how much seconds left
117+
int secondsLeft = user.SecondsLeft;
140118
```
141119

120+
142121
#### 4. Exception handling (asynchronous)
143122

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();
172142
}
173143
```
174144

145+
175146
#### 5. Supported file formats, conversions and actions
176147

177148
https://www.convertapi.com/doc/supported-formats
178149

179150
#### 6. GitHub
180151

181-
https://github.com/ConvertAPI/convertapi-dotnet
182-
152+
https://github.com/ConvertAPI/convertapi-java

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ import com.convertapi.ConvertApi;
9393

9494
public class SimpleConversion {
9595
public static void main(String[] args) {
96-
ConvertApi.convert("source.docx", "result.pdf", "YOUR API SECRET");
96+
ConvertApi.convertFile("source.docx", "result.pdf", "YOUR API SECRET");
9797
}
9898
}
9999
```

src/com/convertapi/ConversionException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.convertapi;
22

3-
class ConversionException extends RuntimeException {
3+
public class ConversionException extends RuntimeException {
44
private final int httpStatusCode;
55

66
public ConversionException(String message, int httpStatusCode){

src/com/convertapi/ConversionResult.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.convertapi.model.ConversionResponse;
44
import com.convertapi.model.ConversionResponseFile;
55

6+
import java.io.InputStream;
67
import java.nio.file.Files;
78
import java.nio.file.Path;
89
import java.util.ArrayList;
@@ -41,7 +42,11 @@ public ConversionResultFile getFile(int index) {
4142
return new ConversionResultFile(response.Files[index]);
4243
}
4344

44-
public CompletableFuture<Path> saveFile(Path file) {
45+
public CompletableFuture<InputStream> asStream() {
46+
return getFile(0).asStream();
47+
}
48+
49+
public CompletableFuture<Path> saveFile(Path file) {
4550
return getFile(0).saveFile(file);
4651
}
4752

src/com/convertapi/Param.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import okhttp3.MediaType;
44
import okhttp3.Request;
5-
import okhttp3.RequestBody;
65

76
import java.io.IOException;
7+
import java.io.InputStream;
88
import java.math.BigDecimal;
99
import java.nio.file.Files;
1010
import java.nio.file.Path;
@@ -14,6 +14,8 @@
1414
import java.util.concurrent.CompletableFuture;
1515
import java.util.concurrent.ExecutionException;
1616

17+
import static java.nio.file.StandardOpenOption.READ;
18+
1719
@SuppressWarnings("WeakerAccess")
1820
public class Param {
1921
private final String name;
@@ -43,28 +45,25 @@ public Param(String name, BigDecimal value) {
4345
this(name, String.valueOf(value));
4446
}
4547

46-
@SuppressWarnings("unused")
47-
public Param(String name, byte[] value, String fileFormat) {
48-
this(name, value, fileFormat, Config.defaults());
48+
public Param(String name, Path value) throws IOException {
49+
this(name, value, Config.defaults());
4950
}
5051

5152
@SuppressWarnings("WeakerAccess")
52-
public Param(String name, byte[] value, String fileFormat, Config config) {
53-
this(name);
54-
String fileName = "getFile." + fileFormat;
55-
this.value = upload(value, fileName, config);
56-
isUploadedFile = true;
53+
public Param(String name, InputStream stream, String fileName) {
54+
this(name, stream, fileName, Config.defaults());
5755
}
5856

59-
public Param(String name, Path value) throws IOException {
60-
this(name, value, Config.defaults());
57+
@SuppressWarnings("WeakerAccess")
58+
public Param(String name, InputStream stream, String fileName, Config config) {
59+
this(name);
60+
this.value = upload(stream, fileName, config);
61+
isUploadedFile = true;
6162
}
6263

6364
@SuppressWarnings("WeakerAccess")
6465
public Param(String name, Path value, Config config) throws IOException {
65-
this(name);
66-
this.value = upload(Files.readAllBytes(value), value.getFileName().toString(), config);
67-
isUploadedFile = true;
66+
this(name, Files.newInputStream(value, READ), value.getFileName().toString(), config);
6867
}
6968

7069
@SuppressWarnings("unused")
@@ -107,13 +106,13 @@ public CompletableFuture<Void> delete() {
107106
: CompletableFuture.completedFuture(null);
108107
}
109108

110-
private static CompletableFuture<List<String>> upload(byte[] data, String fileName, Config config) {
109+
private static CompletableFuture<List<String>> upload(InputStream stream, String fileName, Config config) {
111110
return CompletableFuture.supplyAsync(() -> {
112111
Request request = Http.getRequestBuilder()
113112
.url(Http.getUrlBuilder(config).addPathSegment("upload")
114113
.addQueryParameter("filename", fileName)
115114
.build())
116-
.post(RequestBody.create(MediaType.parse("application/octet-stream"), data))
115+
.post(RequestBodyStream.create(MediaType.parse("application/octet-stream"), stream))
117116
.build();
118117
try {
119118
String id = Http.getClient().newCall(request).execute().body().string();

0 commit comments

Comments
 (0)