DioDocs for PDF
フォーム
機能 > フォーム
このトピックの内容:

AcroForm とも呼ばれる PDF ベースの入力可能フォームは、テキストボックス、ボタン、チェックボックスなどのフィールドを含む対話式のフォームです。これらのフィールドには、プログラムでデータを設定したり、ユーザーからの入力情報を受け取る手段として手動でデータを設定することができます。AcroForm の詳細については、PDF 仕様 1.7(12.7 項)を参照してください。

DioDocs for PDF では、AcroForm クラスを使用して AcroForm を作成し、AcroForm クラスの Fields プロパティを使用して、よく使用される AcroForm のプロパティを定義することができます。このライブラリを使用して、さまざまなフォームフィールドを追加、取得、変更、および削除することができます。AcroForm では、次のフィールドを使用できます。

FormFields

AcroForm フィールドの追加

DioDocs for PDF を使用して PDF ドキュメントに AcroForm フィールドを追加するには

  1. フォームに追加するフィールドに対応するクラスのインスタンスを作成します。
    たとえば、TextField クラスです。
  2. フィールドの基本プロパティを設定します。
    コードでは、Value プロパティを使用してフィールドに値を設定できます。
  3. Add メソッドを使用して、フォームにフィールドを追加します。
C#
コードのコピー
public void CreatePDF(Stream stream)
{
    var doc = new GcPdfDocument();
    var page = doc.NewPage();
    var g = page.Graphics;
    TextFormat tf = new TextFormat();
    tf.Font = StandardFonts.Times;
    tf.FontSize = 14;
    PointF ip = new PointF(72, 72);
    float fldOffset = 72 * 2;
    float fldHeight = tf.FontSize * 1.2f;
    float dY = 32;

    // テキストフィールドを追加します
    g.DrawString("テキストフィールド:", tf, ip);
    var fldText = new TextField();
    fldText.Value = "TextFieldの初期値";
    fldText.Widget.Page = page;
    fldText.Widget.Rect = new RectangleF(ip.X + fldOffset, ip.Y, 72 * 3, fldHeight);
    fldText.Widget.TextFormat.Font = tf.Font;
    fldText.Widget.TextFormat.FontSize = tf.FontSize;
    doc.AcroForm.Fields.Add(fldText);
    ip.Y += dY;

    // チェックボックスを追加します
    g.DrawString("チェックボックス:", tf, ip);
    var fldCheckbox = new CheckBoxField();
    fldCheckbox.Value = true;
    fldCheckbox.Widget.Page = page;
    fldCheckbox.Widget.Rect = new RectangleF(ip.X + fldOffset, ip.Y, fldHeight, fldHeight);
    doc.AcroForm.Fields.Add(fldCheckbox);
    ip.Y += dY;

    // ドキュメントを保存します
    doc.Save(stream);
}
先頭に戻る

フィールドの書式設定

TextField、ComboTextField、および ComboBoxField の書式をパーセンテージ、数値、日付、時刻、およびその他の特別な書式に設定するには、TextFieldCombTextField、および ComboBoxField クラスの次のメソッドを使用します。

メソッド 説明

SetPercentFormat

指定された書式に対応する ActionJavaScript オブジェクトを使用して、フィールドの Format イベントと KeyPress イベントを初期化します。 このメソッドは、フィールド注釈のフィールド値や外観ストリームには影響しません。
SetNumberFormat 指定された書式に対応する ActionJavaScript オブジェクトを使用して、フィールドの Format イベントと KeyPress イベントを初期化します。 このメソッドは、フィールド注釈のフィールド値や外観ストリームには影響しません。
SetDateFormat 指定された書式に対応する ActionJavaScript オブジェクトを使用して、フィールドの Format イベントと KeyPress イベントを初期化します。 このメソッドは、フィールド注釈のフィールド値や外観ストリームには影響しません。
SetTimeFormat 指定された書式に対応する ActionJavaScript オブジェクトを使用して、フィールドの Format イベントと KeyPress イベントを初期化します。 このメソッドは、フィールド注釈のフィールド値や外観ストリームには影響しません。
SetSpecialFormat 指定された書式に対応する ActionJavaScript オブジェクトを使用して、フィールドの Format イベントと KeyPress イベントを初期化します。 このメソッドは、フィールド注釈のフィールド値や外観ストリームには影響しません。
SetPercentValue フィールドの Format イベントと KeyPress イベントを初期化し、フィールド値を設定し、フィールド注釈の外観ストリームを生成します。 渡された値は、対応する書式を使用して文字列に変換されます。
SetNumberValue フィールドの Format イベントと KeyPress イベントを初期化し、フィールド値を設定し、フィールド注釈の外観ストリームを生成します。 渡された値は、対応する書式を使用して文字列に変換されます。
SetDateValue フィールドの Format イベントと KeyPress イベントを初期化し、フィールド値を設定し、フィールド注釈の外観ストリームを生成します。 渡された値は、対応する書式を使用して文字列に変換されます。
SetTimeValue フィールドの Format イベントと KeyPress イベントを初期化し、フィールド値を設定し、フィールド注釈の外観ストリームを生成します。 渡された値は、対応する書式を使用して文字列に変換されます。
SetSpecialFormatValue フィールドの Format イベントと KeyPress イベントを初期化し、フィールド値を設定し、フィールド注釈の外観ストリームを生成します。 渡された値は、対応する書式を使用して文字列に変換されます。

