PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Sep 23, 2017 12:16 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Wed Mar 29, 2017 4:33 pm 
Offline

Joined: Wed Mar 29, 2017 4:22 pm
Posts: 11
Location: Virginia (USA)
I'm filling in a premade PDF form and I've got the text fields working great.
I've run into an issue with the Radio Buttons though:

In my code it SEEMS to work in that if I set a SelectedIndex on my PdfRadioButtonField I can see the corresponding Value property in the Visual Studio.
Likewise if I set the Value property it changes the SelectedIndex value to the correct one so it APPEARS to be working great.... until you actually look at the resulting document.

While the code shows the correct selection being made when you view the saved document it shows NO selection as being made (none of the checkboxes in the radio button group are checked)

What am I missing?

I've googled all over but there seems to be very little help for AcroForm use in PDFSharp.


Top
 Profile  
Reply with quote  
PostPosted: Thu Mar 30, 2017 2:49 pm 
Offline

Joined: Wed Mar 29, 2017 4:22 pm
Posts: 11
Location: Virginia (USA)
Not only does it not seem to set them but it appears to break existing ones:

In order to try to compare what was different between a set and not set value I checked one of the boxes in the source pdf and saved it.
First problem is that the nuget version of PdfSharp wouldn't even open the newly saved version (apparently just checking a box and saving upgrades the version).
So I upgraded to the -pre version of PdfSharp (hey, maybe that would fix my radio button issue too right?) to see if that worked.
It does open the file and setting the text boxes still work but even if I comment out everything even referencing the radio button the resulting document loses the checked radio button from the source.

Inspecting the radio button in the new version shows it's Value as "/1" which doesn't make sense for a radio button as it has multiple kids and while the kid whose selected should be "/1" (the On value in this PDF) the value of the parent should be which kid is selected. In the non -Pre version this seemed to work corretly by having "/Choice1", "/Choice2", etc. in there depending on what SelectedIndex was set.

Again it's entirely possible I'm missing something but the behavior in the -Pre version seems more incorrect then the releave version with respect to radio buttons. (though the new version does allow for opening of higher version pdfs)

Any help here would be greatly appreciated.


Top
 Profile  
Reply with quote  
PostPosted: Wed Apr 05, 2017 7:02 pm 
Offline

Joined: Wed Mar 29, 2017 4:22 pm
Posts: 11
Location: Virginia (USA)
Ok I'm still trying to get this to work. Clearly the SelectedIndex and Value properties of the PdfRadioButtonField don't seem to work (meaning they don't visibly check the checkbox in the radio button group)

So I tried doing it myself manually as I understand it.
First, apparently PDFs don't have a standardized "On" value. So /Off is always off but on you have to figure out. I THINK I have that working.

To get it I do the following:
1. Get the PdfReference of one of the options from the RadioButtonField.Fields.Elements collection.
2. cast that reference to a PdfDictionary object
3. Get the AP Element of the Dictionary object
4. Get the N element of the AP Element
5. loop through the keys looking for the non-/Off value and save it. In my case it's "/1"

Once I have that I try to manually select the option by:
Taking the Dictionary object I initialized in step 2 above and doing Elements.SetName on both PdfAcroField.Keys.V and PdfAnnotation.Keys.AS with the value I got from step 5 above.
In theory this should select the element I grabbed in Step 1 above from the RadioButtonField so I can pick whichever one I wanted based on which member of the collection I grab.

This still doesn't work for me though so I'm clearly still missing something.

Again, any help here would be greatly appreciated.


Top
 Profile  
Reply with quote  
PostPosted: Thu Apr 06, 2017 7:08 pm 
Offline

Joined: Wed Mar 29, 2017 4:22 pm
Posts: 11
Location: Virginia (USA)
Ok I have a new discovery now.
I've been banging my head against the wall trying to get once radiobuttongroup to work but there is another one on the form.
On the other one if I set the Value = new pdfName("/Choice1" or "/Choice2") it works and selects one of the two checkboxes in that radio button group.
This however does not work in the first set. Not only that checking them in the file and saving it then loading it into pdfSharp causes it to lose it's value. This one has 5 choices but they seem to be named /Choice1, /Choice2, /Choice3, /Choice4, and /Choice4 again.
My GUESS is that the duplicate naming of /Choice4 is breaking pdfSharp. This appears to be ok as far a Acrobat reader is concerned though and the form works fine when you fill it out manually. (checking the fourth of fifth option doesn't check both and IS retained).
The problem is that IF my guess is even right we don't control the form. It's a government form that's given to us and we're just trying to automate filling in the form fields with entries from a database.
Can anyone confirm my guess is the issue (duplicate name fields) and offer any suggestions on how I can get around it and at least perserve the prechecked value. (I wouldn't even mind loading one of 5 different pdfs each with a different option pre-checked but right now pdfSharp loses even the prechecked value)


Top
 Profile  
Reply with quote  
PostPosted: Thu Apr 06, 2017 8:07 pm 
Offline

Joined: Wed Mar 29, 2017 4:22 pm
Posts: 11
Location: Virginia (USA)
Yet more info:
The RadioButtonField that DOES work does NOT have an /Opt Element.

The RedioButtonField that doesn't work and doesn't even retain a preset value DOES have an /Opt Element.
The issue in PDFSharp may be related to the bug reported here:

viewtopic.php?f=3&t=647

That bug was reported in 2009 but the only reply is from the bug submitter. I don't know if this bug was ever confirmed or fixed or what.


Top
 Profile  
Reply with quote  
PostPosted: Fri Apr 07, 2017 6:06 pm 
Offline

Joined: Wed Mar 29, 2017 4:22 pm
Posts: 11
Location: Virginia (USA)
Ok so my deadline is Monday and I'm running out of time. So now instead of trying to get it to work I've moved to trying to get it to not lose a selection that was saved in the file before it was opened.

Just to see where it was losing the preselected value I commented out everything. The program just opens the file (for modify) with the radio button group option already selected and then saves it. This keeps the value. I added back in lines until it lost the selection.

The selection seems to be lost when I add the "/NeedAppearances" key to the Elements collection of the AcroForm.
If I don't add this key the values I fill into the text boxes don't display unless the box has focus.

I'm not sure why adding a key to the form object would break a RadioButtonField but that's where I lose the preset value but I'm not sure where to go from here as not adding that key makes the text controls not work and adding it breaks the radiobutton.

Any help would be greatly appreciated.

Found this that may be related: https://forums.adobe.com/thread/1839021
Of course there's no answer to that either.


Top
 Profile  
Reply with quote  
PostPosted: Mon Apr 10, 2017 3:29 pm 
Offline

Joined: Wed Mar 29, 2017 4:22 pm
Posts: 11
Location: Virginia (USA)
If anyone is actually following this thread (or googles it later) we did get it to work but it's a hack and I'd still be interested in the correct way of making it work.

What's we're doing (and seems to work) is:
Remove the /Opt element from the Radio Button Field.
Add a /V element to the Radio Button Field.
Set the Value property of the Radio Button Field to the index of the Radio Button Field. ex: "/1" for the second option.

Why this works I have no idea... how the index works after you delete the option array seems unintuitive and I'm sure it's not "correct" but the correct radio button group is visibly checked in the resulting document when this is done.

Any thoughts on why this does work and/or the proper way to do it would be greatly appreciated.


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 5 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:  
Powered by phpBB® Forum Software © phpBB Group