2

Similar to iTextSharp - move Acrofield but without Reputation I can't write comments there.

How can a RadioButtonItem be moved to a different Page? I've tried several things including the use of the internal method "ForcePage", the Property "PlaceInPage" or setting PdfName.P to the new Page without success:

 PdfReader pdfr = new PdfReader(docdata);
 var mspdf = new MemoryStream();
 var pdfstamper = new PdfStamper(pdfr, mspdf);

 var _field = pdfstamper.AcroFields.GetFieldItem("gender");

 //accessing the internal "ForcePage" Method
 System.Reflection.BindingFlags bindingFlags = System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic;
 System.Reflection.MethodInfo minfo = _field.GetType().GetMethod("ForcePage", bindingFlags);
 minfo.Invoke(_field, new Object[] { 0, 2 });

 //trying to set it manually
 _field.GetWidget(0).Put(PdfName.P, (PdfObject)pdfstamper.Writer.GetPageReference(2));

This doesn't affect the fields position in any way in the resulting PDF, while the example taken from the other SO-Question works well for moving the radioField around within it's current page:

 PdfArray rect = _field.GetWidget(0).GetAsArray(PdfName.RECT);
 rect[0] = new PdfNumber(20);

Creating a new radiofield with the values of the old one in a different attempt:

 PdfFormField radiogroup = null;
 for (int i = 0; i < _field.Size ; i++)
 {
     object onValue = _field.GetWidget(i).GetAsDict(PdfName.AP).GetAsDict(PdfName.N).Keys.First();
     RadioCheckField radio = new RadioCheckField(pdfstamper.Writer, pdfstamper.AcroFields.GetFieldPositions("gender")[i].position, "genderNew", PdfName.DecodeName(name.ToString()));

     if (radiogroup == null)
     {         
        radiogroup = radio.GetRadioGroup(true, true);
        radiogroup.FieldName = "genderNew";
     }         

     var radiofield = radio.RadioField;
     page = pdfstamper.AcroFields.GetFieldPositions("gender")[i].page;
     radiofield.Page = page;
     ((PdfAnnotation)radiofield).PlaceInPage = page;
     radiogroup.AddKid(radiofield);
 }
 pdfstamper.AddAnnotation(radiogroup, 1);
 pdfstamper.AcroFields.RemoveField("gender");

This adds all of the RadioFields on Page 1, not on the page the items themselves were supposed to be placed.

If you wonder about the purpose of my quest: On most of our PDFs we have several Gender-Radios on different pages. They all have the same fieldname ("gender") and are supposed to be acting in unison - so when you choose "Male" on one page, the "Male" option is also selected on all the other pages. After generating the interactive PDF with InDesign the radiobuttons are not marked with the unison-flag as you can see here:

https://i.stack.imgur.com/3NTaN.jpg

When we tick the box by hand in Acrobat everything works, but the docs are plenty and are recreated often so we have to do it by code.

Trying to just modify the unison-behavior by code I experimented with the following example:

 pdfstamper.AcroFields.SetFieldProperty("gender", "setfflags", PdfFormField.FF_RADIOSINUNISON, null);

Surprisingly the "Button with the same name and choice are selected in unison" - checkbox is now selected when inspecting the radio button properties in Acrobat, but it still doesn't act in unison.

Placing several RadioGroups with the same fieldname ("gender") on all the target pages (setting the pagenumber within the "AddAnnotation"-Method), they show up on the correct location, but are not acting in unison (e.g. selecting "Male" on page 2 deselects the radios on page 1 despite setting the "radiosInUnison"-Parameter in the "GetRadioGroup"-Method)

When adding all the radioFields to one radioGroup the behavior is as expected in unison, but we can't split the radioFields up onto different pages as you can see above.

Community
  • 1
  • 1
Schmafuh
  • 21
  • 3
  • 1
    We found a different solution to our problem, so we don't need to move radiofields around anymore for now. If someone is in the same "multipage radiobutton unison"-pain the following can solve it: - Split up your document into pages. - Read every single page and replace the radiobuttons with new ones (keep the same name and set the radiosInUnison-Parameter in the GetRadioGroup Method. - Merge all the pages in the same order back together and voilà: the radiobutton groups have correctly been merged to one unison group. cheers – Schmafuh Apr 16 '14 at 10:32

0 Answers0