Set page transitions for existing PDF document in C#/VB.NET

2016-06-15 03:46:12 Written by  support iceblue
Rate this item
(0 votes)

Page transitions give visual sparkle by creating a "PowerPoint-like" effect when switching between pages. Page transitions are useful when you create a slideshow in PDF format. This article will introduce how to add page transition effects to an existing PDF document using Spire.PDF.

Code Snippet:

Step 1: Initialize an object of PdfDocument class and load the sample PDF which you want to add transition effects to.

PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");

Step 2: Create a new PDF document, add sections to it based on the page count of source file and transfer every individual page to a section. Spire.PDF provides PageSettings property in PdfSection class for setting transition effects, that's reason why we should add pages to sections within a new PDF document.

PdfDocument newDoc = new PdfDocument();
for (int i = 0; i < doc.Pages.Count; i++)
{
    PdfSection secetion = newDoc.Sections.Add();
    PdfPageBase page = doc.Pages[i];
    PdfTemplate template = page.CreateTemplate();
    PdfNewPage newpage = secetion.Pages.Add();
    newpage.Canvas.DrawTemplate(template, new PointF(0 - doc.PageSettings.Margins.Left, 0- doc.PageSettings.Margins.Top));
}

Step 3: As demonstrated above, we know that each section in the new document contains a page. Then we could set transition effect for each page through PageSettings.Transition.

PdfSection secetion1 = newDoc.Sections[0];
secetion1.PageSettings.Transition.Style = PdfTransitionStyle.Cover;
secetion1.PageSettings.Transition.Duration = 3;
secetion1.PageSettings.Transition.PageDuration = 2;

PdfSection secetion2 = newDoc.Sections[1];
secetion2.PageSettings.Transition.Style = PdfTransitionStyle.Glitter;
secetion2.PageSettings.Transition.Duration = 3;
secetion2.PageSettings.Transition.PageDuration = 2;

PdfSection secetion3 = newDoc.Sections[2];
secetion3.PageSettings.Transition.Style = PdfTransitionStyle.Fade;
secetion3.PageSettings.Transition.Duration = 3;
secetion3.PageSettings.Transition.PageDuration = 2;

Step 4: Save and launch the file.

newDoc.SaveToFile("Transition.pdf", FileFormat.PDF);
System.Diagnostics.Process.Start("Transition.pdf");

Output:

How to set page transitions for existing PDF document in C#, VB.NET

[C#]
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;


namespace SetPageTransitions
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");

            PdfDocument newDoc = new PdfDocument();
            for (int i = 0; i < doc.Pages.Count; i++)
            {
                PdfSection secetion = newDoc.Sections.Add();
                PdfPageBase page = doc.Pages[i];
                PdfTemplate template = page.CreateTemplate();
                PdfNewPage newpage = secetion.Pages.Add();
                newpage.Canvas.DrawTemplate(template, new PointF(0 - doc.PageSettings.Margins.Left, 0 - doc.PageSettings.Margins.Top));
            }

            PdfSection secetion1 = newDoc.Sections[0];
            secetion1.PageSettings.Transition.Style = PdfTransitionStyle.Cover;
            secetion1.PageSettings.Transition.Duration = 3;
            secetion1.PageSettings.Transition.PageDuration = 2;

            PdfSection secetion2 = newDoc.Sections[1];
            secetion2.PageSettings.Transition.Style = PdfTransitionStyle.Glitter;
            secetion2.PageSettings.Transition.Duration = 3;
            secetion2.PageSettings.Transition.PageDuration = 2;

            PdfSection secetion3 = newDoc.Sections[2];
            secetion3.PageSettings.Transition.Style = PdfTransitionStyle.Fade;
            secetion3.PageSettings.Transition.Duration = 3;
            secetion3.PageSettings.Transition.PageDuration = 2;

            newDoc.SaveToFile("Transition.pdf", FileFormat.PDF);
            System.Diagnostics.Process.Start("Transition.pdf");
        }
    }
}
[VB.NET]
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports System.Drawing


Namespace SetPageTransitions
	Class Program
		Private Shared Sub Main(args As String())
			Dim doc As New PdfDocument()
			doc.LoadFromFile("C:\Users\Administrator\Desktop\sample.pdf")

			Dim newDoc As New PdfDocument()
			For i As Integer = 0 To doc.Pages.Count - 1
				Dim secetion As PdfSection = newDoc.Sections.Add()
				Dim page As PdfPageBase = doc.Pages(i)
				Dim template As PdfTemplate = page.CreateTemplate()
				Dim newpage As PdfNewPage = secetion.Pages.Add()
				newpage.Canvas.DrawTemplate(template, New PointF(0 - doc.PageSettings.Margins.Left, 0 - doc.PageSettings.Margins.Top))
			Next

			Dim secetion1 As PdfSection = newDoc.Sections(0)
			secetion1.PageSettings.Transition.Style = PdfTransitionStyle.Cover
			secetion1.PageSettings.Transition.Duration = 3
			secetion1.PageSettings.Transition.PageDuration = 2

			Dim secetion2 As PdfSection = newDoc.Sections(1)
			secetion2.PageSettings.Transition.Style = PdfTransitionStyle.Glitter
			secetion2.PageSettings.Transition.Duration = 3
			secetion2.PageSettings.Transition.PageDuration = 2

			Dim secetion3 As PdfSection = newDoc.Sections(2)
			secetion3.PageSettings.Transition.Style = PdfTransitionStyle.Fade
			secetion3.PageSettings.Transition.Duration = 3
			secetion3.PageSettings.Transition.PageDuration = 2

			newDoc.SaveToFile("Transition.pdf", FileFormat.PDF)
			System.Diagnostics.Process.Start("Transition.pdf")
		End Sub
	End Class
End Namespace

Additional Info

  • tutorial_title: Set page transitions for existing PDF document in C#, VB.NET
Last modified on Tuesday, 27 August 2024 07:54