SelectPdf for .NET 10 - PDF/A Archiving Standards - C# / .NET 10 Sample

PDF/A is the ISO standard for long term archiving: everything needed to reproduce the document - fonts above all - must be inside the file. Set PdfStandard on the converter options and the produced document carries the conformance metadata and the output intent of that standard.

Level B guarantees visual reproduction, level U adds a Unicode mapping for all text, and level A adds the tagged logical structure, so it is archival and accessible at once. PDF/A-3 is the level that may carry embedded files, which is what hybrid electronic invoices are built on, and PDF/A-4 is the newer level, defined on PDF 2.0.

Archiving and accessibility are separate standards, so PdfStandard and AccessibilityStandard can be combined - see the accessible pdf sample.

Url:


Pdf standard:




Sample code · C#
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using SelectPdf.Universal;

namespace SelectPdf.Samples.Pages
{
    public class PdfAStandardsModel : PageModel
    {
        public void OnGet()
        {
            TxtUrl = "https://selectpdf.com";
            DdlStandard = "PdfA3B";
        }

        [BindProperty]
        public string TxtUrl { get; set; }

        [BindProperty]
        public string DdlStandard { get; set; } = "PdfA3B";

        public List<SelectListItem> Standards { get; } = new List<SelectListItem>
        {
            new SelectListItem { Value = "PdfA", Text = "PDF/A-1b" },
            new SelectListItem { Value = "PdfA2B", Text = "PDF/A-2b" },
            new SelectListItem { Value = "PdfA3B", Text = "PDF/A-3b (allows embedded files)" },
            new SelectListItem { Value = "PdfA3U", Text = "PDF/A-3u (Unicode mapping)" },
            new SelectListItem { Value = "PdfA3A", Text = "PDF/A-3a (accessible - implies tagging)" },
            new SelectListItem { Value = "PdfA4", Text = "PDF/A-4 (PDF 2.0)" },
            new SelectListItem { Value = "PdfA4E", Text = "PDF/A-4e (PDF 2.0, engineering)" },
            new SelectListItem { Value = "PdfA4F", Text = "PDF/A-4f (PDF 2.0, embedded files)" },
            new SelectListItem { Value = "PdfX", Text = "PDF/X-1a:2001 (print production)" },
        };

        public IActionResult OnPost()
        {
            try
            {
                // instantiate a html to pdf converter object
                HtmlToPdf converter = new HtmlToPdf();
                converter.Options.NavigationTimeout = Startup.NavigationTimeout;

                // the standard the produced document conforms to
                converter.Options.PdfStandard =
                    SampleForm.ParseEnum(DdlStandard, PdfStandard.PdfA3B);

                // the accessible levels are tagged documents, and a tagged document declares a title
                // and a language
                if (DdlStandard == "PdfA3A")
                {
                    converter.Options.AccessibilityStandard = PdfUAStandard.UA1;
                    converter.Options.Title = "SelectPdf - archived document";
                    converter.Options.Language = "en-US";
                }

                // create a new pdf document converting an url
                PdfDocument doc = converter.ConvertUrl(TxtUrl);

                // save pdf document
                byte[] pdf = doc.Save();

                // close pdf document
                doc.Close();

                // return resulted pdf document
                FileResult fileResult = new FileContentResult(pdf, "application/pdf");
                fileResult.FileDownloadName = "ArchivedDocument.pdf";
                return fileResult;
            }
            catch (Exception ex)
            {
                // a document that cannot be produced to the chosen standard is refused with the reason;
                // show it on the page rather than as a server error
                ViewData["Message"] = "An error occurred: " + ex.Message;
                return Page();
            }
        }
    }
}