Home > Article > Backend Development > Sample code sharing for adding comments to PDF documents using C# (picture)
This article will give an example of how to use free components to add text annotations to PDF documents in C#, including free text annotations. Free text comments allow us to customize its style and appearance, which is of great practical value
When organizing documents, we may need to add comments to explain some or a paragraph of text, so how to achieve this programmatically Woolen cloth? This article will give an example of how to use free components to add text comments to PDF documents in C#, including free text comments. Free text comments allow us to customize its style and appearance, which is of great practical value.
First, download this free version of the component Free Spire.PDF. After the component is downloaded and installed, Visual Studio creates a C# console project, adds the .DLL of the bin folder as a reference and the following namespace:
using System; using System.Drawing; using System.Windows.Forms; using Spire.Pdf; using Spire.Pdf.Graphics; using Spire.Pdf.Annotations;
Now let’s take a look at how to add comments to the new document.
Step 1: Create a new PDF documentObject, and add a new page.
PdfDocument doc = new PdfDocument(); PdfPageBase page = doc.Pages.Add();
Step 2: Add text to the document and set the position, font size, and color of the text.
PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 13); string text = "HelloWorld"; PointF point = new PointF(200, 100); page.Canvas.DrawString(text, font, PdfBrushes.Red, point);
Step 3: Add annotation to the text and set the border, color and position of the annotation.
PdfTextMarkupAnnotation annotation1 = new PdfTextMarkupAnnotation("管理员", "一般来说,这是每一种计算机编程语言中最基本、最简单的程序", text, new PointF(0, 0), font); annotation1.Border = new PdfAnnotationBorder(0.75f); annotation1.TextMarkupColor = Color.Green; annotation1.Location = new PointF(point.X + doc.PageSettings.Margins.Left, point.Y + doc.PageSettings.Margins.Left);
Step 4: Add comments to the page and finally save the document.
(page as PdfNewPage).Annotations.Add(annotation1); doc.SaveToFile("result.pdf");
This is the rendering after adding comments:
Full code:
PdfDocument doc = new PdfDocument(); PdfPageBase page = doc.Pages.Add(); PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 13); string text = "HelloWorld"; PointF point = new PointF(200, 100); page.Canvas.DrawString(text, font, PdfBrushes.Red, point); PdfTextMarkupAnnotation annotation1 = new PdfTextMarkupAnnotation("管理员", "一般来说,这是每一种计算机编程语言中最基本、最简单的程序", text, new PointF(0, 0), font); annotation1.Border = new PdfAnnotationBorder(0.75f); annotation1.TextMarkupColor = Color.Green; annotation1.Location = new PointF(point.X + doc.PageSettings.Margins.Left, point.Y + doc.PageSettings.Margins.Left); (page as PdfNewPage).Annotations.Add(annotation1); doc.SaveToFile("result.pdf"); System.Diagnostics.Process.Start("result.pdf");
Add free text comments
Similarly, adding free text comments to a document is relatively simple.
Step 1: Create a new PDF document object and add a new page.
PdfDocument doc = new PdfDocument(); PdfPageBase page = doc.Pages.Add();
Step 2: Initialize a PdfFreeTextAnnotation, and then customize the text of the annotation.
RectangleF rect = new RectangleF(0, 40, 150, 50); PdfFreeTextAnnotation textAnnotation = new PdfFreeTextAnnotation(rect); textAnnotation.Text = "Free text annotation ";
Step 3: Set the properties of the annotation, including font, fill color, border color and transparency.
PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 10); PdfAnnotationBorder border = new PdfAnnotationBorder(1f); textAnnotation.Font = font; textAnnotation.Border = border; textAnnotation.BorderColor = Color. Purple; textAnnotation.LineEndingStyle = PdfLineEndingStyle.Circle; textAnnotation.Color = Color. Pink; textAnnotation.Opacity = 0.8f;
Step 4: Add comments to the page.
page.AnnotationsWidget.Add(textAnnotation);
Step 5: Save and reopen the document.
doc.SaveToFile("FreeTextAnnotation.pdf", FileFormat.PDF); System.Diagnostics.Process.Start("FreeTextAnnotation.pdf");
This is the rendering of adding free text comments:
Full code:
PdfDocument doc = new PdfDocument(); PdfPageBase page = doc.Pages.Add(); RectangleF rect = new RectangleF(0, 40, 150, 50); PdfFreeTextAnnotation textAnnotation = new PdfFreeTextAnnotation(rect); textAnnotation.Text = "Free text annotation "; PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 10); PdfAnnotationBorder border = new PdfAnnotationBorder(1f); textAnnotation.Font = font; textAnnotation.Border = border; textAnnotation.BorderColor = Color. Purple; textAnnotation.LineEndingStyle = PdfLineEndingStyle.Circle; textAnnotation.Color = Color.Pink; textAnnotation.Opacity = 0.8f; page.AnnotationsWidget.Add(textAnnotation); doc.SaveToFile("FreeTextAnnotation.pdf", FileFormat.PDF); System.Diagnostics.Process.Start("FreeTextAnnotation.pdf");
The above is the detailed content of Sample code sharing for adding comments to PDF documents using C# (picture). For more information, please follow other related articles on the PHP Chinese website!