How to modify the size of the annotation in PDF using C# and VB.NET
The Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can modify the size of the annotation in PDF using C# and VB.NET.
Steps to modify the size of the annotation in PDF programmatically:
- Create a new C# Windows Forms application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
- Include the following namespaces in the Form.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; using Syncfusion.Pdf.Interactive; using Syncfusion.Pdf.Parsing; using System.Drawing;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Graphics Imports Syncfusion.Pdf.Interactive Imports Syncfusion.Pdf.Parsing Imports System.Drawing
- Use the following code snippet to modify the size of the annotation in existing PDF document.
C#
//Loads the document PdfLoadedDocument lDoc = new PdfLoadedDocument("Annotation.pdf"); //Gets the first page of the document PdfLoadedPage page = lDoc.Pages[0] as PdfLoadedPage; //Gets the annotation collection PdfLoadedAnnotationCollection annotations = page.Annotations; foreach(PdfLoadedAnnotation annot in annotations) { //Select the Free text annotation in collection and change its size if(annot is PdfLoadedFreeTextAnnotation) { PdfLoadedFreeTextAnnotation freeTextAnnot = annot as PdfLoadedFreeTextAnnotation; RectangleF annotBounds = freeTextAnnot.Bounds; freeTextAnnot.Bounds = new RectangleF(annotBounds.X, annotBounds.Y, 200, 100); } } //Save and close the document lDoc.Save("ModifiedAnnot.pdf"); lDoc.Close(true); //This will open the PDF file so, the result will be seen in default PDF Viewer Process.Start( "ModifiedAnnot.pdf");
VB.NET
'Loads the document Dim lDoc As PdfLoadedDocument = New PdfLoadedDocument(("Annotation.pdf")) 'Gets the first page of the document Dim page As PdfLoadedPage = CType(lDoc.Pages(0), PdfLoadedPage) 'Gets the annotation collection Dim annotations As PdfLoadedAnnotationCollection = page.Annotations For Each annot As PdfLoadedAnnotation In annotations 'Select the Free text annotation in collection and change its size If (TypeOf annot Is PdfLoadedFreeTextAnnotation) Then Dim freeTextAnnot As PdfLoadedFreeTextAnnotation = CType(annot,PdfLoadedFreeTextAnnotation) Dim annotBounds As RectangleF = freeTextAnnot.Bounds freeTextAnnot.Bounds = New RectangleF(annotBounds.X, annotBounds.Y, 200, 100) End If Next 'Save and close the document lDoc.Save("ModifiedAnnot.pdf") lDoc.Close(True) 'This will open the PDF file so, the result will be seen in default PDF Viewer Process.Start("ModifiedAnnot.pdf")
A complete working sample can be downloaded from ModifyAnnotationSize.zip
The loaded PDF with annotation is shown below:
By executing the program, you will get the PDF document as follows.
Take a moment to peruse the documentation, where you can find other options like adding annotation to PDF, flatten annotation, modifying annotation, and removing annotation with code examples.
Refer here to learn more about PDF annotation.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.