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 AdvancedPdfSecurityModel : PageModel
    {
        public void OnGet()
        {

        }

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

            // the certificate
            string certFile = Startup.ServerPath + @"\wwwroot\files\selectpdf.pfx";

            // load the pdf document using the advanced security manager
            PdfSecurityManager security = new PdfSecurityManager();
            security.Load(file);

            // encryption algorithm and key length
            security.EncryptionAlgorithm = PdfEncryptionAlgorithm.AES;
            security.EncryptionKeySize = PdfEncryptionKeySize.EncryptKey256Bit;

            // set document passwords
            security.OwnerPassword = "test1";
            security.UserPassword = "test2";

            //set document permissions
            security.CanAssembleDocument = true;
            security.CanCopyContent = true;
            security.CanAccessibilityCopyContent = true;
            security.CanEditAnnotations = true;
            security.CanEditContent = true;
            security.CanFillFields = true;
            security.CanPrint = true;
            security.CanFullQualityPrint = true;

            // add the digital signature
            security.Sign(certFile, "selectpdf");

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

            // close pdf document
            security.Close();

            // return resulted pdf document
            FileResult fileResult = new FileContentResult(pdf, "application/pdf");
            fileResult.FileDownloadName = "Document.pdf";
            return fileResult;
        }
    }
}