Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to Create a Multilevel List from text in c# export to MS Word
Like the following Example:

1) Ibrahim was one of the ____ men.
A) wise
B) more wise
C) wiser
D) wisest

What I have tried:

string fileName = file;
           using (RichEditDocumentServer server = new RichEditDocumentServer())
           {
               Document document = server.Document;

               //Append the first line
               DocumentRange newRange = document.AppendText("");

               //Change the range's format options
               CharacterProperties characterProperties = document.BeginUpdateCharacters(newRange);
               characterProperties.ForeColor = Color.Black;
               characterProperties.FontSize = 11;
               characterProperties.FontName = "Calibri";
               characterProperties.Italic = false;

               document.EndUpdateCharacters(characterProperties);






               string[] items = memo;
               string finalitems = "";
               for (int i = 0; i < items.Length; i++)
               {

                   string gg = items[i].ToString();
                   string[] h = gg.Split(')');
                   string bb = h[0].ToString();
                   if (bb != "")
                   {
                       int n;
                       bool isNumeric = int.TryParse(h[0].ToString(), out n);
                       if (isNumeric == true)
                       {
                           finalitems = finalitems + gg + '\r';
                       }
                       else
                       {
                           if (bb != "A" && bb != "B" && bb != "C" && bb != "D")
                           {
                               finalitems = finalitems + '\t' + gg + '\r';
                           }
                           else
                           {
                               if (bb == "A")
                               {
                                   finalitems = finalitems + '\t' + gg + '\r';
                               }
                               if (bb == "B")
                               {
                                   finalitems = finalitems + '\t' + gg + '\r';
                               }
                               if (bb == "C")
                               {
                                   finalitems = finalitems + '\t' + gg + '\r';
                               }
                               if (bb == "D")
                               {
                                   finalitems = finalitems + '\t' + gg + '\r';
                               }
                           }
                       }
                   }

               }



               document.Sections[0].Page.PaperKind = System.Drawing.Printing.PaperKind.Legal;

               document.Unit = DevExpress.Office.DocumentUnit.Inch;
               // Get the first section in a document
               Section firstSection = document.Sections[0];
               // Create columns and apply them to the document
               SectionColumnCollection sectionColumnsLayout =
                   firstSection.Columns.CreateUniformColumns(firstSection.Page, 0.2f, 2);
               firstSection.Columns.SetColumns(sectionColumnsLayout);


               document.AppendText(finalitems.ToString());

               server.SaveDocument(txtfolder.EditValue.ToString()+"\\"+ fileName+".docx", DocumentFormat.OpenXml);
Posted
Updated 19-Jul-20 8:08am

1 solution

It appears to me that the only change you are making to each string in memo[] is to add a a tab after ')' Could the solution be this simple:
private string data = @"1) Ibrahim was one of the ____ men.
A) wise
B) more wise
C) wiser
D) wisest";

data = data.Replace(") ", ")\t");
Not that simple ? You can use a StringBuilder to reduce the number of strings created:
private string[] splitString = new string[] {")", Environment.NewLine};

string const insertText = ")\t";

var splitdata = data.Split(splitString, StringSplitOptions.RemoveEmptyEntries); 

StringBuilder sb = new StringBuilder();

for (int i = 0; i < splitdata.Length; i += 2)
{
    sb.AppendLine(splitdata[i] + insertText + splitdata[i + 1]);
}

string processeddata = sb.ToString();
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900