页面过渡通过在页面之间切换时创建"类似 PowerPoint"的效果来提供视觉效果。当您以 PDF 格式创建幻灯片时,页面过渡非常有用。本文将介绍如何使用 Spire.PDF 向现有 PDF 文档添加页面过渡效果。
Spire.PDF for .NET 是一款独立 PDF 控件,用于 .NET 程序中创建、编辑和操作 PDF 文档。使用 Spire.PDF 类库,开发人员可以新建一个 PDF 文档或者对现有的 PDF 文档进行处理,且无需安装 Adobe Acrobat。
E-iceblue功能类库Spire 系列文档处理组件均由中国本土团队研发,不依赖第三方软件,不受其他国家的技术或法律法规限制,同时适配国产操作系统如中科方德、中标麒麟等,兼容国产文档处理软件 WPS(如 .wps/.et/.dps 等格式
代码片段:
步骤 1 :初始化 PdfDocument类的对象并加载您想要添加过渡效果的示例 PDF。
PdfDocument doc = new PdfDocument(); doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");
步骤 2 :创建一个新的 PDF 文档,根据源文件的页数向其中添加章节,并将每个单独的页面转移到一个章节。Spire.PDF在PdfSection类中提供了 PageSettings属性来设置过渡效果,这就是为什么我们应该在新的 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)); }
步骤3 :如上所示,我们知道新建的文档中每个部分包含一个页面。然后我们可以通过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;
步骤 4:保存并启动文件。
newDoc.SaveToFile("Transition.pdf", FileFormat.PDF); System.Diagnostics.Process.Start("Transition.pdf");
输出:
[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