Articles in this section

Ensuring PDF/A Format Compliance in ASP.NET Core File Uploads

This article provides a guide on how to check if an uploaded PDF file is in PDF/A format and how to refuse the upload if it does not meet this standard.

Setting Up the Uploader in the View

First, you need to set up the uploader in your Index.cshtml view. Here’s a sample code snippet that initializes the uploader with the necessary settings:

@{
    ViewData["Title"] = "Home Page";
    var asyncSettings = new Syncfusion.EJ2.Inputs.UploaderAsyncSettings {
        SaveUrl = "https://localhost:7060/Uploader/Save",
        RemoveUrl = "https://localhost:7060/Uploader/Remove"
    };
}

<ejs-uploader id="UploadFiles" allowedExtensions=".pdf" asyncSettings="@asyncSettings" removing="onFileRemove"></ejs-uploader>
<script>
    function onFileRemove(args) {
        args.postRawFile = false;
    }
</script>

Handling the File Save Operation

In your UploaderController.cs, you will need to handle the file save operation. The following code snippet demonstrates how to process the uploaded files and check for PDF/A conformance:

[DisableRequestSizeLimit]
public void Save(IList<IFormFile> UploadFiles)
{
    try
    {
        var saveLocation = @"E:\"; // Replace with the path where you want to save the files
        foreach (var file in UploadFiles)
        {
            var filePath = Path.Combine(saveLocation, file.FileName);
            // Load an existing PDF document
            using (FileStream docStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);

                // Get the conformance level of the loaded document
                PdfConformanceLevel conformance = loadedDocument.Conformance;

                // Close the document
                loadedDocument.Close(true);

                if (conformance == Syncfusion.Pdf.PdfConformanceLevel.None)
                {
                    throw new ArgumentException("This is not a valid PDF/A document.");
                }
            }
        }
    }
    catch (Exception e)
    {
        Response.Clear();
        Response.ContentType = "application/json; charset=utf-8";
        Response.StatusCode = 404;
        Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "File failed to upload";
    }
}

In the code snippet above, the Save method iterates through the list of uploaded files. For each file, it opens a FileStream and creates a PdfLoadedDocument object to load the PDF document. It then checks the Conformance property of the loaded document to determine if it is a valid PDF/A file. If the file does not conform to PDF/A standards, an ArgumentException is thrown, and the upload is refused.

Handling Exceptions

If an exception occurs, such as when the uploaded file is not a valid PDF/A file, the catch block is executed. The response is cleared, and a JSON response with a 404 status code is returned, indicating that the file failed to upload.

Sample

For a practical implementation, you can download a sample from Syncfusion that demonstrates the file upload process and PDF/A conformance check: SyncfusionCoreUploader Sample.

Additional References

For more information on working with PDF files in ASP.NET Core MVC and checking for PDF/A conformance, you can refer to the following resources:

Please note that the code example provided uses the Syncfusion PDF library, which is a third-party library. Make sure to include the necessary Syncfusion PDF NuGet packages in your project to use the classes and methods demonstrated in the code snippet.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied