This sample shows how to swap pages (interchange positions of two pages) in a PDF document.

Please use PdfDocument.SwapPages method to interchange positions of two pages. This method accepts indexes of the pages that are to be swapped. If you need to change positions

of a range of pages then please have a look at PdfDocument.MovePages methods. You may move one page to a new position with help of PdfDocument.MovePage method.

C#
using System.Diagnostics;
using System.Drawing;
using System.IO;

using BitMiracle.Docotic.Pdf;

namespace BitMiracle.Docotic.Pdf.Samples
{
    public static class SwapPages
    {
        public static void Main()
        {
            // NOTE: 
            // When used in trial mode, the library imposes some restrictions.
            // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            // for more information.

            string pathToFile = "SwapPages.pdf";

            using (PdfDocument pdf = new PdfDocument())
            {
                buildTestDocument(pdf);

                pdf.SwapPages(9, 0);
                pdf.SwapPages(8, 1);

                pdf.Save(pathToFile);
            }

            Process.Start(pathToFile);
        }

        private static void buildTestDocument(PdfDocument pdf)
        {
            PdfFont times = pdf.AddFont(PdfBuiltInFont.HelveticaBoldOblique);
            double pageWidth = pdf.GetPage(0).Width;

            PdfPage page = pdf.GetPage(0);
            for (int i = 0; i < 10; i++)
            {
                if (i > 0)
                    page = pdf.AddPage();

                page.Canvas.Font = times;
                page.Canvas.FontSize = 16;

                string titleFormat = string.Format("Page {0}", i + 1);

                double textWidth = page.Canvas.GetTextWidth(titleFormat);
                page.Canvas.DrawString(new PdfPoint((pageWidth - textWidth) / 2, 100), titleFormat);
            }
        }
    }
}
VB
Imports System.Diagnostics
Imports System.Drawing
Imports System.IO

Imports BitMiracle.Docotic.Pdf

Namespace BitMiracle.Docotic.Pdf.Samples
    Public NotInheritable Class SwapPages
        Public Shared Sub Main()
            ' NOTE: 
            ' When used in trial mode, the library imposes some restrictions.
            ' Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            ' for more information.

            Dim pathToFile As String = "SwapPages.pdf"

            Using pdf As New PdfDocument()

                buildTestDocument(pdf)

                pdf.SwapPages(9, 0)
                pdf.SwapPages(8, 1)

                pdf.Save(pathToFile)
            End Using

            Process.Start(pathToFile)

        End Sub

        Private Shared Sub buildTestDocument(pdf As PdfDocument)
            Dim times As PdfFont = pdf.AddFont(PdfBuiltInFont.HelveticaBoldOblique)
            Dim pageWidth As Double = pdf.GetPage(0).Width

            Dim page As PdfPage = pdf.GetPage(0)
            For i As Integer = 0 To 9
                If i > 0 Then
                    page = pdf.AddPage()
                End If

                page.Canvas.Font = times
                page.Canvas.FontSize = 16

                Dim titleFormat As String = String.Format("Page {0}", i + 1)

                Dim textWidth As Double = page.Canvas.GetTextWidth(titleFormat)
                page.Canvas.DrawString(New PdfPoint((pageWidth - textWidth) / 2, 100), titleFormat)
            Next
        End Sub
    End Class
End Namespace