RAD PDF - Interactive Demonstrations

This implementation demonstrates how to use RAD PDF with ASP.NET 5 or ASP.NET Core using our NuGet

RAD PDF Sample Source Files

Pages\Shared\_Layout.cshtml


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - RadPdfNet5Demo</title>

    @Html.Raw(RadPdf.Web.UI.PdfWebControl.RenderHead())
</head>
<body>
    <div>
        @RenderBody()
    </div>
</body>
</html>

Pages\_ViewImports.cshtml


@using RadPdfNet5Demo
@namespace RadPdfNet5Demo.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

Pages\_ViewStart.cshtml


@{
    Layout = "_Layout";
}

Pages\Index.cshtml


@{
@page
@model RadPdfNet5Demo.Pages.BasicModel

@using RadPdf.Web.UI;

@*
    To use the Client API, the following code must be in the <head> element of your View's HTML:
    @Html.Raw(RadPdf.Web.UI.PdfWebControl.RenderHead())

    See: /Pages/Shared/_Layout.cshtml
*@

@{
    ViewData["Title"] = "Basic Example";

    // Get web control from ViewBag
    PdfWebControl pdfWebControl1 = ViewData["PdfWebControl1"] as PdfWebControl;

    // Set control's properties
    pdfWebControl1.ID = "PdfWebControl1"; // Important if Client API is to be used!
    pdfWebControl1.Height = "600px";
    pdfWebControl1.Width = "100%";
}

<h2>Basic</h2>

@* Render web control to body *@
@Html.Raw(pdfWebControl1.RenderControl())

Pages\Index.cshtml.cs


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

using RadPdf.Lite;
using RadPdf.Web.UI;

namespace RadPdfNet5Demo.Pages
{
    public class BasicModel : PageModel
    {
        public void OnGet()
        {
            string path = @"C:\demo.pdf";

            // Get PDF as byte array from file (or database, browser upload, remote storage, etc)
            byte[] pdfData = System.IO.File.ReadAllBytes(path);

            // Create RAD PDF control
            PdfWebControl pdfWebControl1 = new PdfWebControl(HttpContext);

            // Create document from PDF data
            pdfWebControl1.CreateDocument("Document Name", pdfData);

            // Put control in ViewBag
            ViewData["PdfWebControl1"] = pdfWebControl1;
        }
    }
}

Program.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace RadPdfNet5Demo
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
        }
}

Startup.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

using RadPdf;

namespace RadPdfNet5Demo
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddRazorPages();

            //Default RAD PDF session provider relies on ASP.NET session state.
            //A custom session provider can be used to avoid use of this.
            services.AddSession();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseStaticFiles();

            app.UseRouting();

            // Default RAD PDF session provider relies on ASP.NET session state, so call this before .UseRadPdf()
            // A custom session provider can be used to avoid use of this.
            app.UseSession();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
            });

            // Create middleware settings
            RadPdfCoreMiddlewareSettings settings = new RadPdfCoreMiddlewareSettings()
            {

                // Add SQL Server Connection String, if not using Lite Documents
                // Sample connection string below connects to a SQL Server Express instance on localhost
                // TrustServerCertificate=True is set to avoid a trust exception (e.g. "The certificate chain was issued by an authority that is not trusted.")
                ConnectionString = @"Server=.\SQLExpress;Database=RadPdf;Trusted_Connection=Yes;TrustServerCertificate=True;",

                // Add License Key
                LicenseKey = "DEMO"

                // To run RAD PDF without the System Service, add UseService = false
                // If using Lite Documents without the System Service, a LiteStorageProvider must also be implemented
            };

            // Add RAD PDF's middleware to app
            app.UseRadPdf(settings);
        }
    }
}

RadPdfNet5Demo.csproj


<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="RadPdf" Version="3.48.0" />
  </ItemGroup>

</Project>
RAD PDF is a Red Software product - ©2007-2024 Red Software