C# 使用PDFSharp填充PdfRadioButtonField
我使用的是PDFSharp版本1.50.4740-beta5,我是从NuGet安装的 我能够填写文本表单字段和复选框表单字段,但我根本无法让单选按钮工作。我没有错。SelectedIndex在我将其设置为1之前和之后均为-1 有几篇关于堆栈溢出的文章帮助我走到了这一步。是否有人使用此产品成功填充单选按钮表单字段? 以下是示例PDF的链接: (在你推荐iTextPdf之前,我已经对它和Aspose.PDF进行了评估,但它们并不实用)C# 使用PDFSharp填充PdfRadioButtonField,c#,pdf,radio-button,pdf-generation,pdfsharp,C#,Pdf,Radio Button,Pdf Generation,Pdfsharp,我使用的是PDFSharp版本1.50.4740-beta5,我是从NuGet安装的 我能够填写文本表单字段和复选框表单字段,但我根本无法让单选按钮工作。我没有错。SelectedIndex在我将其设置为1之前和之后均为-1 有几篇关于堆栈溢出的文章帮助我走到了这一步。是否有人使用此产品成功填充单选按钮表单字段? 以下是示例PDF的链接: (在你推荐iTextPdf之前,我已经对它和Aspose.PDF进行了评估,但它们并不实用) 以下几点对我很有用: 找出单选按钮的可能值,例如使用PDF工具
以下几点对我很有用:
Value
属性与PdfName
实例一起使用(不是PdfString
!)/
var radio = (PdfRadioButtonField)form.Fields["MyRadioButtonField"];
radio.Value = new PdfName("/choice1");
我在找到了解决方案,因为这是谷歌搜索“PDFSharp单选按钮”的第一步,我想我应该分享一下对我来说似乎有用的东西
我为PdfRadioButtonField
对象创建了扩展函数,它:
- 获取所有选项值及其索引
- 按索引号设置无线字段的值
- 按值设置无线字段的值
ErPdfRadioOption
为:
public class ErPdfRadioOption
{
public int Index { get; set; }
public string Value { get; set; }
}
获取无线电场的所有可用选项
结果(JSON)
按索引设置无线字段的选项
您可能认为这就是SelectedIndex
属性应该用于的内容。。。但显然不是
public static void SetOptionByIndex(this PdfRadioButtonField source,int index) {
if (source == null) return;
List<ErPdfRadioOption> options = source.FindOptions();
ErPdfRadioOption selectedOption = options.Find(x => x.Index == index);
if (selectedOption == null) return; //The specified index does not exist as an option for this radio field
// https://forum.pdfsharp.net/viewtopic.php?f=2&t=3561
PdfArray kids = (PdfArray)source.Elements["/Kids"];
int j = 0;
foreach (var kid in kids) {
var kidValues = ((PdfReference)kid).Value as PdfDictionary;
//PdfRectangle rectangle = kidValues.Elements.GetRectangle(PdfAnnotation.Keys.Rect);
if (j == selectedOption.Index)
kidValues.Elements.SetValue("/AS", new PdfName(selectedOption.Value));
else
kidValues.Elements.SetValue("/AS", new PdfName("/Off"));
j++;
}
}
按值设置无线字段的选项
公共静态void SetOptionByValue(此PdfRadioButtonField源,字符串值){
if(source==null | | string.IsNullOrWhiteSpace(value))返回;
如果(!value.StartsWith(“/”,StringComparison.OrdinalingOreCase))value=“/”+value;//所有值都以“/”字符开头
列表选项=source.FindOptions();
ErPdfRadioOption selectedOption=options.Find(x=>x.Value==Value);
if(selectedOption==null)return;//指定的索引不作为此单选字段的选项存在
source.SetOptionByIndex(selectedOption.Index);
}
注意:这取决于上面的FindOptions()
和SetOptionByIndex()
函数
根据PDFsharp论坛上的这篇文章:
根据Chris的回答,使用PDFsharp版本1.50.5147,突出的一点是,您必须将所有选项的值设置为\Off,同时将您想要的选项值设置为\Choice。为此,您可以使用以下简单功能:
//sets a radio button option by setting the index and value and turning the others off
static void setRadioOption (PdfRadioButtonField field, int option, string optvalue)
{
PdfArray kids = (PdfArray)field.Elements["/Kids"];
int j = 0;
foreach (var kid in kids)
{
var kidValues = ((PdfReference)kid).Value as PdfDictionary;
if (j == option)
kidValues.Elements.SetValue("/AS", new PdfName(optvalue));
else
kidValues.Elements.SetValue("/AS", new PdfName("/Off"));
j++;
}
}
你有没有找到解决这个问题的办法?我也遇到同样的问题,我没有。这个项目是我正在做的一些小事情中的一个,所以它被搁置了。我可能不会使用PDFSharp。它似乎不被支持。不幸的是,这对我不起作用。但是按照Chris的观点,需要关闭/关闭除您需要选择的选项之外的所有选项,然后将其设置为/choice1值确实有效。如果这对任何人都有帮助,我下面有一个片段。
PdfDocument source;
PdfAcroField field = source.AcroForm.Fields[...]; //name or index of the field
PdfRadioButtonField radioField = field as PdfRadioButtonField;
return radioField.FindOptions();
[
{
"Index": 0,
"Value": "/Choice1"
},
{
"Index": 1,
"Value": "/Choice2"
},
{
"Index": 2,
"Value": "/Choice3"
}
]
public static void SetOptionByIndex(this PdfRadioButtonField source,int index) {
if (source == null) return;
List<ErPdfRadioOption> options = source.FindOptions();
ErPdfRadioOption selectedOption = options.Find(x => x.Index == index);
if (selectedOption == null) return; //The specified index does not exist as an option for this radio field
// https://forum.pdfsharp.net/viewtopic.php?f=2&t=3561
PdfArray kids = (PdfArray)source.Elements["/Kids"];
int j = 0;
foreach (var kid in kids) {
var kidValues = ((PdfReference)kid).Value as PdfDictionary;
//PdfRectangle rectangle = kidValues.Elements.GetRectangle(PdfAnnotation.Keys.Rect);
if (j == selectedOption.Index)
kidValues.Elements.SetValue("/AS", new PdfName(selectedOption.Value));
else
kidValues.Elements.SetValue("/AS", new PdfName("/Off"));
j++;
}
}
PdfDocument source;
PdfAcroField field = source.AcroForm.Fields[...]; //name or index of the field
PdfRadioButtonField radioField = field as PdfRadioButtonField;
radioField.SetOptionByIndex(index);
public static void SetOptionByValue(this PdfRadioButtonField source, string value) {
if (source == null || string.IsNullOrWhiteSpace(value)) return;
if (!value.StartsWith("/", StringComparison.OrdinalIgnoreCase)) value = "/" + value; //All values start with a '/' character
List<ErPdfRadioOption> options = source.FindOptions();
ErPdfRadioOption selectedOption = options.Find(x => x.Value == value);
if (selectedOption == null) return; //The specified index does not exist as an option for this radio field
source.SetOptionByIndex(selectedOption.Index);
}
//sets a radio button option by setting the index and value and turning the others off
static void setRadioOption (PdfRadioButtonField field, int option, string optvalue)
{
PdfArray kids = (PdfArray)field.Elements["/Kids"];
int j = 0;
foreach (var kid in kids)
{
var kidValues = ((PdfReference)kid).Value as PdfDictionary;
if (j == option)
kidValues.Elements.SetValue("/AS", new PdfName(optvalue));
else
kidValues.Elements.SetValue("/AS", new PdfName("/Off"));
j++;
}
}