我使用的PDFSharp版本1.50.4740-beta5来自http://www.pdfsharp.net,我从NuGet安装。
我能够填充文本表单字段和复选框表单字段,但是我无法让单选按钮工作。我没搞错。在我将其设置为1之前和之后,SelectedIndex是-1。
几篇关于堆栈溢出的文章帮助我走到了这一步。是否有人成功地使用此产品填充单选按钮表单字段?下面是一个指向样例PDF:enabled.pdf的链接
(在您建议iTextPdf之前,我已经评估了它和Aspose.PDF,它们不实用)
//using PdfSharp.Pdf.IO;
//using PdfSharp.Pdf;
//using PdfSharp.Pdf.AcroForms;
string fileName = "x:\\interactiveform_enabled.pdf";
PdfSharp.Pdf.PdfDocument pdfDocument = PdfReader.Open(fileName);
//The populated fields are not visible by default
if (pdfDocument.AcroForm.Elements.ContainsKey("/NeedAppearances"))
{
pdfDocument.AcroForm.Elements["/NeedAppearances"] = new PdfBoolean(true);
}
else
{
pdfDocument.AcroForm.Elements.Add("/NeedAppearances", new PdfBoolean(true));
}
PdfRadioButtonField currentField = (PdfRadioButtonField)(pdfDocument.AcroForm.Fields["Sex"]);
currentField.ReadOnly = false;
currentField.SelectedIndex = 1;
pdfDocument.Flatten();
pdfDocument.Save("x:\\interactiveform_enabled_2.pdf");发布于 2021-02-04 06:34:57
因为这是谷歌搜索"PDFSharp单选按钮“的第一名,所以我想我应该分享一下似乎对我有用的东西。
我为PdfRadioButtonField对象创建了扩展函数,该对象:
在以下示例中,ErPdfRadioOption是:
public class ErPdfRadioOption
{
public int Index { get; set; }
public string Value { get; set; }
}获取无线电场的所有可用选项
public static List<ErPdfRadioOption> FindOptions(this PdfRadioButtonField source) {
if (source == null) return null;
List<ErPdfRadioOption> options = new List<ErPdfRadioOption>();
PdfArray kids = source.Elements.GetArray("/Kids");
int i = 0;
foreach (var kid in kids) {
PdfReference kidRef = (PdfReference)kid;
PdfDictionary dict = (PdfDictionary)kidRef.Value;
PdfDictionary dict2 = dict.Elements.GetDictionary("/AP");
PdfDictionary dict3 = dict2.Elements.GetDictionary("/N");
if (dict3.Elements.Keys.Count != 2)
throw new Exception("Option dictionary should only have two values");
foreach (var key in dict3.Elements.Keys)
if (key != "/Off") { // "Off" is a reserved value that all non-selected radio options have
ErPdfRadioOption option = new ErPdfRadioOption() { Index = i, Value = key };
options.Add(option);
}
i++;
}
return options;
}用法
PdfDocument source;
PdfAcroField field = source.AcroForm.Fields[...]; //name or index of the field
PdfRadioButtonField radioField = field as PdfRadioButtonField;
return radioField.FindOptions();结果(JSON)
[
{
"Index": 0,
"Value": "/Choice1"
},
{
"Index": 1,
"Value": "/Choice2"
},
{
"Index": 2,
"Value": "/Choice3"
}
]按索引设置无线电字段的选项
你会认为这就是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++;
}
}注意:这取决于上面的FindOptions()函数。
用法
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);
}注意:这取决于上面的FindOptions()和SetOptionByIndex()函数。
基于PDFsharp论坛上的这篇文章:https://forum.pdfsharp.net/viewtopic.php?f=2&t=3561#p11386
使用PDFsharp版本1.50.5147
发布于 2019-10-04 20:27:58
以下几点对我有用:
Value实例一起使用PdfName属性(而不是PdfString!)/例如,如果要设置选中的选项"choice1“:
var radio = (PdfRadioButtonField)form.Fields["MyRadioButtonField"];
radio.Value = new PdfName("/choice1");发布于 2021-05-20 22:53:54
根据Chris的回答,突出的一点是,必须将所有选项的值设置为Off,同时将希望的选项的值设置为\选项。为此,您可以有一个简单的函数,如:
//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++;
}
}https://stackoverflow.com/questions/48231834
复制相似问题