Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Sun Feb 14, 2021 2:57 pm

Hi Team,

We need to extract the drawn shapes (example: data flow diagram by using shapes inside MS Word) as Image or HTML format, so that the same can be referred or consumed in other operations.
Please help on the solution.

Thanks,
Pradeep

pr20080798
 
Posts: 148
Joined: Wed Jan 20, 2021 1:15 pm

Mon Feb 15, 2021 10:28 am

Hello,

Thanks for your inquiry and sorry for the late reply as weekend.

Please refer to the following code to extract the shape in the word to image.
Code: Select all
        static void Main(string[] args)
        {
            Document document = new Document();
            document.LoadFromFile(@"J:\test\pic.docx", FileFormat.Docx);
            int i = 0;
            foreach(Section sec in document.Sections)
            {
                foreach(Paragraph para in sec.Paragraphs)
                {
                    foreach(DocumentObject obj in para.ChildObjects)
                    {
                        if(obj.DocumentObjectType == DocumentObjectType.ShapeGroup)
                        {
                            ShapeGroup shape = obj as ShapeGroup;
                            Image image = ConvertShapeToImage(shape);
                            string name = string.Format("Image-{0}.png", i);
                            image.Save(name, System.Drawing.Imaging.ImageFormat.Png);
                            i++;
                        }

                    }
                }
            }
        }
        private static Image ConvertShapeToImage(ShapeGroup obj)
        {
            Document doc = new Document();
            Section section = doc.AddSection();
            section.AddParagraph().ChildObjects.Add(obj.Clone());
            Image image = doc.SaveToImages(0, ImageType.Bitmap);
            doc.Close();
            return image;
        }
    }

If the code does not match your needs, please provide us with your input file and expect result file.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Wed Feb 17, 2021 11:20 am

Hi, Thanks for the reply.

Could you please share the same piece of code in Java? There are few methods in that block which we do not see in Java library.

We will try that code and see if it works, otherwise we will share our own word file and expected result file.

Thanks in advance.

pr20080798
 
Posts: 148
Joined: Wed Jan 20, 2021 1:15 pm

Thu Feb 18, 2021 2:34 am

Hello,

Please refer to the following code to extract shape to image in Java.
Code: Select all
   public static void main(String[] args) throws IOException {

      Document document = new Document();
      document.loadFromFile("E:\\testdoc\\haveShape.docx");
      
      int shapeCount = 0;
      
      for(int i =0; i< document.getSections().getCount(); i++) {
         Section section = document.getSections().get(i);
         for (int j = 0; j < section.getParagraphs().getCount(); j++) {
            Paragraph paragraph = section.getParagraphs().get(j);
            for (int k = 0; k < paragraph.getChildObjects().getCount(); k++) {
               DocumentObject obj = paragraph.getChildObjects().get(k);
               if(obj.getDocumentObjectType() == DocumentObjectType.Shape_Group) {
                  ShapeGroup group = (ShapeGroup)obj;
                  String name = String.format("image-%d.png", shapeCount);
                  Image image = ConverShapeToImage(group);
                  BufferedImage bufferedImage = (BufferedImage) image;
                  ImageIO.write(bufferedImage, "png", new File(name));
                  shapeCount++;
               }
            }
         }
      }
      
   }
   
   public static Image ConverShapeToImage(ShapeGroup group) {
      Document document = new Document();
      Section section = document.addSection();
      section.addParagraph().getChildObjects().add(group.deepClone());
      Image image = document.saveToImages(0, ImageType.Bitmap);
      document.close();
      return image;
   }

If the code does not match your needs, please provide us with your input file and expected result file.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Fri Mar 05, 2021 8:27 am

Hello,

Hope you are doing well!

Has the issue been solved now? Could you please give us some feedback at your convenience?

Thanks in advance.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Mon Jul 26, 2021 5:15 am

Hi Marcia,

The given solution is working perfectly. We noticed, the extracted images (.png) come in fixed dimension even for single small shape of Word file. Is there any way to make the high resolution dynamically based on the actual objects?

Thanks in advance!!

pr20080798
 
Posts: 148
Joined: Wed Jan 20, 2021 1:15 pm

Mon Jul 26, 2021 6:36 am

Hello,

Thanks for your inquiry!

Since our Spire.Doc only supports to convert document to picture page by page, I am afraid that there is no better way to extract the group shapes or shapes based on the actual objects. Even in the MS Word, the group shapes or shapes cannot be extracted as a picture separately, the best way to achieve it is copying them to a new document, and convert the document to picture as the method I provided last time. Hope you can understand.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Fri Aug 13, 2021 7:05 am

Thank you Marcia for confirming the same.

Also, please confirm what are shapes supported by Spire to extract them as images for the given solution. We tried L-Shape, Cloud, Cube, Smiley Face etc. but these are not extracting even treating as Shape object in your condition.

Thanks in advance!!

pr20080798
 
Posts: 148
Joined: Wed Jan 20, 2021 1:15 pm

Fri Aug 13, 2021 9:36 am

Hello,

Thank you for your feedback.
I used the following code to extract the shapes (L-Shape, Cloud, Cube, Smiley Face etc) you mentioned, all of them could be extracted successfully. Please have a try, if there is any question, please provide your sample Word including shapes to help us look into your issue further.
Code: Select all
    public static void main(String[] args) throws IOException {
        Document document = new Document();
        document.loadFromFile("inputFile");
        int shapeCount = 0;
        for (int i =0; i< document.getSections().getCount(); i++){
            Section sections = document.getSections().get(i);
            for (int j = 0; j < sections.getParagraphs().getCount(); j++) {
                Paragraph paragraph = sections.getParagraphs().get(j);
                for (int k = 0; k < paragraph.getChildObjects().getCount(); k++) {
                    DocumentObject obj = paragraph.getChildObjects().get(k);

                    if (obj instanceof ShapeObject){
                        ShapeObject shapeObject = (ShapeObject) obj;
                        String name = String.format("image-%d.png", shapeCount);
                        Image image = ConverShapeToImage(shapeObject);
                        BufferedImage bufferedImage = (BufferedImage) image;
                        ImageIO.write(bufferedImage, "png", new File(name));
                        shapeCount++;
                    }
                }
            }
        }
    }
    public static Image ConverShapeToImage(ShapeObject shapeObject) {
        Document document = new Document();
        Section section = document.addSection();
        section.addParagraph().getChildObjects().add(shapeObject.deepClone());
        Image image = document.saveToImages(0, ImageType.Bitmap);
        document.close();
        return image;
    }

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1648
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.Doc