带选项的单选按钮
内容
[
隐藏
]在本教程中,我们将向您展示如何使用 Aspose.PDF for .NET 将带有选项的单选按钮添加到 PDF 文档。我们将逐步解释 C# 源代码,以指导您完成此过程。
第 1 步:准备
确保您已导入必要的库并设置文档目录的路径:
string dataDir = "YOUR DOCUMENTS DIRECTORY";
第 2 步:实例化文档对象
实例化一个 Document 对象来创建一个新的 PDF 文档:
Document doc = new Document();
第三步:添加页面和表格
向文档添加页面并创建一个表格来保存单选按钮选项:
Page page = doc.Pages.Add();
Aspose.Pdf.Table table = new Aspose.Pdf.Table();
table. ColumnWidths = "120 120 120";
page.Paragraphs.Add(table);
第 4 步:实例化 RadioButtonField 对象
实例化一个 RadioButtonField 对象来表示单选按钮:
RadioButtonField rf = new RadioButtonField(page);
rf. PartialName = "radio";
doc.Form.Add(rf, 1);
第 5 步:添加单选按钮选项
将单选按钮选项添加到 RadioButtonField 对象:
RadioButtonOptionField opt1 = new RadioButtonOptionField();
RadioButtonOptionField opt2 = new RadioButtonOptionField();
RadioButtonOptionField opt3 = new RadioButtonOptionField();
opt1.OptionName = "Item1";
opt2.OptionName = "Item2";
opt3.OptionName = "Item3";
opt1.Width = 15;
opt1. Height = 15;
opt2.Width = 15;
opt2. Height = 15;
opt3.Width = 15;
opt3. Height = 15;
rf.Add(opt1);
rf.Add(opt2);
rf.Add(opt3);
第 6 步:自定义单选按钮选项
通过设置边框、文本颜色和标题文本等属性来自定义单选按钮选项:
opt1.Border = new Border(opt1);
opt1.Border.Width = 1;
opt1.Border.Style = BorderStyle.Solid;
opt1.Characteristics.Border = System.Drawing.Color.Black;
opt1.DefaultAppearance.TextColor = System.Drawing.Color.Red;
opt1.Caption = new TextFragment("Item1");
//对 opt2 和 opt3 重复相同的步骤
步骤 7:将单选按钮选项添加到表中
将单选按钮选项添加到表中以显示它们:
Cell c1 = table.Rows.Add().Cells.Add();
Cell c2 = table.Rows[table.Rows.Count].Cells.Add();
Cell c3 = table.Rows[table.Rows.Count].Cells.Add();
c1.Paragraphs.Add(opt1);
c2.Paragraphs.Add(opt2);
c3.Paragraphs.Add(opt3);
步骤 8:保存 PDF 文档
保存创建的PDF文档:
dataDir = dataDir + "RadioButtonWithOptions_out.pdf";
doc.Save(dataDir);
使用 Aspose.PDF for .NET 的单选按钮示例源代码
try
{
//文档目录的路径。
string dataDir = "YOUR DOCUMENT DIRECTORY";
Document doc = new Document();
Page page = doc.Pages.Add();
Aspose.Pdf.Table table = new Aspose.Pdf.Table();
table.ColumnWidths = "120 120 120";
page.Paragraphs.Add(table);
Row r1 = table.Rows.Add();
Cell c1 = r1.Cells.Add();
Cell c2 = r1.Cells.Add();
Cell c3 = r1.Cells.Add();
RadioButtonField rf = new RadioButtonField(page);
rf.PartialName = "radio";
doc.Form.Add(rf, 1);
RadioButtonOptionField opt1 = new RadioButtonOptionField();
RadioButtonOptionField opt2 = new RadioButtonOptionField();
RadioButtonOptionField opt3 = new RadioButtonOptionField();
opt1.OptionName = "Item1";
opt2.OptionName = "Item2";
opt3.OptionName = "Item3";
opt1.Width = 15;
opt1.Height = 15;
opt2.Width = 15;
opt2.Height = 15;
opt3.Width = 15;
opt3.Height = 15;
rf.Add(opt1);
rf.Add(opt2);
rf.Add(opt3);
opt1.Border = new Border(opt1);
opt1.Border.Width = 1;
opt1.Border.Style = BorderStyle.Solid;
opt1.Characteristics.Border = System.Drawing.Color.Black;
opt1.DefaultAppearance.TextColor = System.Drawing.Color.Red;
opt1.Caption = new TextFragment("Item1");
opt2.Border = new Border(opt1);
opt2.Border.Width = 1;
opt2.Border.Style = BorderStyle.Solid;
opt2.Characteristics.Border = System.Drawing.Color.Black;
opt2.DefaultAppearance.TextColor = System.Drawing.Color.Red;
opt2.Caption = new TextFragment("Item2");
opt3.Border = new Border(opt1);
opt3.Border.Width = 1;
opt3.Border.Style = BorderStyle.Solid;
opt3.Characteristics.Border = System.Drawing.Color.Black;
opt3.DefaultAppearance.TextColor = System.Drawing.Color.Red;
opt3.Caption = new TextFragment("Item3");
c1.Paragraphs.Add(opt1);
c2.Paragraphs.Add(opt2);
c3.Paragraphs.Add(opt3);
dataDir = dataDir + "RadioButtonWithOptions_out.pdf";
//保存 PDF 文件
doc.Save(dataDir);
Console.WriteLine("\nRadio button field with three options added successfully.\nFile saved at " + dataDir);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
结论
恭喜!您已使用 Aspose.PDF for .NET 成功向 PDF 文档添加了带有选项的单选按钮。您现在可以使用此方法在 PDF 文档中创建交互式表单。