Orientation management of PDF files in C# and .NET: A step-by-step guide

In today's world, working with PDF files has become an integral part of daily activities in both business and personal life. It is often necessary not only to create, but also to edit PDF documents, including their rotation. In this article, we will look at how to rotate pages in a PDF document using the Sautinsoft PDF .Net library in C# and .NET.

This can be useful for:
  • Correction of the document orientation.
  • Combining pages with different orientations.
  • Easier reading for users using mobile devices.
    Step-by-step guide:
  1. Add SautinSoft.PDF from NuGet.
  2. Create a new PDF document and add a new page.
  3. Rotate the page content.
  4. Create and format text.
  5. Save the document.

Output result:

Complete code

using System;
using SautinSoft.Pdf;
using System.IO;
using SautinSoft.Pdf.Content;
using System.Drawing;
using System.Runtime.CompilerServices;

namespace Sample
{
    class Program
    {
        /// <remarks>
        /// Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/rotate-pdf.php
        /// </remarks>
        static void Main()
        {
            // Before starting this example, please get a free trial key:
            // https://sautinsoft.com/start-for-free/

            // Apply the key here:
            // PdfDocument.SetLicense("...");

            using (var document = new PdfDocument())
            {
                var page = document.Pages.Add();
                // Rotate page content
                page.Rotate = 0;
                // Rotate page view: Landscape or Portrait.
                page.SetMediaBox(page.Size.Height, page.Size.Width);
                var formattedText1 = new PdfFormattedText();
                var text1 = "Hello World";
                formattedText1.FontSize = 15;
                formattedText1.FontFamily = new PdfFontFamily("Calibri");
                formattedText1.Append(text1);
                page.Content.DrawText(formattedText1, new PdfPoint(200, 400));
                document.Save("Output.pdf");
            }
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("Output.pdf") { UseShellExecute = true });
        }
    }
}
      
    

    

Download

Option Infer On

Imports System
Imports SautinSoft.Pdf
Imports System.IO
Imports SautinSoft.Pdf.Content
Imports System.Drawing
Imports System.Runtime.CompilerServices

Namespace Sample
	Friend Class Program
		''' <remarks>
		''' Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/rotate-pdf.php
		''' </remarks>
		Shared Sub Main()
			' Before starting this example, please get a free trial key:
			' https://sautinsoft.com/start-for-free/

			' Apply the key here:
			' PdfDocument.SetLicense("...");

			Using document = New PdfDocument()
				Dim page = document.Pages.Add()
				' Rotate page content
				page.Rotate = 0
				' Rotate page view: Landscape or Portrait.
				page.SetMediaBox(page.Size.Height, page.Size.Width)
				Dim formattedText1 = New PdfFormattedText()
				Dim text1 = "Hello World"
				formattedText1.FontSize = 15
				formattedText1.FontFamily = New PdfFontFamily("Calibri")
				formattedText1.Append(text1)
				page.Content.DrawText(formattedText1, New PdfPoint(200, 400))
				document.Save("Output.pdf")
			End Using
			System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo("Output.pdf") With {.UseShellExecute = True})
		End Sub
	End Class
End Namespace

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.