PDF processing control Aspose.PDF function demonstration: Use C# or VB.NET to rotate PDF pages, text or images

In this article, let us explore the scenarios related to PDF document rotation, it is possible to programmatically rotate an entire page or PDF page content including text or images using C# or VB.NET in a .NET framework based application.

Furthermore, this article will walk through the following PDF page, image or text rotation scenarios with the help of simple and basic PDF rotation functional examples:

  • Rotate all pages of a PDF document using C#
  • Rotate specific pages of PDF using C#
  • Rotate text on PDF document using C#
  • Rotate image on PDF using C#

Currently, the .NET version of Aspose.PDF has been upgraded to v20.10, adding support for ZUGFeRD attachments, optimizing the function of adding signatures, and fixing many bugs such as abnormal conversion from XPS to PDF. Interested friends can click the button below to download the latest version.

Rotate all pages of a PDF document using C#

Let us assume a PDF document created by scanning some documents (scanning all images at a certain angle). Just like all pages are upside down, you need to rotate all pages of a PDF document in a C# or VB.NET application. Likewise, there are probably thousands of related use cases where PDF files need to be rotated. You can rotate all pages of a PDF file as follows:

  • Load the input PDF document
  • iterate over each page
  • Rotate PDF pages using the Rotation property
  • Save the output PDF file

The code snippet below shows how to rotate all pages of a PDF file using C# or VB.NET:

// Load input PDF document
Document document = new Document(dataDir + "Rotate.pdf");

// Iterate through each page of PDF
foreach(Page page in document.Pages)
{
    // Rotate the PDF document on desired angle
    page.Rotate = Rotation.on180;
}

// Save output rotated PDF file
document.Save(dataDir + "Rotated.pdf");

Rotate specific pages of PDF using C#

Rotation in PDF documents is applied at the page level. Therefore, you can also rotate specific pages of the PDF file if you want. You just need to select the page numbers to apply the rotation to. The following steps illustrate how to rotate certain pages of a PDF file:

  • Load the input PDF document
  • Specify the page numbers to rotate
  • iterate over certain page numbers
  • Rotate the page at a specific angle
  • Save the output PDF file

The following code snippet illustrates how to rotate a particular page or specific pages in a PDF document using C# or VB.NET:

// Load input PDF document
Document document = new Document(dataDir + "Rotate.pdf");

// Specify the page numbers you want to apply rotation on
int[] pages = { 1, 3, 7 };

// Iterate through particular pages 
foreach (Page page in document.Pages)
{
    foreach (int match in pages)
    {
        if (page.Number == match)
        {
            // Rotate the page
            page.Rotate = Rotation.on90;
        }
    }
}

// Save rotated PDF document
document.Save(dataDir + "Rotated.pdf");

Rotate text on PDF document using C#

When adding text in a PDF document, you can rotate the text at different angles. This text rotation might be more relevant when adding some watermark text in PDF documents. Let's add some text at specific coordinates on the page, and then rotate the text 45 degrees diagonally.

  • Initialize the object of the Document class
  • Add blank pages to PDF documents
  • Create a new TextFragment object
  • Add text at specific coordinates on the page
  • Append text and save output PDF file

The code snippet below shows how to rotate text in a PDF document using C# or VB.NET:

// Initialize document
Document pdfDocument = new Document();
// Get particular page
Page pdfPage = pdfDocument.Pages.Add();

// Create text fragment
TextFragment tf = new TextFragment("Rotated text");

// Add text at specific loaction on the page
tf.Position = (new Position(200, 600));

// Set text properties
tf.TextState.FontSize = 12;
tf.TextState.Font = FontRepository.FindFont("TimesNewRoman");
tf.TextState.BackgroundColor = Aspose.Pdf.Color.LightGray;
tf.TextState.ForegroundColor = Aspose.Pdf.Color.Red;
tf.TextState.Rotation = 45;
tf.TextState.Underline = true;

// Create TextBuilder object
TextBuilder textBuilder = new TextBuilder(pdfPage);
// Append the text fragment to the PDF page
textBuilder.AppendText(tf);
// Save document
pdfDocument.Save(dataDir + "Text_Rotated.pdf");

Rotate image on PDF using C#

You can rotate images in PDF documents when adding or inserting images in PDF documents. This can be helpful when you want to update or change the image orientation. You can follow the steps below to rotate images on PDF pages:

  • Load the input PDF document
  • Create an instance of the ImageStamp class
  • Set various properties including rotation
  • Save the output PDF file

The following code demonstrates how to programmatically rotate an image or picture in a PDF document using C# or VB.NET:

// Open document
Document pdfDocument = new Document(dataDir + "Image.pdf");

// Create image stamp
ImageStamp imageStamp = new ImageStamp(dataDir + "Image.jpg");
imageStamp.XIndent = 100;
imageStamp.YIndent = 100;
imageStamp.Height = 300;
imageStamp.Width = 300;
imageStamp.Rotate = Rotation.on90;
imageStamp.Opacity = 0.5;
// Add stamp to particular page
pdfDocument.Pages[1].AddStamp(imageStamp);

dataDir = dataDir + "RotatedImage.pdf";
// Save output document
pdfDocument.Save(dataDir);

Guess you like

Origin blog.csdn.net/m0_67129275/article/details/132402521