Domanda

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);
}
È stato utile?

Soluzione

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});
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top