Scott
Scott

Reputation:

Microsoft Word Document Controls not accepting carriage returns

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??

Upvotes: 1

Views: 6735

Answers (4)

Adrian Kent
Adrian Kent

Reputation: 11

None of the above answers were any help for me.

However I figured out that the InsertAfter method swaps the \n in the original XML string for \v and when this is passed into the content control it then renders correctly.

contentControl.MultiLine = true    
contentControl.Range.InsertAfter(your string)

Upvotes: 1

Bim
Bim

Reputation: 21

Use a function like this:

private static Run InsertFormatRun(Run run, string[] formatText)
{
    foreach (string text in formatText)
    {
        run.AppendChild(new Text(text));
        RunProperties runProps = run.AppendChild(new RunProperties());
        Break linebreak = new Break();
        runProps.AppendChild(linebreak);
    }
    return run;          
}

Upvotes: 2

Erlenhoff
Erlenhoff

Reputation: 1

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;  
}

Upvotes: 0

Zitun
Zitun

Reputation: 388

I got the same problem but it was in a table cell.

I had one string with carriage return (multiple line) into a Text object that was append to a paragraph that was append to a table cell.

=> The carriage return was ignored by word.

Well the solution was simple : Create one paragraph by line and add all of these paragraph's to the table cell.

Upvotes: 0

Related Questions