First, consider any of the software products (some of which are quite expensive, but can still cost) on the market that provide simple methods for setting the value of formfields.
OpenXML SDK, ( , ), ( , , , - OpenXML SDK , ):
TextInput:
, "" . , textinput.
, "end" . , - , .
, "" . , "end" fieldchar, , "" .
( rPr).
, "end" .
( , , "", , "" ).
.
, "", , "" .
, pPr.
, , "" , "".
:
:
(< br/" > ).
, .
:
, .
, .
- , , "" bookmarkend, , .
, :
private static void SetFormFieldValue(TextInput textInput, string value)
{
if (value == null)
{
if (textInput.DefaultTextBoxFormFieldString != null && textInput.DefaultTextBoxFormFieldString.Val.HasValue)
value = textInput.DefaultTextBoxFormFieldString.Val.Value;
}
short maxLength = 0;
if (textInput.MaxLength != null && textInput.MaxLength.Val.HasValue)
maxLength = textInput.MaxLength.Val.Value;
if (value != null && maxLength > 0 && value.Length > maxLength)
value = value.Substring(0, maxLength);
if (textInput.TextBoxFormFieldType != null
&& textInput.TextBoxFormFieldType.Val.HasValue
&& textInput.TextBoxFormFieldType.Val.Value != TextBoxFormFieldValues.Regular)
throw new ApplicationException("SetFormField: Unsupported textboxformfieldtype, only regular is handled.\r\n" + textInput.Parent.OuterXml);
if (!string.IsNullOrWhiteSpace(value)
&& textInput.Format != null
&& textInput.Format.Val.HasValue)
{
switch (textInput.Format.Val.Value)
{
case "Uppercase":
value = value.ToUpperInvariant();
break;
case "Lowercase":
value = value.ToLowerInvariant();
break;
case "First capital":
value = value[0].ToString().ToUpperInvariant() + value.Substring(1);
break;
case "Title case":
value = System.Globalization.CultureInfo.InvariantCulture.TextInfo.ToTitleCase(value);
break;
default:
break;
}
}
Run rTextInput = textInput.Ancestors<Run>().FirstOrDefault();
if (rTextInput == null) throw new ApplicationException("SetFormField: Did not find run containing textinput.\r\n" + textInput.Parent.OuterXml);
Run rSeparate = rTextInput.ElementsAfter().FirstOrDefault(ru =>
ru.GetType() == typeof(Run)
&& ru.Elements<FieldChar>().FirstOrDefault(fc =>
fc.FieldCharType == FieldCharValues.Separate)
!= null) as Run;
if (rSeparate == null) throw new ApplicationException("SetFormField: Did not find run containing separate.\r\n" + textInput.Parent.OuterXml);
Run rEnd = rTextInput.ElementsAfter().FirstOrDefault(ru =>
ru.GetType() == typeof(Run)
&& ru.Elements<FieldChar>().FirstOrDefault(fc =>
fc.FieldCharType == FieldCharValues.End)
!= null) as Run;
if (rEnd == null)
{
Paragraph p = rSeparate.Parent as Paragraph;
Paragraph pEnd = p.ElementsAfter().FirstOrDefault(pa =>
pa.GetType() == typeof(Paragraph)
&& pa.Elements<Run>().FirstOrDefault(ru =>
ru.Elements<FieldChar>().FirstOrDefault(fc =>
fc.FieldCharType == FieldCharValues.End)
!= null)
!= null) as Paragraph;
if (pEnd == null) throw new ApplicationException("SetFormField: Did not find paragraph containing end.\r\n" + textInput.Parent.OuterXml);
rEnd = pEnd.Elements<Run>().FirstOrDefault(ru =>
ru.Elements<FieldChar>().FirstOrDefault(fc =>
fc.FieldCharType == FieldCharValues.End)
!= null);
}
Run rFirst = rSeparate.NextSibling<Run>();
if (rFirst == null || rFirst == rEnd)
{
RunProperties rPr = rTextInput.GetFirstChild<RunProperties>();
if (rPr != null) rPr = rPr.CloneNode(true) as RunProperties;
rFirst = rSeparate.InsertAfterSelf<Run>(new Run(new[] { rPr }));
}
rFirst.RemoveAllChildren<Text>();
Run r = rFirst.NextSibling<Run>();
while(r != rEnd)
{
if (r != null)
{
r.Remove();
r = rFirst.NextSibling<Run>();
}
else
{
Paragraph p = rFirst.Parent.NextSibling<Paragraph>();
if (p == null) throw new ApplicationException("SetFormField: Did not find next paragraph prior to or containing end.\r\n" + textInput.Parent.OuterXml);
r = p.GetFirstChild<Run>();
if (r == null)
{
p.Remove();
while (p.FirstChild != null)
{
OpenXmlElement oxe = p.FirstChild;
oxe.Remove();
if (oxe.GetType() == typeof(ParagraphProperties)) continue;
rSeparate.Parent.AppendChild(oxe);
}
}
}
}
if (rEnd.Parent != rSeparate.Parent)
{
Paragraph p = rEnd.Parent as Paragraph;
p.Remove();
while (p.FirstChild != null)
{
OpenXmlElement oxe = p.FirstChild;
oxe.Remove();
if (oxe.GetType() == typeof(ParagraphProperties)) continue;
rSeparate.Parent.AppendChild(oxe);
}
}
if (value != null)
{
string[] lines = value.Replace("\r\n", "\n").Split(new char[]{'\v', '\n', '\r'});
string line = lines[0];
Text text = rFirst.AppendChild<Text>(new Text(line));
if (line.StartsWith(" ") || line.EndsWith(" ")) text.SetAttribute(new OpenXmlAttribute("xml:space", null, "preserve"));
for (int i = 1; i < lines.Length; i++)
{
rFirst.AppendChild<Break>(new Break());
line = lines[i];
text = rFirst.AppendChild<Text>(new Text(lines[i]));
if (line.StartsWith(" ") || line.EndsWith(" ")) text.SetAttribute(new OpenXmlAttribute("xml:space", null, "preserve"));
}
}
else
{
short length = maxLength;
if (length == 0 || length > 5) length = 5;
rFirst.AppendChild(new Text(((char)8194).ToString()));
r = rFirst;
for (int i = 1; i < length; i++) r = r.InsertAfterSelf<Run>(r.CloneNode(true) as Run);
}
}
1. textinput form. xml- , , - . - , Word . , / , OpenXML.
2. Word.
-
-
-
-
-
- ( f.ex 20, ) |
Visual Studio document.xml( " ", xml).
, , .
3. formfield.