Sample code sharing to annotate a PDF document using C #

Source: Internet
Author: User
This article describes how to use free components in C # to add text annotations to PDF documents, including free text annotations. Free text annotations allow us to customize the style and appearance of it, very practical value

When we organize our documents, we may need to comment on some or a piece of text, how do we do it programmatically? This article describes how to use free components in C # to add text annotations to PDF documents, including free text annotations. Free text annotations allow us to customize its style and appearance, which is of great practical value.

First, download this free version of the spire.pdf. After the component download is installed, Visual Studio creates a C # console project that adds the Bin folder. DLL as a reference and the following namespaces:

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 Document object and add another 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); string text = "HelloWorld"; PointF point = new PointF (;p). Canvas.drawstring (text, font, pdfbrushes.red, point);

Step 3: Add a comment to the text and set the border, color, and position of the note.

Pdftextmarkupannotation annotation1 = new Pdftextmarkupannotation ("Administrator", "Generally speaking, this is the most basic and simplest program in every computer programming language", 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);d oc. SaveToFile ("Result.pdf");

This is after adding a comment:

All code:

Pdfdocument doc = new pdfdocument ();      Pdfpagebase page = doc. Pages.Add ();      Pdffont font = new Pdffont (Pdffontfamily.helvetica, +);      String text = "HelloWorld";      PointF point = new PointF (.);      Page. Canvas.drawstring (text, font, pdfbrushes.red, point);       Pdftextmarkupannotation annotation1 = new Pdftextmarkupannotation ("Administrator", "Generally speaking, this is the most basic and simplest program in every computer programming language", 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 a free text comment

Also, it is relatively easy to add free text annotations to a document.

Step 1: Create a new PDF Document object and add another page.

Pdfdocument doc = new pdfdocument (); Pdfpagebase page = doc. Pages.Add ();

Step 2: Initialize a pdffreetextannotation, and then customize the text of the comment.

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 note, including the 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 addition of free text annotations:

All 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"); 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.