I use Microsoft.Office.Interop.Word to get words from Word file and then I populate it into a table layout panel. Unfortunately, the words displayed at the table layout panel are not following exact sequence as in the Word file.

How to fix this?

// Open a doc file.
Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
Document d ocument = application.Documents.Open(txtUploadedPathToken.Text);

// Loop through all words in the document.
int count = document.Words.Count;

for (int i = 1; i <= count; i++)
{
    // Write the word.
    string text = document.Words[i].Text;
    //Console.WriteLine("Word {0} = {1}", i, text);
    tableLayoutPanel2.Controls.Add(new Label() { Text = text, Anchor = AnchorStyles.Left, AutoSize = true}, 0, 0);
}
有帮助吗?

解决方案

Your word document reading code seems OK. but you may need to change how you add items to panel. Since you add new items to same position (0,0) it may give incorrect order.

foreach (Microsoft.Office.Interop.Word.Range range in document.Words)
{
   string text = range.Text;
   tableLayoutPanel2.Controls.Add(new Label() { Text = text, Anchor = AnchorStyles.Left, AutoSize = true});
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top