How to rotate a certain page within PDF document in WPF

2016-04-19 01:29:30 Written by  support iceblue
Rate this item
(0 votes)

It seems very easy to rotate the PDF page. However, we often find that the rotation is applied to the whole PDF document when we try to rotate a particular page. Is there an easy and fast way to rotate a certain PDF page within the DPF document?

In the following sections, I will demonstrate how to rotate a certain PDF page within PDF document in WPF with several lines of codes.

The code snippets are as followed:

Step 1: Initialize a new instance of PdfDocument class and load the PDF document from the file.

PdfDocument doc = new PdfDocument();
doc.LoadFromFile("LeavesOfGrass.pdf");

Step 2: For rotation, Spire.PDF enables you to define 0, 90, 180 and 270 degrees. In this example, my PDF file has five pages. I want to rotate the fifth page by 270 degrees and make other pages remain the same.

PdfPageBase page = doc.Pages[4];
page.Rotation = PdfPageRotateAngle.RotateAngle270;

Step 3: Save the PDF document and launch the file.

doc.SaveToFile("RotatedLeavesOfGrass.pdf");
System.Diagnostics.Process.Start("RotatedLeavesOfGrass.pdf");

Effective screenshot:

How to rotate a certain page within PDF document in WPF

How to rotate a certain page within PDF document in WPF

Full Codes:

[C#]
using System.Windows;
using Spire.Pdf;

namespace Rotate_PDF_page
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            PdfDocument doc = new PdfDocument();
            doc.LoadFromFile("LeavesOfGrass.pdf");
            PdfPageBase page = doc.Pages[4];
            page.Rotation = PdfPageRotateAngle.RotateAngle270;
            doc.SaveToFile("RotatedLeavesOfGrass.pdf");
            System.Diagnostics.Process.Start("RotatedLeavesOfGrass.pdf");
        }
    }
}
[VB.NET]
Imports System.Windows
Imports Spire.Pdf

Namespace Rotate_PDF_page
	''' 
	''' Interaction logic for MainWindow.xaml
	''' 
	Public Partial Class MainWindow
		Inherits Window
		Public Sub New()
			InitializeComponent()
		End Sub

		Private Sub button1_Click(sender As Object, e As RoutedEventArgs)
			Dim doc As New PdfDocument()
			doc.LoadFromFile("LeavesOfGrass.pdf")
			Dim page As PdfPageBase = doc.Pages(4)
			page.Rotation = PdfPageRotateAngle.RotateAngle270
			doc.SaveToFile("RotatedLeavesOfGrass.pdf")
			System.Diagnostics.Process.Start("RotatedLeavesOfGrass.pdf")
		End Sub
	End Class
End Namespace

Additional Info

  • tutorial_title: Rotate a certain page within PDF document in WPF
Last modified on Friday, 24 September 2021 09:55