Flatten PDF annotations in C# .NET
Flattening a PDF annotation means that you remove the annotation object from a document’s internal structure and place its appearance directly on the PDF page as regular page content. This means that the annotation is no longer editable as an annotation, but its rendered content may still be extractable.
Download sampleTo flatten an already existing annotation, use the GetAnnotationCount method to get the total number of annotations on the selected page, loop through all annotations on the selected page, and find the annotation that you want to flatten by any of its parameters, such as its title, type, or subject. Use the methods starting with the GetAnnotation prefix. The full list is available in the get properties guide.
To flatten a newly created annotation, use the following code:
Register the SDK license before running SDK operations. For setup details, refer to the getting started with .NET SDK guide.
using GdPicture14;
LicenseManager license = new LicenseManager();license.RegisterKEY("");using GdPicturePDF gdpicturePDF = new GdPicturePDF();gdpicturePDF.LoadFromFile(@"input.pdf");// Set the origin of the coordinate system.gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft);// Set the measurement unit to centimeters.gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter);// Select the first PDF page.gdpicturePDF.SelectPage(1);// Set the text content and determine its size.float fontSize = 16;string fontResName = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontHelvetica);string text = "Text annotation example.";float textWidth = gdpicturePDF.GetTextWidth(fontResName, fontSize, text) + 0.5f;float textHeight = gdpicturePDF.GetTextHeight(fontResName, fontSize, false) + 0.5f;// Add the free text annotation.int annotIndex = gdpicturePDF.AddFreeTextAnnotation(1, 2, textWidth, textHeight, true, "Annotation Example", "Example", text, fontResName, fontSize, 0, 0, 200, 100, 100, 100, 1f);// Flatten the free text annotation.gdpicturePDF.FlattenAnnotation(annotIndex);gdpicturePDF.SaveToFile(@"output.pdf");Register the SDK license before running SDK operations. For setup details, refer to the getting started with .NET SDK guide.
Imports GdPicture14
Dim license As LicenseManager = New LicenseManager()license.RegisterKEY("")Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("input.pdf") ' Set the origin of the coordinate system. gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft) ' Set the measurement unit to centimeters. gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter) ' Select the first PDF page. gdpicturePDF.SelectPage(1) ' Set the text content and determine its size. Dim fontSize As Single = 16 Dim fontResName As String = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontHelvetica) Dim text As String = "Text annotation example." Dim textWidth As Single = gdpicturePDF.GetTextWidth(fontResName, fontSize, text) + 0.5F Dim textHeight As Single = gdpicturePDF.GetTextHeight(fontResName, fontSize, False) + 0.5F ' Add the free text annotation. Dim annotIndex As Integer = gdpicturePDF.AddFreeTextAnnotation(1, 2, textWidth, textHeight, True, "Annotation Example", "Example", text, fontResName, fontSize, 0, 0, 200, 100, 100, 100, 1F) ' Flatten the free text annotation. gdpicturePDF.FlattenAnnotation(annotIndex) gdpicturePDF.SaveToFile("output.pdf")End Using