Add new page to the pdf document in EJ2 TypeScript Pdfviewer control

8 May 20233 minutes to read

The PDF Viewer library allows you to add a new page to the PDF document using the PDF library.

Step 1: Follow the steps provided in the link to create a simple PDF Viewer sample.

Step 2: Follow the steps provided in the link to create the PDF Viewer web service.

Step 3: Add the following code sample in the PDF Viewer controller code to add a new page in the PDF document using the PDF library.

[HttpPost("Load")]
[Route("[controller]/Load")]
//Post action for loading PDF documents.
public IActionResult Load([FromBody] Dictionary<string, string> jsonObject)
{
    Console.WriteLine("Load called");
    //Initialize the PDF viewer object with the memory cache object.
    PdfRenderer pdfviewer = new PdfRenderer(_cache);
    MemoryStream stream = new MemoryStream();
    object jsonResult = new object();
    if (jsonObject != null && jsonObject.ContainsKey("document"))
    {
        if (bool.Parse(jsonObject["isFileName"]))
        {
            string documentPath = GetDocumentPath(jsonObject["document"]);
            if (!string.IsNullOrEmpty(documentPath))
            {
                byte[] bytes = System.IO.File.ReadAllBytes(documentPath);
                stream = new MemoryStream(bytes);
            }
            else
            {
                return this.Content(jsonObject["document"] + " is not found");
            }
        }
        else
        {
            byte[] bytes = Convert.FromBase64String(jsonObject["document"]);
            stream = new MemoryStream(bytes);
        }
    }

    //Code to create a new page at the end of the loaded pdf document.
    PdfLoadedDocument pdfLoadedDocument = new PdfLoadedDocument(stream);
    pdfLoadedDocument.Pages.Add();
    MemoryStream str = new MemoryStream();
    pdfLoadedDocument.Save(str);
    pdfLoadedDocument.Close(true);
    jsonResult = pdfviewer.Load(str, jsonObject);
    return Content(JsonConvert.SerializeObject(jsonResult));
}

View sample in GitHub