このチュートリアルでは、C# を使用して PDF にドロップダウン コンポーネントを追加する方法について説明します。また、注釈ライブラリを利用して、C# を使用して PDF にドロップダウンを挿入する簡単な C# プログラムを作成する方法についても説明します。 PDF のドロップダウンは通常、「コンボ ボックス」または「リスト ボックス」フォーム フィールドと呼ばれます。事前定義されたオプションのリストから 1 つのオプションを選択できます。 C# プログラミング言語を使用して PDF にドロップダウンを挿入するには、注釈ライブラリを使用する必要があります。 C# プログラミング言語を使用して PDF でドロップダウンを作成する方法を次に示します。
C# を使用して PDF にドロップダウン コンポーネントを追加する手順
- NuGet から GroupDocs.Annotation for .NET パッケージをインストールします
- GroupDocs.Annotation 名前空間への参照を追加して、PDF にドロップダウンを挿入します
- 入力 PDF パスを使用して Annotator オブジェクトを作成します
- DropdownComponent オブジェクトを初期化し、いくつかのプロパティを設定する
- Annotator クラスの Add メソッドを呼び出し、ドロップダウン Component オブジェクトをそれに渡します
- 結果の PDF パスで Annotator クラスの Save メソッドを呼び出す
これらの手順を実行すると、PDF ドキュメントにドロップダウン フィールドを正常に挿入できるようになります。ドロップダウンでは、指定したオプションのリストから選択できます。追加のソフトウェアをインストールすることなく、前述の手順を使用して、.NET をサポートする任意のシステムで C# を使用して PDF でドロップダウンを作成 できます。 DropdownComponent クラスのプロパティを使用して、必要に応じて PDF にドロップダウンを追加できます。
C# を使用して PDF にドロップダウン コンポーネントを追加するコード
using GroupDocs.Annotation; | |
using GroupDocs.Annotation.Models; | |
using GroupDocs.Annotation.Models.FormatSpecificComponents.Pdf; | |
using System; | |
using System.Collections.Generic; | |
namespace AddDropdowntInPDFusingCSharp | |
{ | |
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// Set License to avoid the limitations of Annotation library | |
License lic = new License(); | |
lic.SetLicense(@"Conholdate.Annotator.lic"); | |
// Create Annotator object with input PDF path | |
using (Annotator annotator = new Annotator("input.pdf")) | |
{ | |
DropdownComponent dropdown = new DropdownComponent | |
{ | |
Options = new List<string> { "Item1", "Item2", "Item3" }, | |
SelectedOption = null, | |
Placeholder = "Choose option", | |
Box = new Rectangle(100, 100, 100, 100), | |
CreatedOn = DateTime.Now, | |
Message = "This is dropdown component", | |
PageNumber = 0, | |
PenColor = 65535, | |
PenStyle = PenStyle.Dot, | |
PenWidth = 3, | |
Replies = new List<Reply> | |
{ | |
new Reply | |
{ | |
Comment = "First comment", | |
RepliedOn = DateTime.Now | |
}, | |
new Reply | |
{ | |
Comment = "Second comment", | |
RepliedOn = DateTime.Now | |
} | |
} | |
}; | |
// Add Dropdown to PDF | |
annotator.Add(dropdown); | |
// Save PDF to disk | |
annotator.Save("result.pdf"); | |
} | |
} | |
} | |
} |
上記の手順に従うことで、C# を使用して問題なく PDF にドロップダウンを簡単に追加できます。 PDF を保存すると、ドロップダウン コンポーネントが追加され、PDF を開くときにドロップダウンからオプションを選択できるようになります。以前、PDF にチェックボックスを追加する記事を公開しました。詳細については、C#を使用してPDFにチェックボックスコンポーネントを追加する方法 を参照してください。