views:

656

answers:

1

So, I have a Microsoft Word 2007 Document with several Plain Text Format (I have tried Rich Text Format as well) controls which accept input via XML.

For carriage returns, I had the string being passed through XML containing "\r\n" when I wanted a carriage return, but the word document ignored that and just kept wrapping things on the same line. I also tried replacing the \r\n with System.Environment.NewLine in my C# mapper, but that just put in \r\n anyway, which still didn't work.

Note also that on the control itself I have set it to "Allow Carriage Returns (Multiple Paragrpahs)" in the control properties.

This is the XML for the listMapper

 <Field id="32"  name="32" fieldType="SimpleText">
  <DataSelector path="/Data/DB/DebtProduct">
   <InputField fieldType=""  path="/Data/DB/Client/strClientFirm" link="" type=""/>
   <InputField fieldType=""  path="strClientRefDebt" link="" type=""/>
  </DataSelector>
  <DataMapper formatString="{0} Account Number: {1}" name="SimpleListMapper" type="">
   <MapperData>
   </MapperData>
  </DataMapper>
 </Field>

Note that this is the listMapper C# where I actually map the list (notice where I try and append the system.environment.newline)

namespace DocEngine.Core.DataMappers
{
    public class CSimpleListMapper:CBaseDataMapper
    {
        public override void Fill(DocEngine.Core.Interfaces.Document.IControl control, CDataSelector dataSelector)
        {
            if (control != null && dataSelector != null)
            {
                ISimpleTextControl textControl = (ISimpleTextControl)control;
                IContent content = textControl.CreateContent();
                CInputFieldCollection fileds = dataSelector.Read(Context);
                StringBuilder builder = new StringBuilder();

                if (fileds != null)
                {
                    foreach (List<string> lst in fileds)
                    {
                        if (CanMap(lst) == false) continue;
                        if (builder.Length > 0 && lst[0].Length > 0)
                            builder.Append(Environment.NewLine);
                        if (string.IsNullOrEmpty(FormatString))
                            builder.Append(lst[0]);
                        else
                            builder.Append(string.Format(FormatString, lst.ToArray()));
                    }

                    content.Value = builder.ToString();

                    textControl.Content = content;
                    applyRules(control, null);
                }
            }
        }
    }
}

Does anybody have any clue at all how I can get MS Word 2007 (docx) to quit ignoring my newline characters??

A: 

// i think it works
WordprocessingDocument _docx = WordprocessingDocument.Create("c:\Test.docx", WordprocessingDocumentType.Document );
MainDocumentPart _part = _docx.MainDocumentPart

string _str = "abc\ndef\ngeh";
string _strArr[] = _str.Split('\n');

foreach (string _line in _strArr)
{
Body _body = new Body();
_body.Append(NewText(_text));
_part.Append(_body);
}
_part.Document.Save();
_docx.Close();

static Paragraph NewText(string _text)
{
Paragraph _head = new Paragraph();
Run _run = new Run();
Text _line = new Text(_text);
_run.Append(_line);
_head.Append(_run);
return _head;
}

Erlenhoff