From 3d70c443f1ce46fb5cf13e5e0fe5b3ed10205911 Mon Sep 17 00:00:00 2001 From: Tomas Rutkauskas Date: Fri, 6 Nov 2026 16:48:29 +0200 Subject: [PATCH 1/3] PDF thumbnail example: fix the comment, keep the thumbnail inside 300x300 The example already converts page 1 straight to JPG, but its comment still described the old extract-then-convert chain and linked pdf-to-extract, which is now the AI data-extraction converter. ScaleImage and ScaleProportions are not pdf -> jpg parameters and the API ignored them, so the 300x300 request came back 300x388 for test.pdf. FitToSize keeps the thumbnail inside 300x300 (231x300). Co-Authored-By: Claude Opus 5.5 --- Examples/CreatePdfThumbnail/Program.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Examples/CreatePdfThumbnail/Program.cs b/Examples/CreatePdfThumbnail/Program.cs index e0e4408..d7b085c 100644 --- a/Examples/CreatePdfThumbnail/Program.cs +++ b/Examples/CreatePdfThumbnail/Program.cs @@ -11,8 +11,7 @@ class Program { /// /// 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 /// static async Task Main(string[] args) @@ -25,10 +24,9 @@ static async Task Main(string[] args) { 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") ); From 0d44e923a96ecb7eb03aa6a07e1d4cc27eeaf5c6 Mon Sep 17 00:00:00 2001 From: Tomas Rutkauskas Date: Mon, 9 Nov 2026 10:41:53 +0200 Subject: [PATCH 2/3] Examples: read the token from CONVERTAPI_TOKEN and find the sample files from any folder The examples reached Examples/TestFiles through paths relative to the working directory, three levels up in six places and four in nine. From bin/Debug/net10.0 only the four-level form reached TestFiles, from the project or repository folder neither did, and the backslash paths do not resolve on macOS or Linux. UploadOnceReuseTwice and Workflow climbed four levels from AppContext.BaseDirectory, and SplitPDF read files/sample.pdf from the working directory. Each example's .csproj now copies the sample files it uses to TestFiles/ next to the program, and Program.cs reads them through AppContext.BaseDirectory, so the examples run from dotnet run, Visual Studio and Rider, on any OS. The examples read the API token from the CONVERTAPI_TOKEN environment variable, the name the README quick start (#83) and the C# pages on convertapi.com use, instead of a hard-coded "api_token". Examples/README.md says how to set it and run an example. Co-Authored-By: Claude Opus 5.5 --- .../ConvertPdfToDocx/ConvertPdfToDocx.csproj | 4 +++ Examples/ConvertPdfToDocx/Program.cs | 5 ++-- .../ConvertPdfToPdfA/ConvertPdfToPdfA.csproj | 5 ++++ Examples/ConvertPdfToPdfA/Program.cs | 7 +++-- .../ConvertPdfToPptx/ConvertPdfToPptx.csproj | 4 +++ Examples/ConvertPdfToPptx/Program.cs | 5 ++-- .../ConvertPdfToXlsx/ConvertPdfToXlsx.csproj | 4 +++ Examples/ConvertPdfToXlsx/Program.cs | 5 ++-- Examples/ConvertRemoteFile/Program.cs | 2 +- Examples/ConvertUrlToPdf/Program.cs | 2 +- .../ConvertWordToPdfAndPng.csproj | 4 +++ Examples/ConvertWordToPdfAndPng/Program.cs | 6 ++-- .../CreatePdfThumbnail.csproj | 4 +++ Examples/CreatePdfThumbnail/Program.cs | 6 ++-- Examples/DeleteFiles/DeleteFiles.csproj | 4 +++ Examples/DeleteFiles/Program.cs | 5 ++-- .../ExceptionHandling.csproj | 4 +++ Examples/ExceptionHandling/Program.cs | 7 +++-- Examples/FromStreamToStream/Program.cs | 2 +- Examples/GetConverterInfoDemo/Program.cs | 2 +- Examples/HandleRetry/HandleRetry.csproj | 4 +++ Examples/HandleRetry/Program.cs | 6 ++-- .../MergeWordDocsToPdf.csproj | 5 ++++ Examples/MergeWordDocsToPdf/Program.cs | 10 +++++-- Examples/README.md | 30 +++++++++++++++++++ Examples/RetrieveUserInformation/Program.cs | 2 +- Examples/SplitAndMergePdf/Program.cs | 6 ++-- .../SplitAndMergePdf/SplitAndMergePdf.csproj | 4 +++ Examples/SplitPDF/Program.cs | 19 +++++++----- Examples/UploadOnceReuseTwice/Program.cs | 9 ++---- Examples/UploadOnceReuseTwice/README.md | 7 ++--- .../UploadOnceReuseTwice.csproj | 4 +++ Examples/WatermarkPdf/Program.cs | 10 +++++-- Examples/WatermarkPdf/WatermarkPdf.csproj | 5 ++++ Examples/Workflow/Program.cs | 6 ++-- Examples/Workflow/Workflow.csproj | 4 +++ 36 files changed, 165 insertions(+), 53 deletions(-) create mode 100644 Examples/README.md diff --git a/Examples/ConvertPdfToDocx/ConvertPdfToDocx.csproj b/Examples/ConvertPdfToDocx/ConvertPdfToDocx.csproj index 6786879..c6d466c 100644 --- a/Examples/ConvertPdfToDocx/ConvertPdfToDocx.csproj +++ b/Examples/ConvertPdfToDocx/ConvertPdfToDocx.csproj @@ -10,4 +10,8 @@ + + + + diff --git a/Examples/ConvertPdfToDocx/Program.cs b/Examples/ConvertPdfToDocx/Program.cs index 88b1328..8950850 100644 --- a/Examples/ConvertPdfToDocx/Program.cs +++ b/Examples/ConvertPdfToDocx/Program.cs @@ -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"); diff --git a/Examples/ConvertPdfToPdfA/ConvertPdfToPdfA.csproj b/Examples/ConvertPdfToPdfA/ConvertPdfToPdfA.csproj index d67784c..bf3d603 100644 --- a/Examples/ConvertPdfToPdfA/ConvertPdfToPdfA.csproj +++ b/Examples/ConvertPdfToPdfA/ConvertPdfToPdfA.csproj @@ -11,4 +11,9 @@ + + + + + diff --git a/Examples/ConvertPdfToPdfA/Program.cs b/Examples/ConvertPdfToPdfA/Program.cs index 359a9db..c6c16a0 100644 --- a/Examples/ConvertPdfToPdfA/Program.cs +++ b/Examples/ConvertPdfToPdfA/Program.cs @@ -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"); diff --git a/Examples/ConvertPdfToPptx/ConvertPdfToPptx.csproj b/Examples/ConvertPdfToPptx/ConvertPdfToPptx.csproj index 6ad95ed..3bb9362 100644 --- a/Examples/ConvertPdfToPptx/ConvertPdfToPptx.csproj +++ b/Examples/ConvertPdfToPptx/ConvertPdfToPptx.csproj @@ -10,4 +10,8 @@ + + + + diff --git a/Examples/ConvertPdfToPptx/Program.cs b/Examples/ConvertPdfToPptx/Program.cs index efed93f..e01b671 100644 --- a/Examples/ConvertPdfToPptx/Program.cs +++ b/Examples/ConvertPdfToPptx/Program.cs @@ -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"); diff --git a/Examples/ConvertPdfToXlsx/ConvertPdfToXlsx.csproj b/Examples/ConvertPdfToXlsx/ConvertPdfToXlsx.csproj index 6ad95ed..3bb9362 100644 --- a/Examples/ConvertPdfToXlsx/ConvertPdfToXlsx.csproj +++ b/Examples/ConvertPdfToXlsx/ConvertPdfToXlsx.csproj @@ -10,4 +10,8 @@ + + + + diff --git a/Examples/ConvertPdfToXlsx/Program.cs b/Examples/ConvertPdfToXlsx/Program.cs index 5801612..7c79e04 100644 --- a/Examples/ConvertPdfToXlsx/Program.cs +++ b/Examples/ConvertPdfToXlsx/Program.cs @@ -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"); diff --git a/Examples/ConvertRemoteFile/Program.cs b/Examples/ConvertRemoteFile/Program.cs index 0ad2871..aaf02ab 100644 --- a/Examples/ConvertRemoteFile/Program.cs +++ b/Examples/ConvertRemoteFile/Program.cs @@ -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"); diff --git a/Examples/ConvertUrlToPdf/Program.cs b/Examples/ConvertUrlToPdf/Program.cs index b7a6ca4..66151e6 100644 --- a/Examples/ConvertUrlToPdf/Program.cs +++ b/Examples/ConvertUrlToPdf/Program.cs @@ -18,7 +18,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")); Console.WriteLine("Converting web page https://en.wikipedia.org/wiki/Data_conversion to PDF..."); diff --git a/Examples/ConvertWordToPdfAndPng/ConvertWordToPdfAndPng.csproj b/Examples/ConvertWordToPdfAndPng/ConvertWordToPdfAndPng.csproj index 630edb1..019ceb1 100644 --- a/Examples/ConvertWordToPdfAndPng/ConvertWordToPdfAndPng.csproj +++ b/Examples/ConvertWordToPdfAndPng/ConvertWordToPdfAndPng.csproj @@ -11,6 +11,10 @@ + + + + diff --git a/Examples/ConvertWordToPdfAndPng/Program.cs b/Examples/ConvertWordToPdfAndPng/Program.cs index 05fba63..979c11a 100644 --- a/Examples/ConvertWordToPdfAndPng/Program.cs +++ b/Examples/ConvertWordToPdfAndPng/Program.cs @@ -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); diff --git a/Examples/CreatePdfThumbnail/CreatePdfThumbnail.csproj b/Examples/CreatePdfThumbnail/CreatePdfThumbnail.csproj index 630edb1..65c86ef 100644 --- a/Examples/CreatePdfThumbnail/CreatePdfThumbnail.csproj +++ b/Examples/CreatePdfThumbnail/CreatePdfThumbnail.csproj @@ -11,6 +11,10 @@ + + + + diff --git a/Examples/CreatePdfThumbnail/Program.cs b/Examples/CreatePdfThumbnail/Program.cs index d7b085c..678be09 100644 --- a/Examples/CreatePdfThumbnail/Program.cs +++ b/Examples/CreatePdfThumbnail/Program.cs @@ -17,8 +17,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"); - 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 { diff --git a/Examples/DeleteFiles/DeleteFiles.csproj b/Examples/DeleteFiles/DeleteFiles.csproj index c1999eb..49be676 100644 --- a/Examples/DeleteFiles/DeleteFiles.csproj +++ b/Examples/DeleteFiles/DeleteFiles.csproj @@ -11,4 +11,8 @@ + + + + diff --git a/Examples/DeleteFiles/Program.cs b/Examples/DeleteFiles/Program.cs index a94c22d..8363519 100644 --- a/Examples/DeleteFiles/Program.cs +++ b/Examples/DeleteFiles/Program.cs @@ -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..."); diff --git a/Examples/ExceptionHandling/ExceptionHandling.csproj b/Examples/ExceptionHandling/ExceptionHandling.csproj index 15fc122..c187486 100644 --- a/Examples/ExceptionHandling/ExceptionHandling.csproj +++ b/Examples/ExceptionHandling/ExceptionHandling.csproj @@ -11,4 +11,8 @@ + + + + diff --git a/Examples/ExceptionHandling/Program.cs b/Examples/ExceptionHandling/Program.cs index 22ee940..ad38da3 100644 --- a/Examples/ExceptionHandling/Program.cs +++ b/Examples/ExceptionHandling/Program.cs @@ -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)); diff --git a/Examples/FromStreamToStream/Program.cs b/Examples/FromStreamToStream/Program.cs index 2363750..cb94b20 100644 --- a/Examples/FromStreamToStream/Program.cs +++ b/Examples/FromStreamToStream/Program.cs @@ -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 = "

My First Heading

My first paragraph.

"; var stream = new MemoryStream(Encoding.UTF8.GetBytes(htmlString)); diff --git a/Examples/GetConverterInfoDemo/Program.cs b/Examples/GetConverterInfoDemo/Program.cs index 642db2d..1305e0d 100644 --- a/Examples/GetConverterInfoDemo/Program.cs +++ b/Examples/GetConverterInfoDemo/Program.cs @@ -7,7 +7,7 @@ class Program static async Task 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); diff --git a/Examples/HandleRetry/HandleRetry.csproj b/Examples/HandleRetry/HandleRetry.csproj index df0a983..39bdaf3 100644 --- a/Examples/HandleRetry/HandleRetry.csproj +++ b/Examples/HandleRetry/HandleRetry.csproj @@ -10,4 +10,8 @@ + + + + diff --git a/Examples/HandleRetry/Program.cs b/Examples/HandleRetry/Program.cs index cf130b6..23189e2 100644 --- a/Examples/HandleRetry/Program.cs +++ b/Examples/HandleRetry/Program.cs @@ -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) }; diff --git a/Examples/MergeWordDocsToPdf/MergeWordDocsToPdf.csproj b/Examples/MergeWordDocsToPdf/MergeWordDocsToPdf.csproj index 37884cb..59dc4be 100644 --- a/Examples/MergeWordDocsToPdf/MergeWordDocsToPdf.csproj +++ b/Examples/MergeWordDocsToPdf/MergeWordDocsToPdf.csproj @@ -9,4 +9,9 @@ + + + + + diff --git a/Examples/MergeWordDocsToPdf/Program.cs b/Examples/MergeWordDocsToPdf/Program.cs index f57e0b3..68e2e46 100644 --- a/Examples/MergeWordDocsToPdf/Program.cs +++ b/Examples/MergeWordDocsToPdf/Program.cs @@ -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", diff --git a/Examples/README.md b/Examples/README.md new file mode 100644 index 0000000..7caa131 --- /dev/null +++ b/Examples/README.md @@ -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. diff --git a/Examples/RetrieveUserInformation/Program.cs b/Examples/RetrieveUserInformation/Program.cs index 8d95d5b..4773eab 100644 --- a/Examples/RetrieveUserInformation/Program.cs +++ b/Examples/RetrieveUserInformation/Program.cs @@ -14,7 +14,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")); var convertApiUser = await convertApi.GetUserAsync(); Console.WriteLine("Secret={0}", convertApiUser.Secret); diff --git a/Examples/SplitAndMergePdf/Program.cs b/Examples/SplitAndMergePdf/Program.cs index 34280f3..d2431ac 100644 --- a/Examples/SplitAndMergePdf/Program.cs +++ b/Examples/SplitAndMergePdf/Program.cs @@ -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"); diff --git a/Examples/SplitAndMergePdf/SplitAndMergePdf.csproj b/Examples/SplitAndMergePdf/SplitAndMergePdf.csproj index 630edb1..65c86ef 100644 --- a/Examples/SplitAndMergePdf/SplitAndMergePdf.csproj +++ b/Examples/SplitAndMergePdf/SplitAndMergePdf.csproj @@ -11,6 +11,10 @@ + + + + diff --git a/Examples/SplitPDF/Program.cs b/Examples/SplitPDF/Program.cs index 02e65e5..918c01b 100644 --- a/Examples/SplitPDF/Program.cs +++ b/Examples/SplitPDF/Program.cs @@ -4,8 +4,11 @@ class Program { static async Task Main() { - // IMPORTANT: Replace "api-token" with your actual ConvertAPI API token from the dashboard https://www.convertapi.com/a/api-tokens. - var api = new ConvertApi("api-token"); + // Get your API token from the dashboard https://www.convertapi.com/a/api-tokens + var api = new ConvertApi(Environment.GetEnvironmentVariable("CONVERTAPI_TOKEN")); + + // Sample PDF from the files folder, copied next to the program when it builds + var sampleFile = Path.Combine(AppContext.BaseDirectory, "files", "sample.pdf"); // Ensure output directories exist before saving files. // You might want to implement more robust directory handling in production code. @@ -19,7 +22,7 @@ static async Task Main() // 1) Bookmark-Based Splitting // Split the PDF into separate files at each bookmark level. var res1 = await api.ConvertAsync("pdf", "split", - new ConvertApiFileParam("File", "files/sample.pdf"), + new ConvertApiFileParam("File", sampleFile), new ConvertApiParam("SplitByBookmark", "true") ); await res1.SaveFilesAsync("output/bookmark-splits-example"); @@ -27,7 +30,7 @@ static async Task Main() // 2) Pattern-Based Splitting // Split the document into chunks of pages by a repeating pattern (e.g., 2 then 3 pages). var res2 = await api.ConvertAsync("pdf", "split", - new ConvertApiFileParam("File", "files/sample.pdf"), + new ConvertApiFileParam("File", sampleFile), new ConvertApiParam("SplitByPattern", "2,3") ); await res2.SaveFilesAsync("output/pattern-splits-example"); @@ -35,7 +38,7 @@ static async Task Main() // 3) Text-Based Splitting // Split the document each time a specific text pattern (regex) is matched. var res3 = await api.ConvertAsync("pdf", "split", - new ConvertApiFileParam("File", "files/sample.pdf"), + new ConvertApiFileParam("File", sampleFile), new ConvertApiParam("SplitByTextPattern", @"Chapter \d+:") // Example: splits at "Chapter 1:", "Chapter 2:", etc. ); await res3.SaveFilesAsync("output/text-pattern-splits-example"); @@ -43,7 +46,7 @@ static async Task Main() // 4) Extracting Specific Pages // Extract individual pages within a specified range, saving each as a separate file. var res4 = await api.ConvertAsync("pdf", "split", - new ConvertApiFileParam("File", "files/sample.pdf"), + new ConvertApiFileParam("File", sampleFile), new ConvertApiParam("ExtractPages", "5-10") // Example: extracts pages 5, 6, 7, 8, 9, 10 into separate files ); await res4.SaveFilesAsync("output/extract-specific-pages-example"); @@ -51,7 +54,7 @@ static async Task Main() // 5) Splitting by Ranges // Create separate files for specific pages or ranges. var res5 = await api.ConvertAsync("pdf", "split", - new ConvertApiFileParam("File", "files/sample.pdf"), + new ConvertApiFileParam("File", sampleFile), new ConvertApiParam("SplitByRange", "1,3,5-7") // Example: creates files for page 1, page 3, and pages 5-7 ); await res5.SaveFilesAsync("output/split-by-ranges-example"); @@ -59,7 +62,7 @@ static async Task Main() // 6) Merging Output // Use SplitByRange or ExtractPages, but merge all results into a single output file. var res6 = await api.ConvertAsync("pdf", "split", - new ConvertApiFileParam("File", "files/sample.pdf"), + new ConvertApiFileParam("File", sampleFile), new ConvertApiParam("SplitByRange", "2,4-6,8"), // Specify pages/ranges new ConvertApiParam("MergeOutput", "true") // Merge them into one file ); diff --git a/Examples/UploadOnceReuseTwice/Program.cs b/Examples/UploadOnceReuseTwice/Program.cs index 6e5c200..6894e24 100644 --- a/Examples/UploadOnceReuseTwice/Program.cs +++ b/Examples/UploadOnceReuseTwice/Program.cs @@ -20,13 +20,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")); - // Use the sample DOCX that ships with this repository - // Resolve the path relative to the compiled output folder to make it robust - // BaseDirectory typically is: Examples\UploadOnceReuseTwice\bin\{Config}\{TFM}\ - var examplesDir = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "..", "..", "..", "..")); - var sourceFile = Path.Combine(examplesDir, "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"); if (!File.Exists(sourceFile)) { diff --git a/Examples/UploadOnceReuseTwice/README.md b/Examples/UploadOnceReuseTwice/README.md index 2dcf8a7..81010e9 100644 --- a/Examples/UploadOnceReuseTwice/README.md +++ b/Examples/UploadOnceReuseTwice/README.md @@ -14,10 +14,9 @@ How it works - The same `FileId` is passed to `ConvertAsync` twice via `new ConvertApiParam("File", uploaded.FileId)`, so the file is not re-uploaded. Run steps -1) Open ConvertApi.sln. -2) In Program.cs, set your API token instead of "api_token". -3) Set startup project to UploadOnceReuseTwice and run. -4) Output files are saved to your system temp folder; paths are printed to console. +1) Set the CONVERTAPI_TOKEN environment variable to your API token (get one at https://www.convertapi.com/a/authentication). +2) From the repository root, run `dotnet run --project Examples/UploadOnceReuseTwice`. Or open ConvertApi.sln, set UploadOnceReuseTwice as the startup project and run it. +3) Output files are saved to your system temp folder; paths are printed to console. Related docs - https://www.convertapi.com/docx-to-pdf diff --git a/Examples/UploadOnceReuseTwice/UploadOnceReuseTwice.csproj b/Examples/UploadOnceReuseTwice/UploadOnceReuseTwice.csproj index d67784c..ac524ce 100644 --- a/Examples/UploadOnceReuseTwice/UploadOnceReuseTwice.csproj +++ b/Examples/UploadOnceReuseTwice/UploadOnceReuseTwice.csproj @@ -11,4 +11,8 @@ + + + + diff --git a/Examples/WatermarkPdf/Program.cs b/Examples/WatermarkPdf/Program.cs index c51ce20..e5a783d 100644 --- a/Examples/WatermarkPdf/Program.cs +++ b/Examples/WatermarkPdf/Program.cs @@ -14,14 +14,18 @@ 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(), $"watermarked-{Guid.NewGuid()}.pdf"); + // Sample files from Examples/TestFiles, copied next to the program when it builds + var pdfFile = Path.Combine(AppContext.BaseDirectory, "TestFiles", "test-text.pdf"); + var overlayFile = Path.Combine(AppContext.BaseDirectory, "TestFiles", "pdf-overlay.pdf"); + Console.WriteLine("PDF sent for text watermarking..."); var pdfWatermark = await convertApi.ConvertAsync("pdf", "watermark", - new ConvertApiFileParam("File", @"..\..\..\TestFiles\test-text.pdf"), + new ConvertApiFileParam("File", pdfFile), new ConvertApiParam("VerticalAlignment", "bottom"), new ConvertApiParam("Text", "This is ConvertAPI watermark %DATETIME%"), new ConvertApiParam("FontSize", 15)); @@ -30,7 +34,7 @@ var pdfWatermarkOverlay = await convertApi.ConvertAsync("pdf", "watermark-overlay", new ConvertApiFileParam("File", pdfWatermark), - new ConvertApiFileParam("OverlayFile", new FileInfo(@"..\..\..\TestFiles\pdf-overlay.pdf")), + new ConvertApiFileParam("OverlayFile", new FileInfo(overlayFile)), new ConvertApiParam("Style", "stamp"), new ConvertApiParam("Opacity", 50)); diff --git a/Examples/WatermarkPdf/WatermarkPdf.csproj b/Examples/WatermarkPdf/WatermarkPdf.csproj index 15fc122..02bf47c 100644 --- a/Examples/WatermarkPdf/WatermarkPdf.csproj +++ b/Examples/WatermarkPdf/WatermarkPdf.csproj @@ -11,4 +11,9 @@ + + + + + diff --git a/Examples/Workflow/Program.cs b/Examples/Workflow/Program.cs index 5a22283..f09a355 100644 --- a/Examples/Workflow/Program.cs +++ b/Examples/Workflow/Program.cs @@ -18,11 +18,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")); Console.WriteLine("Converting PDF to JPG and compressing result files with ZIP"); - var examplesDir = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "..", "..", "..", "..")); - var fileName = Path.Combine(examplesDir, "TestFiles", "test.pdf"); + // Sample file from Examples/TestFiles, copied next to the program when it builds + var fileName = Path.Combine(AppContext.BaseDirectory, "TestFiles", "test.pdf"); var firstTask = await convertApi.ConvertAsync("pdf", "jpg", new ConvertApiFileParam(fileName)); Console.WriteLine($"Conversions done. Cost: {firstTask.ConversionCost}. Total files created: {firstTask.FileCount()}"); diff --git a/Examples/Workflow/Workflow.csproj b/Examples/Workflow/Workflow.csproj index 630edb1..65c86ef 100644 --- a/Examples/Workflow/Workflow.csproj +++ b/Examples/Workflow/Workflow.csproj @@ -11,6 +11,10 @@ + + + + From e6a4eddb343b56cd5b0028a835b534ef375057cd Mon Sep 17 00:00:00 2001 From: Tomas Rutkauskas Date: Mon, 9 Nov 2026 10:42:00 +0200 Subject: [PATCH 3/3] WatermarkPdf: stop starting the result PDF as a process Process.Start(path) uses UseShellExecute = false on .NET Core and later, so it tries to execute the saved PDF itself and throws Win32Exception on every OS (on macOS: "Permission denied"). The result was already saved and its path printed, so the example now ends there, like the other examples. Co-Authored-By: Claude Opus 5.5 --- Examples/WatermarkPdf/Program.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/Examples/WatermarkPdf/Program.cs b/Examples/WatermarkPdf/Program.cs index e5a783d..5c16da8 100644 --- a/Examples/WatermarkPdf/Program.cs +++ b/Examples/WatermarkPdf/Program.cs @@ -45,8 +45,6 @@ sw.Stop(); Console.WriteLine("Elapsed " + sw.Elapsed); - - Process.Start(saveFiles.ToString()); } //Catch exceptions from asynchronous methods catch (ConvertApiException e)