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:
- Visual Studio: You’ll need Visual Studio installed on your machine.
- 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
- Open Visual Studio and create a new C# Console Application project.
- 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.