|
| 1 | +<div>cia:<embed type="text/plain" src="https://raw.githubusercontent.com/ConvertAPI/convertapi-java/master/src/com/convertapi/examples/SimpleConversion.java"></div> |
| 2 | + |
| 3 | +#### 1. Install the ConvertAPI library from NuGet |
| 4 | + |
| 5 | +``` |
| 6 | +PM> Install-Package ConvertApi |
| 7 | +``` |
| 8 | + |
| 9 | +#### 2.a. Simple conversion methods |
| 10 | + |
| 11 | +```csharp |
| 12 | +//Import |
| 13 | +using ConvertApiDotNet; |
| 14 | + |
| 15 | +//Get your secret at https://www.convertapi.com/a |
| 16 | +var convertApi = new ConvertApi("your-api-secret"); |
| 17 | + |
| 18 | +//Excel to PDF API https://www.convertapi.com/xlsx-to-pdf |
| 19 | +convertApi.ConvertFile(@"c:\test.xlsx", @"c:\sheet.pdf")); |
| 20 | + |
| 21 | +//Web to PDF API https://www.convertapi.com/web-to-pdf |
| 22 | +convertApi.ConvertUrl("https://www.google.com", @"c:\google.pdf")); |
| 23 | + |
| 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")); |
| 26 | +``` |
| 27 | + |
| 28 | +#### 2.b. Convert local file |
| 29 | + |
| 30 | +```csharp |
| 31 | +//Import |
| 32 | +using ConvertApiDotNet; |
| 33 | + |
| 34 | +//Convert Word document |
| 35 | +const string sourceFile = @"c:\test.docx"; |
| 36 | + |
| 37 | +//Get your secret at https://www.convertapi.com/a |
| 38 | +var convertApi = new ConvertApi("your-api-secret"); |
| 39 | + |
| 40 | +//Set input and output formats and pass file parameter. |
| 41 | +//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"); |
| 45 | +``` |
| 46 | + |
| 47 | +#### 2.c. Convert remote file and set additional parameters |
| 48 | + |
| 49 | +```csharp |
| 50 | +//Import |
| 51 | +using ConvertApiDotNet; |
| 52 | + |
| 53 | +//Convert PowerPoint document |
| 54 | +var sourceFile = new Uri("https://github.com/Baltsoft/CDN/raw/master/cara/testfiles/presentation2.pptx"); |
| 55 | + |
| 56 | +//Get your secret at https://www.convertapi.com/a |
| 57 | +var convertApi = new ConvertApi("your-api-secret"); |
| 58 | + |
| 59 | +//Set input and output formats and pass file parameter. |
| 60 | +//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"); |
| 70 | +``` |
| 71 | + |
| 72 | +#### 2.d. Convert from a stream |
| 73 | + |
| 74 | +```csharp |
| 75 | +//Import |
| 76 | +using ConvertApiDotNet; |
| 77 | + |
| 78 | +//Convert html code |
| 79 | +const string htmlString = "<!DOCTYPE html><html><body><h1>My First Heading</h1><p>My first paragraph.</p></body></html>"; |
| 80 | + |
| 81 | +//Get your secret at https://www.convertapi.com/a |
| 82 | +var convertApi = new ConvertApi("your-api-secret"); |
| 83 | + |
| 84 | +//Pass as stream |
| 85 | +var stream = new MemoryStream(Encoding.UTF8.GetBytes(htmlString)); |
| 86 | + |
| 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") |
| 90 | +); |
| 91 | + |
| 92 | +//PDF as stream |
| 93 | +var outputStream = convertToPdf.Result.FileStream(); |
| 94 | +``` |
| 95 | + |
| 96 | +#### 2.e. Conversions chaining |
| 97 | + |
| 98 | +```csharp |
| 99 | +//Import |
| 100 | +using ConvertApiDotNet; |
| 101 | + |
| 102 | +//Split PDF document and merge first and last pages to new PDF |
| 103 | +const string sourceFile = @"c:\test.pdf"; |
| 104 | + |
| 105 | +//Get your secret at https://www.convertapi.com/a |
| 106 | +var convertApi = new ConvertApi("your-api-secret"); |
| 107 | + |
| 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"); |
| 121 | +``` |
| 122 | + |
| 123 | +#### 3. Read account status |
| 124 | + |
| 125 | +```csharp |
| 126 | +//Import |
| 127 | +using ConvertApiDotNet; |
| 128 | + |
| 129 | +//Convert Word document |
| 130 | +const string sourceFile = @"c:\test.docx"; |
| 131 | + |
| 132 | +//Get your secret at https://www.convertapi.com/a |
| 133 | +var convertApi = new ConvertApi("your-api-secret"); |
| 134 | + |
| 135 | +//Read full account data |
| 136 | +var user = convertApi.GetUserAsync().Result; |
| 137 | + |
| 138 | +//Find out how much seconds left |
| 139 | +var secondsLeft = user.SecondsLeft; |
| 140 | +``` |
| 141 | + |
| 142 | +#### 4. Exception handling (asynchronous) |
| 143 | + |
| 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); |
| 172 | +} |
| 173 | +``` |
| 174 | + |
| 175 | +#### 5. Supported file formats, conversions and actions |
| 176 | + |
| 177 | +https://www.convertapi.com/doc/supported-formats |
| 178 | + |
| 179 | +#### 6. GitHub |
| 180 | + |
| 181 | +https://github.com/ConvertAPI/convertapi-dotnet |
| 182 | + |
0 commit comments