Rotate Pages, Text, Tables, and Images in a PDF Using C#

Table of Contents

PDF documents are widely used for sharing and presenting information. Sometimes, you may need to manipulate the content within a PDF, including rotating pages, text, tables, and images. In this article, we’ll explore how to accomplish these tasks using C# and the iTextSharp library, a popular tool for working with PDFs in .NET applications.

Prerequisites

Before we start, make sure you have the following prerequisites in place:

  1. Visual Studio: You’ll need Visual Studio installed on your machine.
  2. iTextSharp Library: You can install the iTextSharp library via NuGet Package Manager in Visual Studio.

Now, let’s dive into the steps to rotate pages, text, tables, and images in a PDF using C#.

Step 1: Create a New C# Project

  1. Open Visual Studio and create a new C# Console Application project.
  2. Install the iTextSharp library using NuGet Package Manager. Right-click on your project in Solution Explorer, select “Manage NuGet Packages,” and search for “iTextSharp.”

Step 2: Add the Necessary Using Statements

In your C# code, add the following using statements to include the required libraries:

using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

Step 3: Rotate PDF Pages

To rotate pages in a PDF document, you can use the following code snippet. This example rotates all pages in a PDF by 90 degrees clockwise:

string inputFilePath = "input.pdf";
string outputFilePath = "output.pdf";

using (FileStream fs = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write, FileShare.None))
{
    using (Document doc = new Document())
    {
        using (PdfCopy copy = new PdfCopy(doc, fs))
        {
            doc.Open();
            using (PdfReader reader = new PdfReader(inputFilePath))
            {
                for (int page = 1; page <= reader.NumberOfPages; page++)
                {
                    PdfDictionary pageDict = reader.GetPageN(page);
                    pageDict.Put(PdfName.ROTATE, new PdfNumber(90));
                }

                for (int page = 1; page <= reader.NumberOfPages; page++)
                {
                    PdfImportedPage importedPage = copy.GetImportedPage(reader, page);
                    copy.AddPage(importedPage);
                }
            }
        }
    }
}

In this code, we first create an instance of the Document class and a PdfCopy object to create a new PDF file. We then open the input PDF using PdfReader and iterate through its pages, rotating each page by 90 degrees clockwise. Finally, we add the rotated pages to the new PDF.

Step 4: Rotate Text, Tables, and Images

To rotate text, tables, and images within a PDF, you can use the PdfContentByte class. Here’s an example of how to rotate text:

string inputFilePath = "input.pdf";
string outputFilePath = "output.pdf";

using (PdfReader reader = new PdfReader(inputFilePath))
using (FileStream fs = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write, FileShare.None))
using (Document doc = new Document())
using (PdfCopy copy = new PdfCopy(doc, fs))
{
    doc.Open();
    for (int page = 1; page <= reader.NumberOfPages; page++)
    {
        PdfImportedPage importedPage = copy.GetImportedPage(reader, page);
        copy.AddPage(importedPage);

        PdfContentByte cb = copy.DirectContent;
        cb.BeginText();
        cb.SetTextMatrix(1, 0, 0, 1, 100, 100); // Set the rotation and position
        cb.ShowText("Rotated Text");
        cb.EndText();
    }
}

In this code, we add rotated text to each page by specifying the rotation angle and position using SetTextMatrix.

To rotate tables and images, you can follow a similar approach, using PdfContentByte to draw the rotated content on the PDF page.

Conclusion

Manipulating PDFs, including rotating pages, text, tables, and images, can be achieved with C# and the iTextSharp library. By following the steps outlined in this article, you can programmatically rotate content within PDF documents to suit your specific requirements. This can be particularly useful in scenarios where you need to correct orientation issues or create custom PDFs with rotated elements.

Command PATH Security in Go

Command PATH Security in Go

In the realm of software development, security is paramount. Whether you’re building a small utility or a large-scale application, ensuring that your code is robust

Read More »
Undefined vs Null in JavaScript

Undefined vs Null in JavaScript

JavaScript, as a dynamically-typed language, provides two distinct primitive values to represent the absence of a meaningful value: undefined and null. Although they might seem

Read More »