none
Add the Text Form Field to the MS Office Word Document RRS feed

  • Question

  • Hello! Does anybody know how can I insert the Text Form Field to the MS Office Word 2003 Document (.doc)? And I also want to set the read-only property fo this field. How can I do this with C#?
    • Edited by YaRyaguzov Sunday, June 28, 2015 7:15 PM
    Sunday, June 28, 2015 7:09 PM

Answers

  • Hi Ya

    Below is the code for C#. Note that in order for form fields to work correctly, the entire document must be protected as a form - editing outside of form fields is impossible. If you insert Section breaks into such a document, single sections can be protected/unprotected, but some types of editing will still be impossible (headers and footers will be locked, for example).

    What you desire can be better achieved using the *.docx file format in Word 2007 and later.

                object oMissing = Missing.Value;
                object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
                object oTrue = true;
                getWordInstance();
                Word.Document wdDoc = wdApp.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                Word.Range rng = wdDoc.Content;
                rng.Collapse(ref oCollapseEnd);
                Word.FormField ffld = rng.FormFields.Add(rng, Word.WdFieldType.wdFieldFormTextInput);
                ffld.Name = "Text1";
                ffld.Enabled = false;
                ffld.Result = "Testing Text1 input"; //optional, if you want to enter text
                ffld.TextInput.Default = "Testing Text1 default text to display"; //Won't show if text is assigned to .Result
                wdDoc.Protect(Word.WdProtectionType.wdAllowOnlyFormFields, ref oTrue, ref oMissing, ref oMissing, ref oMissing);
    


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, June 29, 2015 5:37 PM
    Moderator

All replies

  • Hi Ya

    Below is the code for C#. Note that in order for form fields to work correctly, the entire document must be protected as a form - editing outside of form fields is impossible. If you insert Section breaks into such a document, single sections can be protected/unprotected, but some types of editing will still be impossible (headers and footers will be locked, for example).

    What you desire can be better achieved using the *.docx file format in Word 2007 and later.

                object oMissing = Missing.Value;
                object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
                object oTrue = true;
                getWordInstance();
                Word.Document wdDoc = wdApp.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                Word.Range rng = wdDoc.Content;
                rng.Collapse(ref oCollapseEnd);
                Word.FormField ffld = rng.FormFields.Add(rng, Word.WdFieldType.wdFieldFormTextInput);
                ffld.Name = "Text1";
                ffld.Enabled = false;
                ffld.Result = "Testing Text1 input"; //optional, if you want to enter text
                ffld.TextInput.Default = "Testing Text1 default text to display"; //Won't show if text is assigned to .Result
                wdDoc.Protect(Word.WdProtectionType.wdAllowOnlyFormFields, ref oTrue, ref oMissing, ref oMissing, ref oMissing);
    


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, June 29, 2015 5:37 PM
    Moderator
  • thanks, I've already done this field
    Monday, June 29, 2015 11:05 PM
  • Here is a solution based on free .NET Word library - https://www.nuget.org/packages/FreeSpire.Doc/

    PdfDocument doc = new PdfDocument();
    PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, new PdfMargins(0));
    
    //add a text field to pdf at specified position
    float x = 50;
    float y = 50;
    PdfTextBoxField textbox = new PdfTextBoxField(page, "TextBox");
    textbox.Bounds = new RectangleF(x, y, 100, 15);
    textbox.BorderWidth = 0.75f;
    textbox.BorderStyle = PdfBorderStyle.Solid;
    doc.Form.Fields.Add(textbox);
    //set a tool tip
    doc.Form.Fields["TextBox"].ToolTip = "Please insert a valid email address";
    //set the field as read-only
    doc.Form.Fields["TextBox"].ReadOnly=true;
    
    doc.SaveToFile("sample.pdf", FileFormat.PDF);

    Monday, October 31, 2016 3:58 AM