C# を使用して PDF ドキュメントにコメントを追加するためのサンプル コードの共有 (写真)
この記事では、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 サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック

C# を使用した Active Directory のガイド。ここでは、Active Directory の概要と、C# での動作方法について、構文と例とともに説明します。

C# データ グリッド ビューのガイド。ここでは、SQL データベースまたは Excel ファイルからデータ グリッド ビューをロードおよびエクスポートする方法の例について説明します。
