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

ExpertPdf Anywhere - Convert Multiple Urls into the same Pdf Document - C# / ASP.NET Sample

This sample shows how to convert 3 different urls to the same pdf document using ExpertPdf Anywhere.

Url 1: 

Url 2: 

Url 3: 


C# code of this sample

Pages/ConvertMultipleUrlsToPdf.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 ExpertPdf.Anywhere.HtmlToPdf;
using ExpertPdf.Anywhere.PdfDocument;

namespace ExpertPdf.Samples.Pages
{
    public class ConvertMultipleUrlsToPdfModel : PageModel
    {
        public void OnGet()
        {
        }

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

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

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

        public IActionResult OnPost()
        {
            // create a new pdf document 
            Document doc = new Document();

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

            // add the first url
            HtmlToPdfElement html1 = new HtmlToPdfElement(TxtUrl1) { AllowLocalAccess = false };
            AddElementResult result = page.AddElement(html1);

            // get the last page of the previous conversion
            PdfPage page2 = doc.Pages[result.EndPageIndex];

            // add the second url
            HtmlToPdfElement html2 = new HtmlToPdfElement(result.EndPageBounds.Left,
                result.EndPageBounds.Bottom, TxtUrl2) { AllowLocalAccess = false };
            result = page2.AddElement(html2);

            // get the last page of the previous conversion
            PdfPage page3 = doc.Pages[result.EndPageIndex];

            // add the third url
            HtmlToPdfElement html3 = new HtmlToPdfElement(result.EndPageBounds.Left,
                result.EndPageBounds.Bottom, TxtUrl3) { AllowLocalAccess = false };
            result = page3.AddElement(html3);

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