首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用PdfRadioButtonField填充PDFSharp

使用PdfRadioButtonField填充PDFSharp
EN

Stack Overflow用户
提问于 2018-01-12 18:08:50
回答 4查看 2.2K关注 0票数 3

我使用的PDFSharp版本1.50.4740-beta5来自http://www.pdfsharp.net,我从NuGet安装。

我能够填充文本表单字段和复选框表单字段,但是我无法让单选按钮工作。我没搞错。在我将其设置为1之前和之后,SelectedIndex是-1。

几篇关于堆栈溢出的文章帮助我走到了这一步。是否有人成功地使用此产品填充单选按钮表单字段?下面是一个指向样例PDF:enabled.pdf的链接

(在您建议iTextPdf之前,我已经评估了它和Aspose.PDF,它们不实用)

代码语言:javascript
复制
        //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");
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2021-02-04 06:34:57

因为这是谷歌搜索"PDFSharp单选按钮“的第一名,所以我想我应该分享一下似乎对我有用的东西。

我为PdfRadioButtonField对象创建了扩展函数,该对象:

  • 获取所有选项值及其索引。
  • 按索引号设置无线电字段的值。
  • 按值设置无线电字段的值。

在以下示例中,ErPdfRadioOption是:

代码语言:javascript
复制
public class ErPdfRadioOption
{
    public int Index { get; set; }
    public string Value { get; set; }
}

获取无线电场的所有可用选项

代码语言:javascript
复制
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;
}

用法

代码语言:javascript
复制
PdfDocument source;
PdfAcroField field = source.AcroForm.Fields[...]; //name or index of the field
PdfRadioButtonField radioField = field as PdfRadioButtonField;
return radioField.FindOptions();

结果(JSON)

代码语言:javascript
复制
[
  {
    "Index": 0,
    "Value": "/Choice1"
  },
  {
    "Index": 1,
    "Value": "/Choice2"
  },
  {
    "Index": 2,
    "Value": "/Choice3"
  }
]

按索引设置无线电字段的选项

你会认为这就是SelectedIndex属性应该用来.但显然不是。

代码语言:javascript
复制
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()函数。

用法

代码语言:javascript
复制
PdfDocument source;
PdfAcroField field = source.AcroForm.Fields[...]; //name or index of the field
PdfRadioButtonField radioField = field as PdfRadioButtonField;
radioField.SetOptionByIndex(index);

按值设置无线电字段的选项

代码语言:javascript
复制
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

票数 2
EN

Stack Overflow用户

发布于 2019-10-04 20:27:58

以下几点对我有用:

  1. 查找单选按钮的可能值,例如使用PDF工具
  2. Value实例一起使用PdfName属性(而不是PdfString!)
  3. 在值名之前使用前缀/

例如,如果要设置选中的选项"choice1“:

代码语言:javascript
复制
var radio = (PdfRadioButtonField)form.Fields["MyRadioButtonField"];
radio.Value = new PdfName("/choice1");

我在https://forum.pdfsharp.net/viewtopic.php?f=2&t=632找到了解决方案

票数 2
EN

Stack Overflow用户

发布于 2021-05-20 22:53:54

根据Chris的回答,突出的一点是,必须将所有选项的值设置为Off,同时将希望的选项的值设置为\选项。为此,您可以有一个简单的函数,如:

代码语言:javascript
复制
    //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++;
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48231834

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档