Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Examples/ConvertPdfToDocx/ConvertPdfToDocx.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@
<ItemGroup>
<ProjectReference Include="..\..\ConvertApi\ConvertApi.csproj" />
</ItemGroup>

<ItemGroup>
<None Include="..\TestFiles\test.pdf" Link="TestFiles\test.pdf" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
</Project>
5 changes: 3 additions & 2 deletions Examples/ConvertPdfToDocx/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ static async Task Main(string[] args)
try
{
//Get your API Token at https://www.convertapi.com/a/authentication
var convertApi = new ConvertApi("api_token");
var convertApi = new ConvertApi(Environment.GetEnvironmentVariable("CONVERTAPI_TOKEN"));

const string sourceFile = @"..\..\..\..\TestFiles\test.pdf";
// Sample file from Examples/TestFiles, copied next to the program when it builds
var sourceFile = Path.Combine(AppContext.BaseDirectory, "TestFiles", "test.pdf");

var destinationFileName = Path.Combine(Path.GetTempPath(), $"result-{Guid.NewGuid()}.docx");

Expand Down
5 changes: 5 additions & 0 deletions Examples/ConvertPdfToPdfA/ConvertPdfToPdfA.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@
<ProjectReference Include="..\..\ConvertApi\ConvertApi.csproj" />
</ItemGroup>

<ItemGroup>
<None Include="..\TestFiles\invoice.pdf" Link="TestFiles\invoice.pdf" CopyToOutputDirectory="PreserveNewest" />
<None Include="..\TestFiles\invoice.xml" Link="TestFiles\invoice.xml" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

</Project>
7 changes: 4 additions & 3 deletions Examples/ConvertPdfToPdfA/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ static async Task Main(string[] args)
try
{
// Get your API Token at https://www.convertapi.com/a/authentication
var convertApi = new ConvertApi("api_token");
var convertApi = new ConvertApi(Environment.GetEnvironmentVariable("CONVERTAPI_TOKEN"));

const string sourceFile = @"..\..\..\..\TestFiles\invoice.pdf";
const string invoiceXml = @"..\..\..\..\TestFiles\invoice.xml";
// Sample files from Examples/TestFiles, copied next to the program when it builds
var sourceFile = Path.Combine(AppContext.BaseDirectory, "TestFiles", "invoice.pdf");
var invoiceXml = Path.Combine(AppContext.BaseDirectory, "TestFiles", "invoice.xml");

var destinationFileName = Path.Combine(Path.GetTempPath(), $"result-pdfa-{Guid.NewGuid()}.pdf");

Expand Down
4 changes: 4 additions & 0 deletions Examples/ConvertPdfToPptx/ConvertPdfToPptx.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@
<ItemGroup>
<ProjectReference Include="..\..\ConvertApi\ConvertApi.csproj" />
</ItemGroup>

<ItemGroup>
<None Include="..\TestFiles\test.pdf" Link="TestFiles\test.pdf" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
</Project>
5 changes: 3 additions & 2 deletions Examples/ConvertPdfToPptx/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ static async Task Main(string[] args)
try
{
//Get your API Token at https://www.convertapi.com/a/authentication
var convertApi = new ConvertApi("api_token");
var convertApi = new ConvertApi(Environment.GetEnvironmentVariable("CONVERTAPI_TOKEN"));

const string sourceFile = @"..\..\..\..\TestFiles\test.pdf";
// Sample file from Examples/TestFiles, copied next to the program when it builds
var sourceFile = Path.Combine(AppContext.BaseDirectory, "TestFiles", "test.pdf");

var destinationFileName = Path.Combine(Path.GetTempPath(), $"result-{Guid.NewGuid()}.pptx");

Expand Down
4 changes: 4 additions & 0 deletions Examples/ConvertPdfToXlsx/ConvertPdfToXlsx.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@
<ItemGroup>
<ProjectReference Include="..\..\ConvertApi\ConvertApi.csproj" />
</ItemGroup>

<ItemGroup>
<None Include="..\TestFiles\test.pdf" Link="TestFiles\test.pdf" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
</Project>
5 changes: 3 additions & 2 deletions Examples/ConvertPdfToXlsx/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ static async Task Main(string[] args)
try
{
//Get your API Token at https://www.convertapi.com/a/authentication
var convertApi = new ConvertApi("api_token");
var convertApi = new ConvertApi(Environment.GetEnvironmentVariable("CONVERTAPI_TOKEN"));

const string sourceFile = @"..\..\..\..\TestFiles\test.pdf";
// Sample file from Examples/TestFiles, copied next to the program when it builds
var sourceFile = Path.Combine(AppContext.BaseDirectory, "TestFiles", "test.pdf");

var destinationFileName = Path.Combine(Path.GetTempPath(), $"result-{Guid.NewGuid()}.xlsx");

Expand Down
2 changes: 1 addition & 1 deletion Examples/ConvertRemoteFile/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ static async Task Main(string[] args)
try
{
//Get your API Token at https://www.convertapi.com/a/authentication
var convertApi = new ConvertApi("api_token");
var convertApi = new ConvertApi(Environment.GetEnvironmentVariable("CONVERTAPI_TOKEN"));

var sourceFile = new Uri("https://cdn.convertapi.com/public/files/demo.pptx");

Expand Down
6 changes: 3 additions & 3 deletions Examples/ConvertUrlToPdf/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ class Program
{
/// <summary>
/// Example of converting Web Page URL to PDF file
/// https://www.convertapi.com/web-to-pdf
/// https://www.convertapi.com/html-to-pdf
/// </summary>
static async Task Main(string[] args)
{
try
{
//Get your API Token at https://www.convertapi.com/a/authentication
var convertApi = new ConvertApi("api_token");
var convertApi = new ConvertApi(Environment.GetEnvironmentVariable("CONVERTAPI_TOKEN"));

Console.WriteLine("Converting web page https://en.wikipedia.org/wiki/Data_conversion to PDF...");

var response = await convertApi.ConvertAsync("web", "pdf",
var response = await convertApi.ConvertAsync("html", "pdf",
new ConvertApiParam("Url", "https://en.wikipedia.org/wiki/Data_conversion"),
new ConvertApiParam("FileName", "web-example"));

Expand Down
4 changes: 4 additions & 0 deletions Examples/ConvertWordToPdfAndPng/ConvertWordToPdfAndPng.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
<ProjectReference Include="..\..\ConvertApi\ConvertApi.csproj" />
</ItemGroup>

<ItemGroup>
<None Include="..\TestFiles\test.docx" Link="TestFiles\test.docx" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

<ItemGroup>
<Compile Remove="Properties\AssemblyInfo.cs" />
<None Remove="App.config" />
Expand Down
6 changes: 4 additions & 2 deletions Examples/ConvertWordToPdfAndPng/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ class Program
static async Task Main(string[] args)
{
//Get your API Token at https://www.convertapi.com/a/authentication
var convertApi = new ConvertApi("api_token");
const string sourceFile = @"..\..\..\TestFiles\test.docx";
var convertApi = new ConvertApi(Environment.GetEnvironmentVariable("CONVERTAPI_TOKEN"));

// Sample file from Examples/TestFiles, copied next to the program when it builds
var sourceFile = Path.Combine(AppContext.BaseDirectory, "TestFiles", "test.docx");

var fileParam = new ConvertApiFileParam(sourceFile);

Expand Down
2 changes: 0 additions & 2 deletions Examples/ConverterCatalogDemo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
Console.WriteLine("=== ConvertAPI Converter Catalog Demo ===");
Console.WriteLine($"Base URI: {ConvertApiDotNet.ConvertApi.ApiBaseUri}");
Console.WriteLine();

ConvertApiDotNet.ConvertApi.ApiBaseUri = "https://stag-v2.convertapi.com";

var catalog = new ConverterCatalog();

Expand Down Expand Up @@ -120,7 +118,7 @@
catch (Exception ex)
{
Console.WriteLine("Call failed: " + ex.Message);
return null;

Check warning on line 121 in Examples/ConverterCatalogDemo/Program.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.

Check warning on line 121 in Examples/ConverterCatalogDemo/Program.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.
}
}

Expand All @@ -134,7 +132,7 @@
}
}

private static void PrintConverters(IEnumerable<ConverterDto> items, string title = null)

Check warning on line 135 in Examples/ConverterCatalogDemo/Program.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

Check warning on line 135 in Examples/ConverterCatalogDemo/Program.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
{
if (!string.IsNullOrWhiteSpace(title))
Console.WriteLine(title + ":");
Expand Down
4 changes: 4 additions & 0 deletions Examples/CreatePdfThumbnail/CreatePdfThumbnail.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
<ProjectReference Include="..\..\ConvertApi\ConvertApi.csproj" />
</ItemGroup>

<ItemGroup>
<None Include="..\TestFiles\test.pdf" Link="TestFiles\test.pdf" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

<ItemGroup>
<Compile Remove="Properties\AssemblyInfo.cs" />
<None Remove="App.config" />
Expand Down
12 changes: 6 additions & 6 deletions Examples/CreatePdfThumbnail/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ class Program
{
/// <summary>
/// Create PDF Thumbnail
/// Example of extracting first page from PDF and then converting it to JPG using chaining.
/// https://www.convertapi.com/pdf-to-extract
/// Example of converting the first page of a PDF to a JPG thumbnail.
/// https://www.convertapi.com/pdf-to-jpg
/// </summary>
static async Task Main(string[] args)
{
//Get your API Token at https://www.convertapi.com/a/authentication
var convertApi = new ConvertApi("api_token");
var pdfFile = @"..\..\..\TestFiles\test.pdf";
var convertApi = new ConvertApi(Environment.GetEnvironmentVariable("CONVERTAPI_TOKEN"));

// Sample file from Examples/TestFiles, copied next to the program when it builds
var pdfFile = Path.Combine(AppContext.BaseDirectory, "TestFiles", "test.pdf");

try
{
var thumbnail = await convertApi.ConvertAsync("pdf", "jpg",
new ConvertApiFileParam(pdfFile),
new ConvertApiParam("ScaleImage", "true"),
new ConvertApiParam("ScaleProportions", "true"),
new ConvertApiParam("ImageHeight", "300"),
new ConvertApiParam("ImageWidth", "300"),
new ConvertApiParam("FitToSize", "true"),
new ConvertApiParam("PageRange", "1")
);

Expand Down
4 changes: 4 additions & 0 deletions Examples/DeleteFiles/DeleteFiles.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@
<ProjectReference Include="..\..\ConvertApi\ConvertApi.csproj" />
</ItemGroup>

<ItemGroup>
<None Include="..\TestFiles\test.docx" Link="TestFiles\test.docx" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

</Project>
5 changes: 3 additions & 2 deletions Examples/DeleteFiles/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ static async Task Main(string[] args)
try
{
// Get your API Token at https://www.convertapi.com/a/authentication
var convertApi = new ConvertApi("api_token");
var convertApi = new ConvertApi(Environment.GetEnvironmentVariable("CONVERTAPI_TOKEN"));

const string sourceFile = @"..\..\..\..\TestFiles\test.docx";
// Sample file from Examples/TestFiles, copied next to the program when it builds
var sourceFile = Path.Combine(AppContext.BaseDirectory, "TestFiles", "test.docx");

Console.WriteLine("Converting DOCX to PDF...");

Expand Down
4 changes: 4 additions & 0 deletions Examples/ExceptionHandling/ExceptionHandling.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@
<ProjectReference Include="..\..\ConvertApi\ConvertApi.csproj" />
</ItemGroup>

<ItemGroup>
<None Include="..\TestFiles\test.docx" Link="TestFiles\test.docx" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

</Project>
7 changes: 5 additions & 2 deletions Examples/ExceptionHandling/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@

try
{
var convertApi = new ConvertApi("api_token");
const string sourceFile = @"..\..\..\TestFiles\test.docx";
// Get your API Token at https://www.convertapi.com/a/authentication
var convertApi = new ConvertApi(Environment.GetEnvironmentVariable("CONVERTAPI_TOKEN"));

// Sample file from Examples/TestFiles, copied next to the program when it builds
var sourceFile = Path.Combine(AppContext.BaseDirectory, "TestFiles", "test.docx");

var convert = await convertApi.ConvertAsync("pdf", "split",
new ConvertApiFileParam(sourceFile));
Expand Down
2 changes: 1 addition & 1 deletion Examples/FromStreamToStream/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Program
static async Task Main(string[] args)
{
//Get your API Token at https://www.convertapi.com/a/authentication
var convertApi = new ConvertApi("api_token");
var convertApi = new ConvertApi(Environment.GetEnvironmentVariable("CONVERTAPI_TOKEN"));
const string htmlString = "<!DOCTYPE html><html><body><h1>My First Heading</h1><p>My first paragraph.</p></body></html>";

var stream = new MemoryStream(Encoding.UTF8.GetBytes(htmlString));
Expand Down
4 changes: 2 additions & 2 deletions Examples/GetConverterInfoDemo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
static async Task<int> Main(string[] args)
{
//Get your api token at https://www.convertapi.com/a
var token = "api_token";
var token = Environment.GetEnvironmentVariable("CONVERTAPI_TOKEN");

var api = new ConvertApi(token);

Expand All @@ -16,7 +16,7 @@
("docx", "pdf"),
("pdf", "pptx"),
("pdf", "merge"),
("any", "pdf")
("file", "pdf")
};

foreach (var (src, dst) in samples)
Expand All @@ -25,7 +25,7 @@
{
Console.WriteLine(new string('=', 60));
Console.WriteLine($"Converter {src} -> {dst}");
var info = await api.GetConverterInfo(src, dst);

Check warning on line 28 in Examples/GetConverterInfoDemo/Program.cs

View workflow job for this annotation

GitHub Actions / build

'ConvertApi.GetConverterInfo(string, string, CancellationToken)' is obsolete: 'Use ConverterCatalog instead'

Check warning on line 28 in Examples/GetConverterInfoDemo/Program.cs

View workflow job for this annotation

GitHub Actions / build

'ConvertApi.GetConverterInfo(string, string, CancellationToken)' is obsolete: 'Use ConverterCatalog instead'
Console.WriteLine($"Title : {info.Title}");
Console.WriteLine($"Summary : {info.Summary}");
Console.WriteLine($"AcceptsFormats : {info.AcceptsFormats}");
Expand Down
4 changes: 4 additions & 0 deletions Examples/HandleRetry/HandleRetry.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@
<ItemGroup>
<ProjectReference Include="..\..\ConvertApi\ConvertApi.csproj" />
</ItemGroup>

<ItemGroup>
<None Include="..\TestFiles\test.pdf" Link="TestFiles\test.pdf" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
</Project>
6 changes: 4 additions & 2 deletions Examples/HandleRetry/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ class Program
static async Task Main()
{
//Get your api token at https://www.convertapi.com/a
var convertApi = new ConvertApi("api_token");
const string sourceFile = @"..\..\..\..\TestFiles\test.pdf";
var convertApi = new ConvertApi(Environment.GetEnvironmentVariable("CONVERTAPI_TOKEN"));

// Sample file from Examples/TestFiles, copied next to the program when it builds
var sourceFile = Path.Combine(AppContext.BaseDirectory, "TestFiles", "test.pdf");

// Define two retry delays.
var retryDelays = new[] { TimeSpan.FromSeconds(2.5), TimeSpan.FromSeconds(7) };
Expand Down
5 changes: 5 additions & 0 deletions Examples/MergeWordDocsToPdf/MergeWordDocsToPdf.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@
<ProjectReference Include="..\..\ConvertApi\ConvertApi.csproj" />
</ItemGroup>

<ItemGroup>
<None Include="..\TestFiles\test.docx" Link="TestFiles\test.docx" CopyToOutputDirectory="PreserveNewest" />
<None Include="..\TestFiles\test.pptx" Link="TestFiles\test.pptx" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

</Project>
10 changes: 7 additions & 3 deletions Examples/MergeWordDocsToPdf/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@ static async Task Main(string[] args)
var sw = new Stopwatch();
sw.Start();
//Get your API Token at https://www.convertapi.com/a/authentication
var convertApi = new ConvertApi("api_token");
var convertApi = new ConvertApi(Environment.GetEnvironmentVariable("CONVERTAPI_TOKEN"));

var destinationFileName = Path.Combine(Path.GetTempPath(), $"test-merged-{Guid.NewGuid()}.pdf");

var convertApiFileParam = new ConvertApiFileParam(@"..\..\..\..\TestFiles\test.docx");
// Sample files from Examples/TestFiles, copied next to the program when it builds
var wordFile = Path.Combine(AppContext.BaseDirectory, "TestFiles", "test.docx");
var powerPointFile = Path.Combine(AppContext.BaseDirectory, "TestFiles", "test.pptx");

var convertApiFileParam = new ConvertApiFileParam(wordFile);

var doc1 = convertApi.ConvertAsync("docx", "pdf", convertApiFileParam);
Console.WriteLine("Word sent for conversion...");
var doc2 = convertApi.ConvertAsync("pptx", "pdf", new ConvertApiFileParam(@"..\..\..\..\TestFiles\test.pptx"));
var doc2 = convertApi.ConvertAsync("pptx", "pdf", new ConvertApiFileParam(powerPointFile));
Console.WriteLine("PowerPoint sent for conversion...");

var mergeTask = await convertApi.ConvertAsync("pdf", "merge",
Expand Down
30 changes: 30 additions & 0 deletions Examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# ConvertAPI .NET SDK examples

Each folder is a small console app that shows one task with the ConvertAPI .NET SDK. The examples build against the SDK source in this repository, so you need the .NET 10 SDK.

## Run an example

1. Get your API token at https://www.convertapi.com/a/authentication
2. Put it in the `CONVERTAPI_TOKEN` environment variable:

```bash
export CONVERTAPI_TOKEN=your-api-token # macOS and Linux
```

```powershell
$env:CONVERTAPI_TOKEN = "your-api-token" # Windows PowerShell
```

3. From the repository root, run an example by its folder name:

```bash
dotnet run --project Examples/CreatePdfThumbnail
```

The example prints where it saved the result, usually in your temp folder. Most examples then wait for Enter before they exit.

In Visual Studio or Rider, set the example as the startup project. The IDE only sees the variable if it was set before the IDE started: on Windows, `setx CONVERTAPI_TOKEN your-api-token` sets it for programs you start afterwards.

## Sample files

The input files are in `TestFiles` (SplitPDF has its own in `SplitPDF/files`). Each example's `.csproj` copies the files it uses next to the program when it builds, and `Program.cs` finds them through `AppContext.BaseDirectory`, so the examples run from any working directory. To convert your own file, put its path in `Program.cs` instead.
4 changes: 1 addition & 3 deletions Examples/RetrieveUserInformation/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ class Program
static async Task Main(string[] args)
{
//Get your API Token at https://www.convertapi.com/a/authentication
var convertApi = new ConvertApi("api_token");
var convertApi = new ConvertApi(Environment.GetEnvironmentVariable("CONVERTAPI_TOKEN"));
var convertApiUser = await convertApi.GetUserAsync();

Console.WriteLine("Secret={0}", convertApiUser.Secret);
Console.WriteLine("ApiKey={0}", convertApiUser.ApiKey);
Console.WriteLine("Active={0}", convertApiUser.Active);
Console.WriteLine("FullName={0}", convertApiUser.FullName);
Console.WriteLine("Email={0}", convertApiUser.Email);
Expand Down
6 changes: 4 additions & 2 deletions Examples/SplitAndMergePdf/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ static async Task Main(string[] args)
try
{
//Get your API Token at https://www.convertapi.com/a/authentication
var convertApi = new ConvertApi("api_token");
const string sourceFile = @"..\..\..\TestFiles\test.pdf";
var convertApi = new ConvertApi(Environment.GetEnvironmentVariable("CONVERTAPI_TOKEN"));

// Sample file from Examples/TestFiles, copied next to the program when it builds
var sourceFile = Path.Combine(AppContext.BaseDirectory, "TestFiles", "test.pdf");

var destinationFileName = Path.Combine(Path.GetTempPath(), $"test-merged-{Guid.NewGuid()}.pdf");

Expand Down
4 changes: 4 additions & 0 deletions Examples/SplitAndMergePdf/SplitAndMergePdf.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
<ProjectReference Include="..\..\ConvertApi\ConvertApi.csproj" />
</ItemGroup>

<ItemGroup>
<None Include="..\TestFiles\test.pdf" Link="TestFiles\test.pdf" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

<ItemGroup>
<Compile Remove="Properties\AssemblyInfo.cs" />
<None Remove="App.config" />
Expand Down
Loading
Loading