Set page transitions for existing PDF document in C#/VB.NET
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:
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"); } } }
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
Enlarge PDF Margins without Changing Page Size in C#/VB.NET
PDF margins are white spaces between body contents and page edge. Unlike Word, margins in PDF document are not easy to be modified as Adobe does not provide any functionality for users to manipulate margins freely. However, you can change the page scaling (enlarge/compress content) or crop page to get fitted margins. In this article, you will learn how to enlarge PDF margins by compressing content.
Step 1: Create a PdfDocument object to load the original PDF document.
PdfDocument origDoc = new PdfDocument(); origDoc.LoadFromFile("sample.pdf");
Step 2: Create another PdfDocument object.
PdfDocument destDoc = new PdfDocument();
Step 3: Set the increments that you want to add to the margins in the existing PDF document.
float top = 50; float bottom = 50; float left = 50; float right = 50;
Step 4: Transfer the compressed content from the original document to the new PDF document.
foreach (PdfPageBase page in origDoc.Pages) { PdfPageBase newPage = destDoc.Pages.Add(page.Size, new PdfMargins(0)); newPage.Canvas.ScaleTransform((page.ActualSize.Width - left - right) / page.ActualSize.Width, (page.ActualSize.Height - top - bottom) / page.ActualSize.Height); newPage.Canvas.DrawTemplate(page.CreateTemplate(), new PointF(left, top)); }
Step 5: Save to file.
destDoc.SaveToFile("result.pdf", FileFormat.PDF);
Original PDF:
Result:
Full Code:
using Spire.Pdf; using Spire.Pdf.Graphics; using System.Drawing; namespace ChangeMargins { class Program { static void Main(string[] args) { PdfDocument origDoc = new PdfDocument(); origDoc.LoadFromFile("sample.pdf"); PdfDocument destDoc = new PdfDocument(); float top = 50; float bottom = 50; float left = 50; float right = 50; foreach (PdfPageBase page in origDoc.Pages) { PdfPageBase newPage = destDoc.Pages.Add(page.Size, new PdfMargins(0)); newPage.Canvas.ScaleTransform((page.ActualSize.Width - left - right) / page.ActualSize.Width, (page.ActualSize.Height - top - bottom) / page.ActualSize.Height); newPage.Canvas.DrawTemplate(page.CreateTemplate(), new PointF(left, top)); } destDoc.SaveToFile("result.pdf", FileFormat.PDF); } } }
Imports Spire.Pdf Imports Spire.Pdf.Graphics Imports System.Drawing Namespace ChangeMargins Class Program Private Shared Sub Main(args As String()) Dim origDoc As New PdfDocument() origDoc.LoadFromFile("sample.pdf") Dim destDoc As New PdfDocument() Dim top As Single = 50 Dim bottom As Single = 50 Dim left As Single = 50 Dim right As Single = 50 For Each page As PdfPageBase In origDoc.Pages Dim newPage As PdfPageBase = destDoc.Pages.Add(page.Size, New PdfMargins(0)) newPage.Canvas.ScaleTransform((page.ActualSize.Width - left - right) / page.ActualSize.Width, (page.ActualSize.Height - top - bottom) / page.ActualSize.Height) newPage.Canvas.DrawTemplate(page.CreateTemplate(), New PointF(left, top)) Next destDoc.SaveToFile("result.pdf", FileFormat.PDF) End Sub End Class End Namespace
C#/VB.NET: Rotate Pages in PDF
In some cases, you might need to rotate PDF pages. For example, when you receive a PDF document that contains disoriented pages, you may wish to rotate the pages so you can read the document easier. In this article, you will learn how to rotate pages in PDF in C# and VB.NET using Spire.PDF for .NET.
Install Spire.PDF for .NET
To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.PDF
Rotate a Specific Page in PDF using C# and VB.NET
Rotation is based on 90-degree increments. You can rotate a PDF page by 0/90/180/270 degrees. The following are the steps to rotate a PDF page:
- Create an instance of PdfDocument class.
- Load a PDF document using PdfDocument.LoadFromFile() method.
- Get the desired page by its index (zero-based) through PdfDocument.Pages[pageIndex] property.
- Get the original rotation angle of the page through PdfPageBase.Rotation property.
- Increase the original rotation angle by desired degrees.
- Apply the new rotation angle to the page through PdfPageBase.Rotation property.
- Save the result document using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using Spire.Pdf; namespace RotatePdfPage { class Program { static void Main(string[] args) { //Create a PdfDocument instance PdfDocument pdf = new PdfDocument(); //Load a PDF document pdf.LoadFromFile("Sample.pdf"); //Get the first page PdfPageBase page = pdf.Pages[0]; //Get the original rotation angle of the page int rotation = (int)page.Rotation; //Rotate the page 180 degrees clockwise based on the original rotation angle rotation += (int)PdfPageRotateAngle.RotateAngle180; page.Rotation = (PdfPageRotateAngle)rotation; //Save the result document pdf.SaveToFile("Rotate.pdf"); } } }
Rotate All Pages in PDF using C# and VB.NET
The following are the steps to rotate all pages in a PDF document:
- Create an instance of PdfDocument class.
- Load a PDF document using PdfDocument.LoadFromFile() method.
- Loop through each page in the document.
- Get the original rotation angle of the page through PdfPageBase.Rotation property.
- Increase the original rotation angle by desired degrees.
- Apply the new rotation angle to the page through PdfPageBase.Rotation property.
- Save the result document using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using Spire.Pdf; namespace RotateAllPdfPages { class Program { static void Main(string[] args) { //Create a PdfDocument instance PdfDocument pdf = new PdfDocument(); //Load a PDF document pdf.LoadFromFile("Sample.pdf"); foreach (PdfPageBase page in pdf.Pages) { //Get the original rotation angle of the page int rotation = (int)page.Rotation; //Rotate the page 180 degrees clockwise based on the original rotation angle rotation += (int)PdfPageRotateAngle.RotateAngle180; page.Rotation = (PdfPageRotateAngle)rotation; } //Save the result document pdf.SaveToFile("RotateAll.pdf"); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Automatically Display Bookmarks or Thumbnails When PDF is Opened in C#/VB.NET
When a user opens a PDF document they see the initial view of the PDF. By default, the Bookmarks Panel or Thumbnails Panel is not shown when the PDF is opened. In this article, we're going to demonstrate how to set document properties so that the Bookmarks Panel or Thumbnails Panel will be open every time the file is launched.
Check the test file below, only the page content is showing when the document is opened.
Code Snippet and Effect:
Step 1: Create a new PDF document and load the test file.
PdfDocument Pdf = new PdfDocument(); Pdf.LoadFromFile("Test.pdf");
Step 2: In the class of ViewerPreferences, there is a PageMode property that specifies how the document should be displayed when opened. Set PageMode as UseOutlines, save the changes to a new PDF file named "ShowBookmarks".
Pdf.ViewerPreferences.PageMode = PdfPageMode.UseOutlines; Pdf.SaveToFile("ShowBookmarks.pdf");
Open the newly-generated file, Bookmarks Panel will be automatically displayed as below:
Step 3: If we set PageMode as UseThumbs, and save the changes to another PDF file named "ShowThumbnails", then we will get following effect if we open this file.
Pdf.ViewerPreferences.PageMode = PdfPageMode.UseThumbs; Pdf.SaveToFile("ShowThumbnails.pdf");
Full Code:
using Spire.Pdf; namespace Bookmarks { class Program { static void Main(string[] args) { PdfDocument Pdf = new PdfDocument(); Pdf.LoadFromFile("Test.pdf"); Pdf.ViewerPreferences.PageMode = PdfPageMode.UseOutlines; Pdf.SaveToFile("ShowBookmarks.pdf"); Pdf.ViewerPreferences.PageMode = PdfPageMode.UseThumbs; Pdf.SaveToFile("ShowThumbnails.pdf"); } } }
Imports Spire.Pdf Namespace Bookmarks Class Program Private Shared Sub Main(args As String()) Dim Pdf As New PdfDocument() Pdf.LoadFromFile("Test.pdf") Pdf.ViewerPreferences.PageMode = PdfPageMode.UseOutlines Pdf.SaveToFile("ShowBookmarks.pdf") Pdf.ViewerPreferences.PageMode = PdfPageMode.UseThumbs Pdf.SaveToFile("ShowThumbnails.pdf") End Sub End Class End Namespace
Duplicate a Page within a PDF Document in C#/VB.NET
In some circumstance where we need to create a copy of the existing pages in our PDF document instead of copying the entire file, in particular, if we have to create hundreds copies of a certain page, it can be tedious to copy the page one after another. This article demonstrates a solution for how to duplicate a page in a PDF document and create multiple copies at a time using Spire.PDF.
In this example, I prepare a sample PDF file that only contains one page and eventually I’ll create ten copies of this page in the same document. Main method would be as follows:
Code Snippet:
Step 1: Create a new PDF document and load the sample file.
PdfDocument pdf = new PdfDocument("Sample.pdf");
Step 2: Get the first page from PDF, get the size of the page. Create a new instance of Pdf Template object based on the content and appearance of the first page.
PdfPageBase page = pdf.Pages[0]; SizeF size = page.Size; PdfTemplate template = page.CreateTemplate();
Step 3: Create a new PDF page with the method Pages.Add() based on the size of the first page, draw the template on the new page at the specified location. Use a for loops to get more copies of this page.
for (int i = 0; i < 10; i++) { page = pdf.Pages.Add(size, new PdfMargins(0)); page.Canvas.DrawTemplate(template, new PointF(0, 0)); }
Step 4: Save the file.
pdf.SaveToFile("Result.pdf");
Output:
Ten copies of the first page have been created in the sample PDF document.
Full Code:
using Spire.Pdf; using Spire.Pdf.Graphics; using System.Drawing; namespace DuplicatePage { class Program { static void Main(string[] args) { PdfDocument pdf = new PdfDocument("Sample.pdf"); PdfPageBase page = pdf.Pages[0]; SizeF size = page.Size; PdfTemplate template = page.CreateTemplate(); for (int i = 0; i < 10; i++) { page = pdf.Pages.Add(size, new PdfMargins(0)); page.Canvas.DrawTemplate(template, new PointF(0, 0)); } pdf.SaveToFile("Result.pdf"); } } }
Imports Spire.Pdf Imports Spire.Pdf.Graphics Imports System.Drawing Namespace DuplicatePage Class Program Private Shared Sub Main(args As String()) Dim pdf As New PdfDocument("Sample.pdf") Dim page As PdfPageBase = pdf.Pages(0) Dim size As SizeF = page.Size Dim template As PdfTemplate = page.CreateTemplate() For i As Integer = 0 To 9 page = pdf.Pages.Add(size, New PdfMargins(0)) page.Canvas.DrawTemplate(template, New PointF(0, 0)) Next pdf.SaveToFile("Result.pdf") End Sub End Class End Namespace
Add image as page background in PDF file
As a powerful PDF component, Spire.PDF supports to work with page setting for PDF well, such as set PDF properties, view preference, and set background color etc. This article will focus on show you how to add image as page background to an existing PDF file in C#.
Make sure Spire.PDF for .NET has been installed correctly and then add Spire.Pdf.dll as reference in the downloaded Bin folder though the below path: "..\Spire.Pdf\Bin\NET4.0\ Spire.Pdf.dll".
Firstly, check the original PDF file without background image.
The following code snippet shows you how to add image as background for PDF in C#.
Step 1: Create a PDF document and load from file.
PdfDocument doc = new PdfDocument(); doc.LoadFromFile("sample.pdf");
Step 2: Get the first page in PDF file
PdfPageBase page = doc.Pages[0];
Step 3: Load the image from file and set it as background image.
Image backgroundImage = Image.FromFile("background.png"); page.BackgroundImage = backgroundImage;
Step 4: Save the document to file and launch it.
doc.SaveToFile("result.pdf"); System.Diagnostics.Process.Start("result.pdf");
Effective Screenshot:
Full Codes:
using Spire.Pdf; using System.Drawing; namespace Addimagebackground { class Program { static void Main(string[] args) { PdfDocument doc = new PdfDocument(); doc.LoadFromFile("sample.pdf"); PdfPageBase page = doc.Pages[0]; Image backgroundImage = Image.FromFile("background.png"); page.BackgroundImage = backgroundImage; doc.SaveToFile("result.pdf"); System.Diagnostics.Process.Start("result.pdf"); } } }
Insert an empty page in a PDF file in C#
Spire.PDF has a function of adding, removing the blank pages in C#. We have already shown you how to remove the blank page in a PDF file. This article will show you how to insert an empty page in a PDF file in C#. By using the Spire.PDF, we can add the blank page to any place in the PDF file you want, such as at the first, the middle of the PDF file or at the end of the PDF file. It is very easy and you only need three lines of code to accomplish this task.
Make sure Spire.PDF for .NET has been installed correctly and then add Spire.Pdf.dll as reference in the downloaded Bin folder though the below path: "..\Spire.Pdf\Bin\NET4.0\Spire.Pdf.dll".
The following code snippet shows you how to insert an empty page in a PDF file. We will show you how to add the empty page at the end of the file and as the second page of the file.
//create a PDF document and load file PdfDocument doc = new PdfDocument(); doc.LoadFromFile("sample.pdf"); //insert blank page at the end of the PDF file doc.Pages.Add(); //insert blank page as the second page doc.Pages.Insert(1); //Save the document to file doc.SaveToFile("result.pdf");
Check the effective screenshots as below:
Add the blank page at the end of the PDF file:
Add the blank page as the second page of the PDF file:
Full codes:
using Spire.Pdf; using System; namespace InsertPage { class Program { static void Main(string[] args) { //create PdfDocument instance and load file PdfDocument doc = new PdfDocument(); doc.LoadFromFile("sample.pdf"); //insert blank page as last page doc.Pages.Add(); doc.SaveToFile("result.pdf"); doc.Close(); System.Diagnostics.Process.Start("result.pdf"); //create PdfDocument instance and load file PdfDocument doc2 = new PdfDocument(); doc2.LoadFromFile("sample.pdf"); //insert blank page as second page doc2.Pages.Insert(1); doc2.SaveToFile("result2.pdf"); doc2.Close(); System.Diagnostics.Process.Start("result2.pdf"); } } }
C#: Get the Number of Pages in a PDF File
Counting the number of pages in a PDF file is essential for various purposes, such as determining document length, organizing content, and evaluating printing requirements. Apart from knowing page count information using PDF viewers, you can also automate the task through programming. In this article, you will learn how to use C# to get the number of pages in a PDF file using Spire.PDF for .NET.
Install Spire.PDF for .NET
To begin with, you need to add the DLL files included in the Spire.PDF for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.PDF
Get the Number of Pages in a PDF File in C#
Spire.PDF for .NET offers the PdfDocument.Pages.Count property to quickly count the number of pages in a PDF file without opening it. The following are the detailed steps.
- Create a PdfDocument object.
- Load a sample PDF file using PdfDocument.LoadFromFile() method.
- Count the number of pages in the PDF file using PdfDocument.Pages.Count property.
- Output the result and close the PDF.
- C#
using Spire.Pdf; namespace GetNumberOfPages { class Program { static void Main(string[] args) { //Create a PdfDocument object PdfDocument pdf = new PdfDocument(); //Load a sample PDF file pdf.LoadFromFile("Contract.pdf"); //Count the number of pages in the PDF int PageNumber = pdf.Pages.Count; Console.WriteLine("The PDF file has {0} pages", PageNumber); //Close the PDF pdf.Close(); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
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.
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(); } } }
C#/VB.NET: Delete Pages from PDF
A PDF document can contain multiple pages with different text content, images, or other objects. Occasionally, users may need to delete certain pages with incorrectly drawn objects or pages that are irrelevant to the topic of the document. This article will demonstrate how to programmatically delete/remove pages from an existing PDF document using Spire.PDF for .NET.
Install Spire.PDF for .NET
To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.PDF
Delete/Remove Pages from PDF
- Create a PdfDocument object.
- Load a sample PDF document using PdfDocument.LoadFromFile() method.
- Get the pages in the PDF document using PdfDocument.Pages property.
- Delete a specified page by index using PdfPageCollection.RemoveAt(int index)method.
- Save the document to another file using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using Spire.Pdf; namespace RemovePage { class Program { static void Main(string[] args) { //Create a PdfDocument object PdfDocument document = new PdfDocument(); //Load a sample PDF document document.LoadFromFile(@"E:\Files\input.pdf"); //Remove the second page document.Pages.RemoveAt(1); //Save the result document document.SaveToFile("RemovePDFPage.pdf"); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.