Apply PDF Page Transitions in C#

Page transitions display a decorative effect such as a dissolve or wipe when you’re turning pages in a document that is exported to PDF format. Page transitions are especially useful when you create a slideshow in PDF format. And Spire.PDF, a powerful .NET component specially designed for developers enables you to apply page transitions to PDF file.

A solution is introduced here to show how to apply page transitions to PDF using Spire.PDF. Spire.PDF provides you a class called PdfSection. PdfSection has a property called PageSettings. And PageSettings has a property called Transition. You can use this property to apply page transitions.

Step 1: Create a new section.

PdfSection section = doc.Sections.Add();
section.PageSettings.Size = PdfPageSize.A4;

Step 2: Create a new PdfPageTransition instance.

section.PageSettings.Transition = new PdfPageTransition();

Step 3: Set the style of page transition.

section.PageSettings.Transition.Style = PdfTransitionStyle.Fade;

You can assign any value that is defined in PdfTransitionStyle to Style.

Step 4: Set the duration of transition effect in seconds.

section.PageSettings.Transition.Duration = 3;

Step 5: Set the page's display duration.

section.PageSettings.Transition.PageDuration = 2;

Step 6: Add more sections and apply more page transitions.

section = doc.Sections.Add();
section.PageSettings.Size = PdfPageSize.A4;
section.PageSettings.Transition = new PdfPageTransition();
section.PageSettings.Transition.Style = PdfTransitionStyle.Box;
section.PageSettings.Transition.Motion = PdfTransitionMotion.Outward;
section.PageSettings.Transition.Duration = 3;
section.PageSettings.Transition.PageDuration = 2;

You can combine the type of PdfPageTransition with the property Direction and the property Dimension of PdfPageTransition to create new and flexible page transition. For example, in this step, the Style of page transition is PdfTransitionStyle.Box and the Motion of page transition is PdfTransitionMotion.Outward.

At last, save the file.

doc.SaveToFile("result.pdf");

To see page transitions in the PDF, open the PDF file in Full Screen.

pdf page transition

pdf page transition

pdf page transition

Here comes to the full code:

using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;


namespace PageTransitions
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument doc = new PdfDocument();

            PdfSection section = doc.Sections.Add();
            section.PageSettings.Size = PdfPageSize.A4;
            section.PageSettings.Transition = new PdfPageTransition();
            section.PageSettings.Transition.Style = PdfTransitionStyle.Fade;
            section.PageSettings.Transition.Duration = 3;
            section.PageSettings.Transition.PageDuration = 2;

            PdfNewPage page = section.Pages.Add();
            page.BackgroundColor = Color.Blue;
            page.Canvas.DrawString("This is Page One.", new PdfFont(PdfFontFamily.Helvetica, 20f), new PdfSolidBrush(Color.Black), 10, 10);

            page = section.Pages.Add();
            page.BackgroundColor = Color.Green;
            page.Canvas.DrawString("This is Page Two.", new PdfFont(PdfFontFamily.Helvetica, 20f), new PdfSolidBrush(Color.Black), 10, 10);

            section = doc.Sections.Add();
            section.PageSettings.Size = PdfPageSize.A4;
            section.PageSettings.Transition = new PdfPageTransition();
            section.PageSettings.Transition.Style = PdfTransitionStyle.Box;
            section.PageSettings.Transition.Motion = PdfTransitionMotion.Outward;
            section.PageSettings.Transition.Duration = 3;
            section.PageSettings.Transition.PageDuration = 2;

            page = section.Pages.Add();
            page.BackgroundColor = Color.Orange;
            page.Canvas.DrawString("This is Page Three.", new PdfFont(PdfFontFamily.Helvetica, 20f), new PdfSolidBrush(Color.Black), 10, 10);

            page = section.Pages.Add();
            page.BackgroundColor = Color.Brown;
            page.Canvas.DrawString("This is Page Four.", new PdfFont(PdfFontFamily.Helvetica, 20f), new PdfSolidBrush(Color.Black), 10, 10);

            section = doc.Sections.Add();
            section.PageSettings.Size = PdfPageSize.A4;
            section.PageSettings.Transition = new PdfPageTransition();
            section.PageSettings.Transition.Duration = 3;
            section.PageSettings.Transition.Style = PdfTransitionStyle.Dissolve;
            section.PageSettings.Transition.PageDuration = 2;

            page = section.Pages.Add();
            page.BackgroundColor = Color.Orange;
            page.Canvas.DrawString("This is Page Five.", new PdfFont(PdfFontFamily.Helvetica, 20f), new PdfSolidBrush(Color.Black), 10, 10);

            page = section.Pages.Add();
            page.BackgroundColor = Color.Navy;
            page.Canvas.DrawString("This is Page Six.", new PdfFont(PdfFontFamily.Helvetica, 20f), new PdfSolidBrush(Color.Black), 10, 10);

            doc.SaveToFile("result.pdf");
            doc.Close();
        }
    }
}