Skip to content

Commit 82d9bca

Browse files
authored
Initial
1 parent 441a3f9 commit 82d9bca

File tree

1 file changed

+180
-0
lines changed

1 file changed

+180
-0
lines changed

GETTING-STARTED.md

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

0 commit comments

Comments
 (0)