TextField、ComboTextField、および ComboBoxField の書式を設定する方法については、次のサンプルコードを参照してください。

C#
コードのコピー
// TextField にパーセント書式を設定します。
private static TextField AddPercentFormat(Page p,
    _Rect rect,
    int decimalPlaces,
    TextField.NumberSeparatorStyle separatorStyle)
{
    TextField result = new TextField();
    p.Doc.AcroForm.Fields.Add(result);

    result.Widget.Page = p;
    result.Widget.Rect = rect;
    result.Widget.Border.Width = 1;
    result.SetPercentFormat(decimalPlaces, separatorStyle);

    return result;
}

// CombTextField の数値書式を設定します。
private static CombTextField AddNumberFormat(Page p,
    _Rect rect,
    int decimalPlaces,
    CombTextField.NumberSeparatorStyle separatorStyle,
    CombTextField.NumberNegativeStyle negativeStyle,
    string currencySymbol,
    CombTextField.CurrencySymbolStyle currencySymbolStyle)
{
    CombTextField result = new CombTextField();
    p.Doc.AcroForm.Fields.Add(result);

    result.Widget.Page = p;
    result.Widget.Rect = rect;
    result.Widget.Border.Width = 1;
    result.SetNumberFormat(decimalPlaces, separatorStyle, negativeStyle, currencySymbol, currencySymbolStyle);

    return result;
}

// TextFieldに日付書式を設定します。
private static TextField AddDateFormat(Page p,
    _Rect rect,
    DateTime v,
    string format)
{
    TextField result = new TextField();
    p.Doc.AcroForm.Fields.Add(result);

    result.Widget.Page = p;
    result.Widget.Rect = rect;
    result.Widget.Border.Width = 1;
    result.SetDateValue(v, format);

    return result;
}

// ComboBoxField に時刻書式を設定します。
private static ComboBoxField AddTimeFormat(Page p,
    _Rect rect,
    DateTime v,
    string format)
{
    ComboBoxField result = new ComboBoxField();
    p.Doc.AcroForm.Fields.Add(result);

    result.Widget.Page = p;
    result.Widget.Rect = rect;
    result.Widget.Border.Width = 1;
    result.SetTimeValue(v, format);

    return result;
}

// TextField に特別な書式を設定します。
private static TextField AddSpecialFormat(Page p,
    _Rect rect,
    string v,
    TextField.SpecialFormat format)
{
    TextField result = new TextField();
    p.Doc.AcroForm.Fields.Add(result);

    result.Widget.Page = p;
    result.Widget.Rect = rect;
    result.Widget.Border.Width = 1;
    result.SetSpecialFormatValue(v, format);            

    return result;
}

