SelectPdf for .NET 10 - Modify Existing Pdf - C# / .NET 10 Sample

This sample shows how to use SelectPdf Library for .NET to load an existing document, add a new page to it and a new text element.

Here is our initial test pdf document:
Test file

Click on the "Create PDF" button below to see the result.

A damaged document (a broken cross-reference table, wrong stream lengths) is repaired when it is opened, as PDF viewers do. Set GlobalProperties.AutoRepair to false to refuse such documents instead. The setting applies to the whole application; this sample sets it for one request only.

PDF document (optional, the test file is used when none is chosen):


Repair a damaged document when it is opened

Sample code · C#
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using SelectPdf.Universal;

namespace SelectPdf.Samples.Pages
{
    public class ModifyExistingPdfModel : PageModel
    {
        public void OnGet()
        {

        }

        // a pdf document to modify (optional; the test file is used when none is uploaded)
        [BindProperty]
        public IFormFile FilePdf { get; set; }

        // repair a damaged document when it is opened (the library default)
        [BindProperty]
        public bool ChkAutoRepair { get; set; } = true;

        public IActionResult OnPost()
        {
            // AutoRepair is a process-wide setting: it applies to every document opened while it is
            // set, in every request. Set it for this request and put the previous value back
            // afterwards; an application normally sets it once, at startup.
            bool previousAutoRepair = GlobalProperties.AutoRepair;
            GlobalProperties.AutoRepair = ChkAutoRepair;

            PdfDocument doc;
            try
            {
                if (FilePdf != null && FilePdf.Length > 0)
                {
                    // load the uploaded pdf document
                    // (a damaged document is repaired, or refused when AutoRepair is off)
                    using (MemoryStream ms = new MemoryStream())
                    {
                        FilePdf.CopyTo(ms);
                        doc = new PdfDocument(new MemoryStream(ms.ToArray()));
                    }
                }
                else
                {
                    // the initial file
                    string file = Path.Combine(Startup.ServerPath, "wwwroot", "files", "doc1.pdf");

                    // load the pdf document
                    doc = new PdfDocument(file);
                }
            }
            catch (PdfDocumentException ex)
            {
                // for example: the document is damaged and AutoRepair is off
                ViewData["Message"] = "An error occurred: " + ex.Message;
                return Page();
            }
            finally
            {
                GlobalProperties.AutoRepair = previousAutoRepair;
            }

            // add a new page to the document
            PdfPage page = doc.AddPage();

            // create a new pdf font (component standard font)
            PdfFont font = doc.Fonts.Add(PdfStandardFont.Helvetica);
            font.Size = 20;

            // create text element and add it to the new page
            PdfTextElement text = new PdfTextElement(100, 100,
                "Sample text added to an existing pdf document.", font);
            page.Add(text);

            // 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 = "Document.pdf";
            return fileResult;

        }
    }
}