ExpertPdf Anywhere ASP.NET Core samples · .NET 10 and .NET 11

ExpertPdf Anywhere - Convert Current Web Page to Pdf - C# / ASP.NET Core Sample

This sample shows how to use ExpertPdf Html to Pdf Converter to convert the current web page to pdf, preserving the values entered in the page controls before hitting the 'Convert' button.

Sample text field:

Sample drop down:


Sample check box:


Sample text field:
px

Sample text field:
px

C# code of this sample

Pages/CurrentWebPageToPdf.cshtml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Mvc.ViewEngines;
using ExpertPdf.Anywhere.HtmlToPdf;
using ExpertPdf.Anywhere.PdfDocument;

namespace ExpertPdf.Samples.Pages
{
    public class CurrentWebPageToPdfModel : PageModel
    {
        private readonly IViewRenderService _viewRenderService;

        public CurrentWebPageToPdfModel(IViewRenderService viewRenderService)
        {
            _viewRenderService = viewRenderService;
        }

        public void OnGet()
        {
            TxtSampleText = "sample text";
            DdlDropDownList1 = "Value3";

            ChkSampleCheckbox = true;

            TxtSampleText2 = "1000";
            TxtSampleText3 = "800";
        }

        [BindProperty]
        public string TxtSampleText { get; set; }

        [BindProperty]
        public string DdlDropDownList1 { get; set; }

        public List<SelectListItem> DropDownList1Values { get; } = new List<SelectListItem>
        {
            new SelectListItem { Value = "Value1", Text = "Value1" },
            new SelectListItem { Value = "Value2", Text = "Value2" },
            new SelectListItem { Value = "Value3", Text = "Value3" },
            new SelectListItem { Value = "Value4", Text = "Value4" },
        };

        [BindProperty]
        public bool ChkSampleCheckbox { get; set; }

        [BindProperty]
        public string TxtSampleText2 { get; set; }

        [BindProperty]
        public string TxtSampleText3 { get; set; }

        public async Task<IActionResult> OnPost()
        {
            string htmlString = await _viewRenderService.RenderToStringAsync(
                "/Pages/CurrentWebPageToPdf.cshtml", "/Pages/Shared/_Layout.cshtml",
                new CurrentWebPageToPdfModel(_viewRenderService)
            {
                TxtSampleText = this.TxtSampleText,
                DdlDropDownList1 = this.DdlDropDownList1,
                ChkSampleCheckbox = this.ChkSampleCheckbox,
                TxtSampleText2 = this.TxtSampleText2,
                TxtSampleText3 = this.TxtSampleText3,
            });

            // get base url
            string baseUrl = Startup.WebRootPath;

            // instantiate a html to pdf converter object
            PdfConverter converter = new PdfConverter() { AllowLocalAccess = false };

            // create a new pdf document converting a html string
            Document doc = converter.GetPdfDocumentObjectFromHtmlString(htmlString, baseUrl);

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