using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

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

        }

        public IActionResult OnPost()
        {
            // the initial file
            string file1 = Startup.ServerPath + @"\wwwroot\files\doc.pdf";

            // load the pdf document
            PdfDocument doc1 = new PdfDocument(file1);

            // create a new pdf document
            PdfDocument doc = new PdfDocument();

            // add 2 pages to the new document
            if (doc1.Pages.Count > 2)
            {
                doc.AddPage(doc1.Pages[1]);
                doc.AddPage(doc1.Pages[2]);
            }

            // 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;

        }
    }
}