ホームページ  >  記事  >  バックエンド開発  >  C# を使用して PDF ドキュメントにコメントを追加するためのサンプル コードの共有 (写真)

C# を使用して PDF ドキュメントにコメントを追加するためのサンプル コードの共有 (写真)

黄舟
黄舟オリジナル
2017-03-25 11:35:521520ブラウズ

この記事では、C#の無料コンポーネントを使用して、フリーテキスト注釈を含むテキスト注釈をPDFドキュメントに追加する方法の例を示します。フリーテキストコメントを使用すると、そのスタイルと外観をカスタマイズできるため、非常に実用的です

ドキュメントを整理するとき、テキストの一部または段落を説明するためにコメントを追加する必要がある場合があります。その場合、それをプログラムで実装するにはどうすればよいでしょうか?この記事では、無料コンポーネントを使用して、C# で PDF ドキュメントにテキスト コメント (フリー テキスト コメントを含む) を追加する方法の例を示します。フリーテキストのコメントを使用すると、そのスタイルと外観をカスタマイズできるため、非常に実用的です。

まず、この無料版コンポーネント Free Spire.PDF をダウンロードします。コンポーネントをダウンロードしてインストールした後、Visual Studio は C# コンソール プロジェクトを作成し、bin フォルダーの .DLL を参照として追加し、次の名前空間を追加します:

using System;
using System.Drawing;
using System.Windows.Forms;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Annotations;

それでは、新しいドキュメントにコメントを追加する方法を見てみましょう。

ステップ 1: 新しい PDF ドキュメントオブジェクトを作成し、新しいページを追加します。

PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();

ステップ 2: ドキュメントにテキストを追加し、テキストの位置、フォント サイズ、色を設定します。

PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 13);
string text = "HelloWorld";
PointF point = new PointF(200, 100);
page.Canvas.DrawString(text, font, PdfBrushes.Red, point);

ステップ 3: テキストに注釈を追加し、注釈の境界線、色、位置を設定します。

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);

ステップ 4: ページにコメントを追加し、最後にドキュメントを保存します。

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

これはコメントを追加した後のレンダリングです:

完全なコード:

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");

自由テキストのコメントを追加します

同様に、文書に自由テキストのコメントを追加するのは比較的簡単です。

ステップ 1: 新しい PDF ドキュメント オブジェクトを作成し、新しいページを追加します。

PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();

ステップ 2: PdfFreeTextAnnotation を初期化し、注釈のテキストをカスタマイズします。

RectangleF rect = new RectangleF(0, 40, 150, 50);
PdfFreeTextAnnotation textAnnotation = new PdfFreeTextAnnotation(rect);
textAnnotation.Text = "Free text annotation ";

ステップ 3: フォント、塗りつぶしの色、境界線の色、透明度など、注釈の プロパティ を設定します。

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;

ステップ 4: ページにコメントを追加します。

page.AnnotationsWidget.Add(textAnnotation);

ステップ 5: ドキュメントを保存して再度開きます。

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

これは、フリーテキスト注釈を追加したレンダリングです:

完全なコード:

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");

以上がC# を使用して PDF ドキュメントにコメントを追加するためのサンプル コードの共有 (写真)の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。