RAD PDF - Interactive Demonstrations

This implementation demonstrates how to use RAD PDF with ASP.NET 6 or ASP.NET 7 or ASP.NET 8 or ASP.NET 9 Razor Pages 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"] - RadPdfNet6Demo</title>

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

Pages\_ViewImports.cshtml


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

Pages\_ViewStart.cshtml


@{
    Layout = "_Layout";
}

Pages\Index.cshtml


@{
@page
@model RadPdfNet6Demo.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 RadPdfNet6Demo.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 Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

using RadPdf;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddSession();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
}
else
{
    app.UseExceptionHandler("/Error");
}


app.UseStaticFiles();

app.UseRouting();

app.UseSession();

app.UseAuthorization();

app.MapRazorPages();

// Create middleware settings (add UseService = false to run RAD PDF without the System Service)
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);

app.Run();

RadPdfNet6Demo.csproj


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

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
  </PropertyGroup>

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

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