static void Main(string[] args)
{
    // GcPdfDocument を初期化します。
    GcPdfDocument doc = new GcPdfDocument();

    // 圧縮レベルを設定します。
    doc.CompressionLevel = System.IO.Compression.CompressionLevel.NoCompression;

    // ドキュメントに空白ページを追加します。
    var p = doc.NewPage();

    // GcPdfGraphics を初期化します。
    var g = p.Graphics;

    // SetPercentFormat 文字列を描画します。
    g.DrawString("SetPercentFormat", StandardFonts.Helvetica, 14, Color.Black, new _Point(10, 10));

    // TextFieldにパーセント書式を設定します。
    AddPercentFormat(p, new _Rect(10, 40, 60, 20), 2, TextField.NumberSeparatorStyle.Comma);
    AddPercentFormat(p, new _Rect(80, 40, 60, 20), 0, TextField.NumberSeparatorStyle.CommaDot);
    AddPercentFormat(p, new _Rect(150, 40, 60, 20), 3, TextField.NumberSeparatorStyle.DotComma);
    AddPercentFormat(p, new _Rect(220, 40, 60, 20), 2, TextField.NumberSeparatorStyle.Dot);
    AddPercentFormat(p, new _Rect(290, 40, 60, 20), 2, TextField.NumberSeparatorStyle.ApostropheDot);

    // SetNumberFormat 文字列を描画します。
    g.DrawString("SetNumberFormat", StandardFonts.Helvetica, 14, Color.Black, new _Point(10, 70));

    // CombTextFields に数値書式を設定します。
    AddNumberFormat(p, new _Rect(10, 100, 60, 20), 2, CombTextField.NumberSeparatorStyle.Comma,
        CombTextField.NumberNegativeStyle.None, null, CombTextField.CurrencySymbolStyle.BeforeWithSpace);
    AddNumberFormat(p, new _Rect(80, 100, 60, 20), 0, CombTextField.NumberSeparatorStyle.CommaDot,
        CombTextField.NumberNegativeStyle.UseRedText, "R", CombTextField.CurrencySymbolStyle.BeforeWithSpace);
    AddNumberFormat(p, new _Rect(150, 100, 60, 20), 3, CombTextField.NumberSeparatorStyle.DotComma,
        CombTextField.NumberNegativeStyle.ShowParentheses, "\u20ac", CombTextField.CurrencySymbolStyle.AfterWithSpace);
    AddNumberFormat(p, new _Rect(220, 100, 60, 20), 2, CombTextField.NumberSeparatorStyle.Dot,
        CombTextField.NumberNegativeStyle.ShowParentheses, "TL", CombTextField.CurrencySymbolStyle.AfterNoSpace);
    AddNumberFormat(p, new _Rect(290, 100, 60, 20), 2, CombTextField.NumberSeparatorStyle.ApostropheDot,
        CombTextField.NumberNegativeStyle.ShowParentheses | CombTextField.NumberNegativeStyle.UseRedText, "TL", CombTextField.CurrencySymbolStyle.AfterWithSpace);

    // SetDateFormat 文字列を描画します。
    g.DrawString("SetDateFormat", StandardFonts.Helvetica, 14, Color.Black, new _Point(10, 130));

    // TextField に日付書式を設定します。
    AddDateFormat(p, new _Rect(10, 160, 60, 20), DateTime.Now, "dd-mm-yyyy");
    AddDateFormat(p, new _Rect(80, 160, 60, 20), DateTime.Now, "d-m-yy");
    AddDateFormat(p, new _Rect(150, 160, 60, 20), DateTime.Now, "yyyy/mmmm/dd");

    // SetTimeFormat 文字列を描画します。
    g.DrawString("SetTimeFormat", StandardFonts.Helvetica, 14, Color.Black, new _Point(10, 200));

    // ComboboxField に時刻書式を設定します。
    DateTime dt = new DateTime(2000, 1, 1, 1, 2, 3);
    AddTimeFormat(p, new _Rect(10, 230, 60, 20), dt, "HH:MM");
    AddTimeFormat(p, new _Rect(80, 230, 60, 20), dt, "h:MM tt");
    AddTimeFormat(p, new _Rect(150, 230, 60, 20), dt, "HH:MM:ss");

    // 特殊な形式の文字列を描画します。
    g.DrawString("SetSpecialFormat", StandardFonts.Helvetica, 14, Color.Black, new _Point(10, 270));

    // TextField に特別な形式を設定します。
    AddSpecialFormat(p, new _Rect(10, 300, 60, 20), "35004", TextField.SpecialFormat.ZipCode);
    AddSpecialFormat(p, new _Rect(80, 300, 60, 20), "84606-6580", TextField.SpecialFormat.ZipCode4);
    AddSpecialFormat(p, new _Rect(150, 300, 60, 20), "", TextField.SpecialFormat.Phone);
    AddSpecialFormat(p, new _Rect(220, 300, 60, 20), "", TextField.SpecialFormat.SSN);

    // ドキュメントを保存します。
    doc.Save("FieldFormat.pdf");
}

 

メモ:外観に影響するフィールドプロパティが変更されると、「SetXXXMethods」 によって生成された外観ストリームが失われるため、これらのメソッドを最後に呼び出す必要があります。

先頭に戻る

AcroForm フィールドの変更

PDF ドキュメント内のフォームフィールドを変更するには、そのインデックスを指定して特定のフォームフィールドを取得し、Value プロパティを使用してフィールドに新しい値を設定します。このプロパティは、指定された文字列値をフィールドに設定します。

C#
コードのコピー
doc.AcroForm.Fields[0].Value = "Sample Text";
先頭に戻る

AcroForm フィールドの削除

PDF ドキュメントの特定のフォームフィールドを削除するには、RemoveAt メソッドを使用し、インデックス値を指定してフィールドを削除します。このほかに、Clear メソッドを使用して、ドキュメント内のすべての AcroForm フィールドを削除することもできます。

C#
コードのコピー
// 特定のフィールドを削除します
doc.AcroForm.Fields.RemoveAt(0);
        
// すべてのフィールドを削除します
doc.AcroForm.Fields.Clear();
先頭に戻る

フォームフィールドのタブ順序の定義

PDFドキュメントのフォームフィールドを移動するには、Tabキーを使用できます。また、AnnotationTabsOrderプロパティを使用して、フォームフィールドの移動順序を、次のいずれかの値に設定できます。

タブの順序の詳細については、PDF 1.7仕様(セクション12.5.1)を参照してください。

C#
コードのコピー
// フォームフィールドのタブの順序を行に設定します
page.AnnotationsTabsOrder = AnnotationsTabsOrder.RowOrder;
メモ: PDFドキュメントに設定されたタブの順序は、PDFビューワで表示および編集できます。詳細については、「フォームエディタ」を参照してください。

DioDocs for PDF を使用した AcroForm の実装の詳細については、DioDocs for PDF サンプルブラウザを参照してください。