この簡単なチュートリアルでは、** C#を使用してPDFフォームフィールドに入力する方法**について詳しく説明します。 PDFファイルには、テキストボックス、ラジオボタン、コンボボックスなどのさまざまな種類のフォームフィールドを含めることができます。 **プログラムでPDFフォームに入力するにはC#**コードは、テキストボックスフォームフィールドにアクセスし、その値とフォーマットを更新する方法を示しています。
C#を使用してPDFフォームフィールドに入力する手順
- NuGetパッケージマネージャーからAspose.PDFを追加するようにソリューションをセットアップします
- サンプルPDFファイルをテキストボックスフォームフィールドを含むDocumentオブジェクトにロードします
- フィールド名を指定してフォームフィールドへの参照を取得し、TextBoxFieldとしてキャストします
- Valueプロパティを使用して、テキストボックスフィールドに新しいテキストを設定します
- テキストボックスフィールドのテキストの色とテキストの配置を設定します
- 更新されたPDFファイルをフォームフィールドに新しい値で保存します
これらの手順では、構成の詳細を共有して* C#でPDFフォームフィールドに入力*する方法と、ソースPDFファイルを読み込んでその中のフォームフィールドにアクセスするための段階的なプロセスに関する情報を提供します。以降の手順で、テキストボックスフィールドの値とフォーマットが更新されます。最後に、更新されたPDFファイルがディスクに保存されます。
C#を使用してPDFフォームフィールドに入力するコード
using Aspose.Pdf; | |
using Aspose.Pdf.Forms; | |
namespace FillPdfFormFieldsInCSharp | |
{ | |
class Program | |
{ | |
static void Main(string[] args) // Main function for filling form fields | |
{ | |
// Create and load license to fill form fields | |
License licSetFormField = new License(); | |
licSetFormField.SetLicense("Aspose.PDF.lic"); | |
// Load the sample PDF file having form with fields | |
Document pdfWithFormFields = new Document("TextBox_out.pdf"); | |
// Get reference to the text box whose value is to be updated | |
TextBoxField formFieldTextBox = pdfWithFormFields.Form["textbox1"] as TextBoxField; | |
// Set the new value in the selected form field | |
formFieldTextBox.Value = "Here is the new value for the form field"; | |
// Set the selected form field appearance | |
formFieldTextBox.Color = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red); | |
formFieldTextBox.TextVerticalAlignment=VerticalAlignment.Bottom; | |
// Save the resultant PDF after updating the form field | |
pdfWithFormFields.Save("TextBoxUpdated.pdf"); | |
System.Console.WriteLine("Done"); | |
} | |
} | |
} |
- C#を使用したPDFフォームへの入力*操作と、オプションの書式設定手順を示します。 Document.Form [“FieldName”]を使用してフィールド名を指定し、それをそれぞれのフィールドタイプ(TextBoxField、RadioButtonField、またはComboBoxField)にキャストすることで、フォームフィールドにアクセスできます。ターゲットフィールドを選択すると、その値を設定できるだけでなく、外観、境界線、色、コンテンツ、高さ、幅、水平方向および垂直方向のテキストの配置などのプロパティも変更できます。
このチュートリアルでは、* C#を使用してPDFフォームに自動的に入力する方法について説明します。 PDFフォームフィールドをフラット化する方法について知りたい場合は、C#でPDFフォームフィールドをフラット化する方法の記事を参照してください。