PdfConverter ASP.NET Core Integration Guide

PdfConverter ASP.NET Core Integration Guide

This guide shows how to integrate PdfConverter into an ASP.NET Core application to expose Visio-to-PDF conversion as an HTTP endpoint.

Prerequisites

  • Aspose.Diagram for .NET (version 26.4.0 or later)
  • ASP.NET Core 6.0 or later
  • License applied at application startup (see Step 1)

Step 1: Apply License at Startup

Apply your Aspose license once in Program.cs before the application starts processing requests:

using Aspose.Diagram;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

var metered = new Metered();
metered.SetMeteredKey("publicKey", "privateKey");

app.Run();

Step 2: Create a PDF Export Endpoint (Stream-Based)

Use stream-based conversion to avoid writing temporary files to disk:

using System.IO;
using Aspose.Diagram.LowCode;
using Microsoft.AspNetCore.Mvc;

[ApiController]
[Route("api/diagram")]
public class PdfConverterController : ControllerBase
{
    [HttpPost("export-pdf")]
    public IActionResult ExportToPdf(IFormFile visioFile)
    {
        if (visioFile == null || visioFile.Length == 0)
            return BadRequest("No file uploaded.");

        using var outputStream = new MemoryStream();

        var loadOptions = new LowCodeLoadOptions();
        loadOptions.InputStream = visioFile.OpenReadStream();

        var saveOptions = new LowCodePdfSaveOptions();
        saveOptions.OutputStream = outputStream;

        PdfConverter.Process(loadOptions, saveOptions);

        string fileName = Path.ChangeExtension(visioFile.FileName, ".pdf");
        return File(outputStream.ToArray(), "application/pdf", fileName);
    }
}

Step 3: Add Error Handling

Wrap the conversion in a try/catch to return appropriate HTTP status codes:

using System;
using System.IO;
using Aspose.Diagram.LowCode;
using Microsoft.AspNetCore.Mvc;

[HttpPost("export-pdf")]
public IActionResult ExportToPdf(IFormFile visioFile)
{
    if (visioFile == null || visioFile.Length == 0)
        return BadRequest("No file uploaded.");

    string tempInput = Path.GetTempFileName() + ".vsdx";
    try
    {
        using (var stream = System.IO.File.Create(tempInput))
            visioFile.CopyTo(stream);

        var loadOptions = new LowCodeLoadOptions();
        loadOptions.InputFile = tempInput;

        var saveOptions = new LowCodePdfSaveOptions();
        var ms = new MemoryStream();
        saveOptions.OutputStream = ms;

        PdfConverter.Process(loadOptions, saveOptions);

        string fileName = Path.ChangeExtension(visioFile.FileName, ".pdf");
        return File(ms.ToArray(), "application/pdf", fileName);
    }
    catch (Exception ex)
    {
        return StatusCode(500, $"PDF export failed: {ex.Message}");
    }
    finally
    {
        if (System.IO.File.Exists(tempInput))
            System.IO.File.Delete(tempInput);
    }
}

Troubleshooting

ProblemLikely CauseFix
Evaluation watermark in PDFLicense not applied at startupApply metered.SetMeteredKey() in Program.cs before app.Run()
Upload rejected with 413Request body size limit exceededConfigure MaxRequestBodySize for large Visio files
Empty PDF returnedInput stream not readableEnsure visioFile.OpenReadStream() is called after upload completes
Temp file not deleted on errorMissing finally blockAlways delete temp files in a finally block

See Also