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 e0e4408..678be09 100644 --- a/Examples/CreatePdfThumbnail/Program.cs +++ b/Examples/CreatePdfThumbnail/Program.cs @@ -11,24 +11,24 @@ 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) { //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") ); 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..5c16da8 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)); @@ -41,8 +45,6 @@ sw.Stop(); Console.WriteLine("Elapsed " + sw.Elapsed); - - Process.Start(saveFiles.ToString()); } //Catch exceptions from asynchronous methods catch (ConvertApiException e) 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 @@ + + + +