Click or drag to resize

PdfIntegrationProviderOnDocumentSaving Method

Called before a document is saved.

Namespace:  RadPdf.Integration
Assembly:  RadPdfStandard (in RadPdfStandard.dll) Version: 4.1.0.0 (4.1.0.0)
Syntax
public virtual void OnDocumentSaving(
	DocumentSavingEventArgs e
)

Parameters

e
Type: RadPdf.IntegrationDocumentSavingEventArgs
An object (defined by DocumentSavingEventArgs) that contains event data.
Remarks
Inheriting classes can override this method to perform other tasks, modify aspects of saving the document, or cancel the save all together. If this method is overridden in an inheriting class, you must call the base implementation.
Examples
The following example overrides the default PdfIntegrationProvider to create a custom Integration Provider for the web application.
C#
using System;
using System.Web;

using RadPdf.Data.Document.Objects;
using RadPdf.Data.Document.Objects.FormFields;
using RadPdf.Integration;

public class CustomPdfIntegrationProvider : PdfIntegrationProvider
{
    public override void OnDocumentSaving(DocumentSavingEventArgs e)
    {
        base.OnDocumentSaving(e);

        //Look for form field with the name "test" and see if it is null or has no value
        if( string.IsNullOrEmpty(((PdfTextField.)e.Document.Fields.Find("test")).Value) )
        {
            //Cancel save
            e.Cancel = true;
            e.CancelMessage = "The form field \"test\" is not completed.";
        }

        //Add rectangle to first page (saved to PDF, but not shown to viewer until document is re-loaded)
        PdfObject obj = e.Document.Pages[0].CreateObject(PdfObjectCreatable.ShapeRectangle);
        {
            obj.Left = 0;
            obj.Top = 0;
            obj.Width = 100;
            obj.Height = 100;
        }
    }
}
The following Program.cs file registers the above custom Integration Provider. This example assumes that CustomPdfIntegrationProvider is in namespace referenced by your ASP.NET web application.
C#
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();

// Setup WebApplication
var app = builder.Build();
app.UseStaticFiles();
app.UseRouting();
app.UseSession();
app.UseAuthorization();

// Or however you normally process page requests
app.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",

    // Attach the Integration Provider
    IntegrationProvider = new CustomPdfIntegrationProvider()

    // 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
    //UseService = false
};

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

app.Run();
See Also