How to read content from microsoft word (docx) file with alignment and font style?

Kaustubh Deshpande 20 Reputation points
2023-02-14T03:38:12.22+00:00

Hey,

Want to read content from microsoft word (docx) file with its alignment and font style. Also is it possible to extract an image present in docx file?

Any answers would be very helpful.

Thanks,

Kaustubh Deshpande

Developer technologies | .NET | Other
{count} votes

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2023-02-14T06:41:47.4033333+00:00

    @Kaustubh Deshpande , Welcome to Microsoft Q&A, if you want to get information from docx file, I recommend that you could install nuget-packge Microsoft.Office.Interop.Word to do it.

    First, please add the using statement:

    using Word = Microsoft.Office.Interop.Word;

    Second, you could try the following code to read some content from docx file:

                Word.Application app = new Word.Application();
                object path = "test.docx";
                Word.Document doc = app.Documents.Open(ref path);
    
                foreach (Paragraph objParagraph in doc.Paragraphs)
                {
                    Console.WriteLine(objParagraph.Range.Text);
                }
    
    

    Third, we could get the image from the docx file in winform app:

     private Word.Application m_word;
            private int m_i;
           
            private void button1_Click(object sender, EventArgs e)
            {
                object missing = Type.Missing;
                object FileName = "\test.docx";
                object readOnly = true;
                m_word = new Word.Application();
                m_word.Documents.Open(ref FileName);
                for (int i = 1; i <= m_word.ActiveDocument.InlineShapes.Count; i++)
                {
                    m_i = i;
                    // CopyFromClipboardShape();
                    Thread thread = new Thread(CopyFromClipbordInlineShape);
                    thread.SetApartmentState(ApartmentState.STA);
                    thread.Start();
                    thread.Join();
                }
    
            }
    
            protected void CopyFromClipbordInlineShape()
            {
                InlineShape inlineShape = m_word.ActiveDocument.InlineShapes[m_i];
                inlineShape.Select();
                m_word.Selection.Copy();
                if (Clipboard.ContainsImage())
                {
                    System.Windows.Forms.IDataObject data = Clipboard.GetDataObject();
                    if (data.GetDataPresent(System.Windows.Forms.DataFormats.Bitmap))
                    {
                        Image image = (Image)data.GetData(System.Windows.Forms.DataFormats.Bitmap, true);
                        image.Save("test.png");
        
    
                    }
                    else
                    {
                        MessageBox.Show("The Data In Clipboard is not as image format");
                     
                    }
                }
                else
                {
                    MessageBox.Show("The Clipboard was empty");
                    
                }
            }
    
    

    Best Regards,

    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.