轉帖|其它|編輯:郝浩|2011-09-08 14:33:34.000|閱讀 558 次
概述:Adobe Acrobat Form (以前叫AcroForm) 是表單域的集合,用來以交互方式從用戶那里收集信息。有時,我們也稱這類表單為交互表單。一個PDF文檔不能包含一個以上的表單。在交互表單中,表單域按層次結構組織,它們可以從父域中繼承某些屬性。在世界范圍內,AcroForms正逐漸成為商業和政府組織收集用戶信息的正式途徑。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
Adobe Acrobat Form (以前叫AcroForm) 是表單域的集合,用來以交互方式從用戶那里收集信息。有時,我們也稱這類表單為交互表單。一個PDF文檔不能包含一個以上的表單。在交互表單中,表單域按層次結構組織,它們可以從父域中繼承某些屬性。在世界范圍內,AcroForms正逐漸成為商業和政府組織收集用戶信息的正式途徑。
Aspose.Pdf和表單域
Aspose.Pdf支持創建交互表單。Aspose.Pdf支持一些簡單類型的可以添加到PDF表單中的表單域。這些表單域類型在枚舉類型FormFieldType中定義。FormFieldType枚舉類型中預定義的表單域如下表所示:
表單域類型 | 描述 |
RadioButton | 單選按鈕 |
List | 列表框 |
CheckBox | 復選框 |
Combo | 組合框 |
Text | 文本域 |
注意:向PDF文檔中添加表單域時必須為每個表單域指定唯一的名稱。因為填充表單域時,通過表單域的名稱來操作。例如:如果需要通過一些工具如Aspose.Pdf.Kit填充表單域時,可以根據表單域名稱來填充。
如何添加表單域?
Aspose.Pdf提供了FormField類以便開發人員在他們的PDF表單中創建不同種類的表單域。開發人員可以創建FormField對象然后使用FormFieldType指定它們的類型。最后開發人員可以使用FormField類的FieldName 和 FieldValue屬性設置表單域的名稱和值。FormField類還有許多其它屬性供開發人員自定義Aspose.Pdf支持的任意類型的表單域。
下面的例子演示了表單域的用法。
示例代碼:
[C#]
//創建一個Pdf對象pdf1
Pdf pdf1 = new Pdf();
//在pdf1中添加一個Section對象
Section sec1 = pdf1.Sections.Add();
//創建一個表單域
FormField combo = new FormField();
//設置表單域的類型為組合框
combo.FormFieldType = FormFieldType.Combo;
//設置表單域的名稱
combo.FieldName = "ACombo";
//添加幾個選項
combo.ChoiceOptions = new string[]{"Red","Green","Blue"};
//指定組合框的默認值
combo.FieldValue = "Red";
//設置表單域的寬度
combo.FormWidth = 80;
//設置表單域的高度
combo.FormHeight = 20;
//添加表單域到sec1的段落集合中
sec1.Paragraphs.Add(combo);
//保存Pdf
pdf1.Save(...);
[VB.NET]
'創建一個Pdf對象pdf1
Dim pdf1 As Pdf = New Pdf
'在pdf1中添加一個Section對象sec1
Dim sec1 As Section = pdf1.Sections.Add()
'創建一個表單域
Dim combo As FormField = New FormField
'設置表單域的類型為組合框
combo.FormFieldType = FormFieldType.Combo
'設置表單域的名稱
combo.FieldName = "ACombo"
'添加幾個選項
combo.ChoiceOptions = New String() {"Red", "Green", "Blue"}
'指定組合框的默認值
combo.FieldValue = "Red"
'設置表單域的寬度
combo.FormWidth = 80
'設置表單域的高度
combo.FormHeight = 20
'添加表單域到sec1的段落集合中
sec1.Paragraphs.Add(combo)
'保存Pdf
pdf1.Save(...)
[XML]
<?xml version="1.0" encoding="utf-8" ?>
<Pdf xmlns="Aspose.Pdf">
<Section>
<FormField FormFieldType="Combo" FieldName="ACombo"
FieldValue="Red" ChoiceOptions="Red Green Blue"
FormWidth="80" FormHeight="20" />
</Section>
</Pdf>
使用自定義定位
在Aspose.Pdf中,FormField類設計為段落,不支持嵌入表單域。由于表單域經常與其它內容(如,文本)一起使用。 通常有兩種方法定位表單域:使用表格或使用自定義定位。
注意:對于單選按鈕,只支持自定義定位。
Aspose.Pdf在PositioningType枚舉類型中定義了一些定位類型。這些預定義的定位類型及含義如下表所示。
定位類型 | 說明 |
Auto | 段落由頁面繪制引擎自動定位 |
PageRelative | 段落相對與頁面定位 |
ColumnRelative | 段落相對于欄目定位 |
ParagraphRelative | 段落相對與其它段落定位 |
下面的例子演示了如何通過自定義定位使用單選按鈕。
[C#]
//創建一個Pdf對象并添加一個Section對象sec1
Pdf pdf1 = new Pdf();
Section sec1 = pdf1.Sections.Add();
//創建一個table,指定列寬并將它添加到sec1中
Table tab1 = new Table();
tab1.ColumnWidths = "120 120 120";
sec1.Paragraphs.Add(tab1);
//在table中添加一行
Row r1 = tab1.Rows.Add();
//在行中添加一個單元格,設置它的邊距,并指定單元格中的第一個段落的id為"text1"
Cell c1 = r1.Cells.Add("item1");
c1.Padding.Left = 30;
c1.Paragraphs[0].ID = "text1";
//在行中添加第2個單元格,設置它的邊距,并指定單元格中的第一個段落的id為"text2"
Cell c2 = r1.Cells.Add("item2");
c2.Padding.Left = 30;
c2.Paragraphs[0].ID = "text2";
//在行中添加第3個單元格,設置它的邊距,并指定單元格中的第一個段落的id為"text3"
Cell c3 = r1.Cells.Add("item3");
c3.Padding.Left = 30;
c3.Paragraphs[0].ID = "text3";
//創建一個單選按鈕類型的表單域,設置表單域名稱和按鈕顏色,設置選中的按鈕
FormField radio = new FormField();
radio.FormFieldType = FormFieldType.RadioButton;
radio.FieldName = "ARadio";
radio.ButtonColor = System.Drawing.Color.FromName("Red");
radio.RadioButtonCheckedIndex = 0;
//創建第1個單選鈕,添加到上面的表單域中,指定寬度和高度。單選鈕的位置設為相對
//段落。 將單選鈕與ID為"text1"的文本段落關聯。
RadioButton bt1 = radio.RadioButtons.Add();
bt1.ButtonHeight = 12;
bt1.ButtonWidth = 12;
bt1.PositioningType = PositioningType.ParagraphRelative;
bt1.ReferenceParagraphID = "text1";
bt1.Left = -20;
bt1.Top = 0;
//創建第2個單選鈕,添加到上面的表單域中,指定寬度和高度。單選鈕的位置設為相對
//段落。 將單選鈕與ID為"text2"的文本段落關聯。
RadioButton bt2 = radio.RadioButtons.Add();
bt2.ButtonHeight = 12;
bt2.ButtonWidth = 12;
bt2.PositioningType = PositioningType.ParagraphRelative;
bt2.ReferenceParagraphID = "text2";
bt2.Left = -20;
bt2.Top = 0;
//創建第3個單選鈕,添加到上面的表單域中,指定寬度和高度。單選鈕的位置設為相對
//段落。 將單選鈕與ID為"text3"的文本段落關聯。
RadioButton bt3 = radio.RadioButtons.Add();
bt3.ButtonHeight = 12;
bt3.ButtonWidth = 12;
bt3.PositioningType = PositioningType.ParagraphRelative;
bt3.ReferenceParagraphID = "text3";
bt3.Left = -20;
bt3.Top = 0;
//將單選按鈕表單域添加到sec1的段落集合中
sec1.Paragraphs.Add(radio);
//保存Pdf
pdf1.Save(...);
[VB.NET]
'創建一個Pdf對象并添加一個Section對象sec1
Dim pdf1 As Pdf = New Pdf
Dim sec1 As Section = pdf1.Sections.Add()
'創建一個table,指定列寬并將它添加到sec1中
Dim tab1 As Table = New Table
tab1.ColumnWidths = "120 120 120"
sec1.Paragraphs.Add(tab1)
'在table中添加一行
Dim r1 As Row = tab1.Rows.Add()
'在行中添加一個單元格,設置它的邊距,并指定單元格中的第一個段落的id為"text1"
Dim c1 As Cell = r1.Cells.Add("item1")
c1.Padding.Left = 30
c1.Paragraphs(0).ID = "text1"
'在行中添加第2個單元格,設置它的邊距,并指定單元格中的第一個段落的id為"text2"
in the cell to "text2"
Dim c2 As Cell = r1.Cells.Add("item2")
c2.Padding.Left = 30
c2.Paragraphs(0).ID = "text2"
'在行中添加第3個單元格,設置它的邊距,并指定單元格中的第一個段落的id為"text3"
in the cell to "text3"
Dim c3 As Cell = r1.Cells.Add("item3")
c3.Padding.Left = 30
c3.Paragraphs(0).ID = "text3"
'創建一個單選按鈕類型的表單域,設置表單域名稱和按鈕顏色,設置選中的按鈕
Dim radio As FormField = New FormField
radio.FormFieldType = FormFieldType.RadioButton
radio.FieldName = "ARadio"
radio.ButtonColor = System.Drawing.Color.FromName("Red")
radio.RadioButtonCheckedIndex = 0
'創建第1個單選鈕,添加到上面的表單域中,指定寬度和高度。單選鈕的位置設為相對
'段落。 將單選鈕與ID為"text1"的文本段落關聯。
Dim bt1 As RadioButton = radio.RadioButtons.Add()
bt1.ButtonHeight = 12
bt1.ButtonWidth = 12
bt1.PositioningType = PositioningType.ParagraphRelative
bt1.ReferenceParagraphID = "text1"
bt1.Left = -20
bt1.Top = 0
'創建第2個單選鈕,添加到上面的表單域中,指定寬度和高度。單選鈕的位置設為相對
'段落。 將單選鈕與ID為"text2"的文本段落關聯。
Dim bt2 As RadioButton = radio.RadioButtons.Add()
bt2.ButtonHeight = 12
bt2.ButtonWidth = 12
bt2.PositioningType = PositioningType.ParagraphRelative
bt2.ReferenceParagraphID = "text2"
bt2.Left = -20
bt2.Top = 0
'創建第3個單選鈕,添加到上面的表單域中,指定寬度和高度。單選鈕的位置設為相對
'段落。 將單選鈕與ID為"text3"的文本段落關聯。
Dim bt3 As RadioButton = radio.RadioButtons.Add()
bt3.ButtonHeight = 12
bt3.ButtonWidth = 12
bt3.PositioningType = PositioningType.ParagraphRelative
bt3.ReferenceParagraphID = "text3"
bt3.Left = -20
bt3.Top = 0
'將單選按鈕表單域添加到sec1的段落集合中
sec1.Paragraphs.Add(radio)
'保存Pdf
pdf1.Save("e:/temp/test.pdf")
[XML]
<?xml version="1.0" encoding="utf-8" ?>
<Pdf xmlns="Aspose.Pdf">
<Section>
<Table ColumnWidths="120 120 120">
<Row>
<Cell PaddingLeft="30">
<Text ID="text1">
<Segment>item1</Segment>
</Text>
</Cell>
<Cell PaddingLeft="30">
<Text ID="text2">
<Segment>item2</Segment>
</Text>
</Cell>
<Cell PaddingLeft="30">
<Text ID="text3">
<Segment>item3</Segment>
</Text>
</Cell>
</Row>
</Table>
<FormField FormFieldType="RadioButton"
RadioButtonCheckedIndex="0" FieldName="aRadioutton"
ButtonColor="Red">
<RadioButton ButtonWidth="12" ButtonHeight="12"
PositioningType="ParagraphRelative"
ReferenceParagraphID="text1" Left="-20" Top="0">
</RadioButton>
<RadioButton ButtonWidth="12" ButtonHeight="12"
PositioningType="ParagraphRelative"
ReferenceParagraphID="text2" Left="-20" Top="0">
</RadioButton>
<RadioButton ButtonWidth="12" ButtonHeight="12"
PositioningType="ParagraphRelative"
ReferenceParagraphID="text3" Left="-20" Top="0">
</RadioButton>
</FormField>
</Section>
</Pdf>
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:網絡轉載