PDF Creator ASP.NET Core samples · .NET 10 and .NET 11

PDF/A

Creates a PDF/A document for long term archiving: PDF/A-2b, PDF/A-3b, PDF/A-3u or PDF/A-3a (tagged). The PDF/A-3 documents carry the data of the table as an associated CSV file.

C# code of this sample

Pages/PdfADemo.cshtml.cs
using System;
using System.ComponentModel.DataAnnotations;
using System.Drawing;
using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

using ExpertPdf.PdfCreator;

namespace ExpertPdf.PdfCreator.Samples.Pages
{
    public class PdfADemoModel : PageModel
    {
        [BindProperty]
        public int Standard { get; set; } = 1;

        [BindProperty]
        public bool AttachData { get; set; } = true;

        public static readonly string[] StandardNames = { "PDF/A-2b", "PDF/A-3b", "PDF/A-3u", "PDF/A-3a (tagged)" };

        public string Message { get; set; }

        public void OnGet()
        {
        }

        public IActionResult OnPost()
        {
            try
            {
                return File(CreatePdf(), "application/pdf", "PdfA.pdf");
            }
            catch (Exception ex)
            {
                Message = "The PDF document was not created. " + ex.Message;
                return Page();
            }
        }

        private byte[] CreatePdf()
        {
            // LicensingManager.LicenseKey = "your license key";

            PdfStandardSubset standard;
            switch (Standard)
            {
                case 0:
                    standard = PdfStandardSubset.Pdf_A_2b;
                    break;
                case 2:
                    standard = PdfStandardSubset.Pdf_A_3u;
                    break;
                case 3:
                    standard = PdfStandardSubset.Pdf_A_3a;
                    break;
                default:
                    standard = PdfStandardSubset.Pdf_A_3b;
                    break;
            }

            // the standard is set when the document is created: the document then embeds its fonts, writes the PDF/A
            // metadata and leaves out what the standard does not allow
            Document document = new Document(standard);
            document.DocumentInformation.Title = "Sales report";
            document.DocumentInformation.Author = "ExpertPdf PDF Creator";

            // PDF/A-3a is the accessible level: the document is tagged and every element has a tag type or is an artifact.
            // The tags are ignored by the other levels, so the same code serves all of them.
            document.Tagged = standard == PdfStandardSubset.Pdf_A_3a;
            document.Language = "en-US";

            PdfPage page = document.AddPage();
            float width = page.ClientRectangle.Width;
            PdfFont headingFont = document.AddFont(new Font("Arial", 18, FontStyle.Bold, GraphicsUnit.Point), true);
            PdfFont font = document.AddFont(new Font("Arial", 11, GraphicsUnit.Point), true);
            PdfFont boldFont = document.AddFont(new Font("Arial", 11, FontStyle.Bold, GraphicsUnit.Point), true);

            TextElement heading = new TextElement(0, 0, "Sales report - " + StandardNames[Standard], headingFont);
            heading.TagType = PdfTagType.Heading1;
            AddElementResult result = page.AddElement(heading);

            ImageElement logo = new ImageElement(0, result.EndPageBounds.Bottom + 10, 150, Path.Combine(Helper.FilesFolder, @"img\logo.jpg"));
            logo.TagType = PdfTagType.Figure;
            logo.AlternateText = "The ExpertPdf logo";
            result = page.AddElement(logo);

            TextElement intro = new TextElement(0, result.EndPageBounds.Bottom + 10, width,
                "A PDF/A document is self-contained: its fonts, images and color profile are inside the file, so that it " +
                "looks the same when it is opened in many years.", font);
            intro.TagType = PdfTagType.Paragraph;
            result = page.AddElement(intro);

            // the table shows the data of the CSV file that a PDF/A-3 document carries as an associated file
            string csvFile = Path.Combine(Helper.FilesFolder, "sales-data.csv");
            string[] lines = System.IO.File.ReadAllLines(csvFile);
            float y = result.EndPageBounds.Bottom + 15;
            float columnWidth = width / 4;
            document.BeginTag(PdfTagType.Table);
            for (int r = 0; r < lines.Length; r++)
            {
                if (lines[r].Trim().Length == 0)
                    continue;
                string[] cells = lines[r].Split(',');
                document.BeginTag(PdfTagType.TableRow);
                for (int c = 0; c < cells.Length; c++)
                {
                    RectangleElement border = new RectangleElement(c * columnWidth, y, columnWidth, 20);
                    border.Artifact = true;
                    page.AddElement(border);

                    TextElement cell = new TextElement(c * columnWidth + 4, y + 4, columnWidth - 8, cells[c], r == 0 ? boldFont : font);
                    cell.TagType = r == 0 ? PdfTagType.TableHeader : PdfTagType.TableDataCell;
                    page.AddElement(cell);
                }
                document.EndTag();
                y += 20;
            }
            document.EndTag();

            // PDF/A-3 allows files of any type inside the document, associated with its content (PDF/A-2 does not)
            if (AttachData && standard != PdfStandardSubset.Pdf_A_2b)
                document.AddAssociatedFile(csvFile, AttachmentRelationship.Data, "text/csv", "The sales figures shown in the table");

            byte[] pdf = document.Save();
            document.Close();
            return pdf;
        }
    }
}