PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Mon Dec 09, 2024 11:38 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Tue Apr 16, 2024 9:32 am 
Offline

Joined: Thu Apr 11, 2024 2:33 pm
Posts: 4
Hello again,

I am struggeling with filling a form via PDFsharp, especially I am not able to figure out how to use the radiobuttons.

In LibreOffice I created a form and gave names to all the form items. I can read out the names of textboxes and checkboxes and can fill them or check them.

But when it comes to radiobuttons I am confused. In LibreOffice I create a group of radiobuttons and export the form as pdf. Lets say the group is called 'radiogroup' and the buttons are called 'opt_yes' and 'opt_no'. When I read out all the field names with the code below, then there is no group name shown, just the first option in the group 'opt_yes' twice. I get one entry with "opt_yes 0 /1' and one entry with 'opt_yes 1 /2'.

Code:
...
PdfRadioButtonField? radioField = fields[i] as PdfRadioButtonField;
List<ErPdfRadioOption> options = FindOptions(radioField);

foreach (var option in options)
{
    Console.WriteLine(radioField.Name.ToString() + " " + option.Index + " " + option.Value);
}
...


How do I get the group name as I need this for my autofill program. And how do I set one of the options in the group? Maybe there is a Sample for that, but I wasn't able to find.

Again, thanks in advance!


Here the code for the function/class I use (found them here on the forum)
Code:
public class ErPdfRadioOption
{
    public int Index { get; set; }
    public string Value { get; set; }
}

public static List<ErPdfRadioOption> FindOptions(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;
}


Top
 Profile  
Reply with quote  
PostPosted: Tue Apr 23, 2024 8:07 am 
Offline

Joined: Thu Apr 11, 2024 2:33 pm
Posts: 4
Hey all,

I found the solution to my problem.

The problem was, that I grouped the radiobuttons in a wrong way in LibreOffice Write. I entered a group name and a name for the radio button. But this way of grouping radiobuttons seems to be wrong.

In Libre Office Write, you have to select all the buttons that belong to a group with holding down the SHIFT key, so they get this blueish marking. Then right click on them and enter "Control Properties". Then enter a name in the first line and nothing else.

In your code you then can activate a radiobutton of this group with

Code:
...
PdfRadioButtonField? radioField = fields[i] as PdfRadioButtonField; //gets the group you created in LibreOffice
SetOptionByIndex(radioField, 0); //the second parameter is the index of the button in the group.
...


Hope this helps anyone who encounters the same issue.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 27 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group