Sample code sharing for adding comments to PDF documents using C# (picture)

黄舟
Release: 2017-03-25 11:35:52
Original
1578 people have browsed it

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;
Copy after login

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();
Copy after login
Copy after login

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);
Copy after login

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);
Copy after login

Step 4: Add comments to the page and finally save the document.

(page as PdfNewPage).Annotations.Add(annotation1);
doc.SaveToFile("result.pdf");
Copy after login

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");
Copy after login

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();
Copy after login
Copy after login

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 ";
Copy after login

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;
Copy after login

Step 4: Add comments to the page.

page.AnnotationsWidget.Add(textAnnotation);
Copy after login

Step 5: Save and reopen the document.

doc.SaveToFile("FreeTextAnnotation.pdf", FileFormat.PDF);
System.Diagnostics.Process.Start("FreeTextAnnotation.pdf");
Copy after login

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");
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template