extract image from word file

前端 未结 4 1264
一向
一向 2020-11-29 08:45

I have been trying the following C# code to extract image from the doc file but it is not working:

object missing = System.Reflection.Missing.Value;                  


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-29 09:30

    I had the same problem I used spire library and i got the solution i am giving the link of that library use just add that dll files in your visual studio and copy the below code :

    enter code here
    
    
    
            if (file.ShowDialog() == DialogResult.OK) //if there is a file choosen by the user  
            {
                object path = file.FileName; //get the path of the file  
                object readOnly = true;
    
                Spire.Doc.Document document = new Spire.Doc.Document(file.FileName);
                int index = 1;
    
                //Get Each Section of Document  
                foreach (Spire.Doc.Section section in document.Sections)
                {
                    //Get Each Paragraph of Section  
                    foreach (Spire.Doc.Documents.Paragraph paragraph in section.Paragraphs)
                    {
                        StringBuilder sb = new StringBuilder();
                        sb.AppendLine(paragraph.Text);//storing the text of word in string builder
                        Console.WriteLine(sb);
                        //Get Each Document Object of Paragraph Items  
                        foreach (DocumentObject docObject in paragraph.ChildObjects)
                        {
                            //If Type of Document Object is Picture, Extract.  
                            if (docObject.DocumentObjectType == DocumentObjectType.Picture)
                            {
                                DocPicture pic = docObject as DocPicture;
    
                                String imgName = String.Format(@"E:\C#\OnlineExam\Question\{0}.png", index);
    
                                //Save Image  
                                pic.Image.Save(imgName, System.Drawing.Imaging.ImageFormat.Png);
                                index++;
                            }
                        }
                    }
                }}
    

    You can find dll files from this link

提交回复
热议问题