How to rotate the stamp image before adding to PDF WinForms?
The Syncfusion Essential® PDF is a feature-rich and high-performance .NET PDF library that is used to create, read, and edit PDF documents programmatically without Adobe dependencies. Using this library, you can rotate the stamp image before adding to a PDF document using C# and VB.NET.
Steps to rotate the stamp image before adding to a PDF document using C# programmatically
1. Create a new C# console application project.
2. Install the Syncfusion.PDF.WinForms NuGet packages as a reference to your .NET Framework application from NuGet.org.
3. Include the following namespaces in the Program.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; using Syncfusion.Pdf.Interactive; using Syncfusion.Pdf.Parsing;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Graphics Imports Syncfusion.Pdf.Interactive Imports Syncfusion.Pdf.Parsing
4. The following code example shows how to rotate the stamp image before adding to a PDF document using C#.
C#
// Load the PDF document PdfLoadedDocument document = new PdfLoadedDocument("../../Data/Test File.pdf"); // Load the page PdfLoadedPage page = document.Pages[0] as PdfLoadedPage; // Load image from the disk PdfBitmap bitmap = new PdfBitmap("../../Data/logo.png"); // Set the rotation angle int rotation = -270; // Set the points based on the rotation RectangleF rect = CalculateTransformation(rotation, bitmap); // Creates a new pdf rubber stamp annotation RectangleF rectangle = new RectangleF(100, 100, rect.Width, rect.Height); PdfRubberStampAnnotation rubberStampAnnotation = new PdfRubberStampAnnotation(rectangle); rubberStampAnnotation.Text = "Stamp annotation with 180 degree rotated image"; // Save the current graphics state PdfGraphicsState state = rubberStampAnnotation.Appearance.Normal.Graphics.Save(); // Translate the coordinate system rubberStampAnnotation.Appearance.Normal.Graphics.TranslateTransform(rect.X, rect.Y); // Rotate the coordinate system rubberStampAnnotation.Appearance.Normal.Graphics.RotateTransform(rotation); // Draw image rubberStampAnnotation.Appearance.Normal.Graphics.DrawImage(bitmap, new RectangleF(0, 0, bitmap.Width, bitmap.Height)); // Restore the graphics state rubberStampAnnotation.Appearance.Normal.Graphics.Restore(state); // Adds annotation to the page page.Annotations.Add(rubberStampAnnotation); // Saves the document to disk document.Save("TestFile_stamped.pdf"); // Close the document document.Close(true); Process.Start("TestFile_stamped.pdf");
//calculate the translate transformation private static RectangleF CalculateTransformation( int rotation, PdfBitmap bitmap) { RectangleF rectangle = new RectangleF(); if (rotation == -45) { rectangle.X = bitmap.Height; rectangle.Y = bitmap.Width; rectangle.Width = bitmap.Width + bitmap.Height; rectangle.Height = bitmap.Width + bitmap.Height; } else if (rotation == -90) { rectangle.X = 0; rectangle.Y = bitmap.Width; rectangle.Width = bitmap.Height; rectangle.Height = bitmap.Width; } else if (rotation == -180) { rectangle.X = bitmap.Width; rectangle.Y = bitmap.Height; rectangle.Width = bitmap.Width; rectangle.Height = bitmap.Height; } else if (rotation == -270) { rectangle.X = bitmap.Height; rectangle.Y = 0; rectangle.Width = bitmap.Height; rectangle.Height = bitmap.Width; } return rectangle; }
VB.NET
'Load the PDF document Dim document As PdfLoadedDocument = New PdfLoadedDocument("../../Data/Test File.pdf") 'Load the page Dim page As PdfLoadedPage = TryCast(document.Pages(0), PdfLoadedPage) 'Load image from the disk Dim bitmap As PdfBitmap = New PdfBitmap("../../Data/logo.png") 'Set the rotation angle Dim rotation As Integer = -270 'Set the points based on the rotation Dim rect As RectangleF = CalculateTransformation(rotation, bitmap) 'Creates a new pdf rubber stamp annotation Dim rectangle As RectangleF = New RectangleF(100, 100, rect.Width, rect.Height) Dim rubberStampAnnotation As PdfRubberStampAnnotation = New PdfRubberStampAnnotation(rectangle) rubberStampAnnotation.Text = "Stamp annotation with 180 degree rotated image" 'Save the current graphics state Dim state As PdfGraphicsState = rubberStampAnnotation.Appearance.Normal.Graphics.Save() 'Translate the coordinate system rubberStampAnnotation.Appearance.Normal.Graphics.TranslateTransform(rect.X, rect.Y) 'Rotate the coordinate system rubberStampAnnotation.Appearance.Normal.Graphics.RotateTransform(rotation) 'Draw image rubberStampAnnotation.Appearance.Normal.Graphics.DrawImage(bitmap, New RectangleF(0, 0, bitmap.Width, bitmap.Height)) 'Restore the graphics state rubberStampAnnotation.Appearance.Normal.Graphics.Restore(state) 'Adds annotation to the page page.Annotations.Add(rubberStampAnnotation) 'Saves the document to disk document.Save("TestFile_stamped.pdf") 'Close the document document.Close(True) Process.Start("TestFile_stamped.pdf")
Private Function CalculateTransformation(ByVal rotation As Integer, ByVal bitmap As PdfBitmap) As RectangleF Dim rectangle As RectangleF = New RectangleF() If rotation = -45 Then rectangle.X = bitmap.Height rectangle.Y = bitmap.Width rectangle.Width = bitmap.Width + bitmap.Height rectangle.Height = bitmap.Width + bitmap.Height End If If rotation = -90 Then rectangle.X = 0 rectangle.Y = bitmap.Width rectangle.Width = bitmap.Height rectangle.Height = bitmap.Width End If If rotation = -180 Then rectangle.X = bitmap.Width rectangle.Y = bitmap.Height rectangle.Width = bitmap.Width rectangle.Height = bitmap.Height End If If rotation = -270 Then rectangle.X = bitmap.Height rectangle.Y = 0 rectangle.Width = bitmap.Height rectangle.Height = bitmap.Width End If Return rectangle End Function
A complete working sample can be downloaded from RotateImage.zip.
By executing the program, you will get the PDF document as follows.
Take a moment to peruse the documentation. You can add the annotation to PDF document, flatten, and modifying the annotations, importing, or exporting annotation from the FDF or XFDF file, adding comments and review status to the PDF annotation, and printing annotation.
Refer to here to explore a rich set of Syncfusion Essential® PDF features.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from the trial setup or NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without trail message.
See Also:
https://help.syncfusion.com/file-formats/pdf/working-with-annotations
How to modify the size of the annotation in PDF using C# and VB.NET | Syncfusion
How to remove all annotations from an existing PDF document | Syncfusion