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.

Fri Apr 01, 2022 3:14 am

Hi Team,

Please let me know how to extract image which has flow chart ,I have attached sample file.

Thanks in advance

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

Fri Apr 01, 2022 6:30 am

Hello,

Thank you for your inquiry.
I tested your word file with the following code, and found that extracting SmartArt to image, the content of the SmartArt is lost. I have logged the issue into our bug tracking system with the ticket number SPIREDOC-7631. Our development team will investigate and fix it.
Code: Select all
Document document = new Document();
document.loadFromFile("flowChart.docx");
int index = 0;
// Traverse all Sections of document
for (int i = 0; i < document.getSections().getCount();i++) {
    Section section = document.getSections().get(i);
    //Traverse all paragraphs in Section
    for (int p = 0; p < section.getParagraphs().getCount(); p++) {
        Paragraph paragraph = section.getParagraphs().get(p);
        for (int c = 0; c < paragraph.getChildObjects().getCount(); c++) {
            Document document1 = new Document();
            Section tempSection = document1.addSection();
            DocumentObject dob = paragraph.getChildObjects().get(c);
            if (dob instanceof ShapeObject){
                Paragraph paragraph1 = tempSection.addParagraph();
                ShapeObject shapeObject = (ShapeObject) dob;
                paragraph1.getChildObjects().add(shapeObject.deepClone());
            }
            // Save image
            if (document1.getSections().get(0).getParagraphs().getCount() != 0) {
                Image image = document1.saveToImages(0, ImageType.Bitmap);
                File file = new File(String.format("extractImageAndShape-%d.png", index));
                ImageIO.write((RenderedImage) image, "PNG", file);
                index++;
            }
        }
    }
}

Sincerely,
Annika
E-iceblue support team
User avatar

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

Fri May 27, 2022 9:59 am

Hello,

Thanks for your patience!
Glad to inform you that we just released Spire.Doc for Java Version:10.5.10 which fixes the issue of SPIREDOC-7631.
Please download the new version to test.

Sincerely,
Annika
E-iceblue support team
User avatar

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

Fri Jul 22, 2022 4:50 am

Hi Team,

Thanks for your response.


Can you please provide code to extract smart Art from the document .I have shared sample file in earlier post.


Thank you

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

Mon Jul 25, 2022 9:04 am

Hi,

Thanks for your inquiry.

Sorry currently we do not support do not support extracting SmartArt individually. You can refer to the code below to extract all the images and shapes in the document.
Code: Select all
        Document document = new Document();
        document.loadFromFile("flowChart.docx");
        int index = 0;

        for (int i = 0; i < document.getSections().getCount();i++) {
            Section section = document.getSections().get(i);

            for (int p = 0; p < section.getParagraphs().getCount(); p++) {
                Paragraph paragraph = section.getParagraphs().get(p);
                for (int c = 0; c < paragraph.getChildObjects().getCount(); c++) {
                    DocumentObject dob = paragraph.getChildObjects().get(c);
                    if (dob instanceof ShapeObject){

                        Document document1 = new Document();
                        Section tempSection = document1.addSection();
                        Paragraph paragraph1 = tempSection.addParagraph();
                        ShapeObject shapeObject = (ShapeObject) dob;
                       
                        paragraph1.getChildObjects().add(shapeObject.deepClone());
                        Image image = document1.saveToImages(0, ImageType.Bitmap);
                        File file = new File(String.format("extractImageAndShape-%d.png", index));
                        ImageIO.write((RenderedImage) image, "PNG", file);
                        index++;
                    }
                }
            }
        }
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Tue Jul 26, 2022 1:38 pm

Hi Team,

I tried your code .It is not working if I use ImageIO.write((RenderedImage) image, "PNG", file) .I am getting "Can't create an ImageOutputStream" exception


It is working for document.saveToFile("C:\\Users\\RA\\Desktop\\smartArt\\image.docx", FileFormat.docx);
code :

ShapeObject shapeObj = (ShapeObject) object;
String imageName = "Image_" + imageIndex + "." + "png";
imageFile = new File(tempFilePath + "/" + Id + imageName);
Image image = convertShapeToImage(shapeObj);

BufferedImage img = resizeImage((BufferedImage)image,originalWidth,originalHeight);
ImageIO.write( img, "png", imageFile); // getting error here


Thanks

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

Wed Jul 27, 2022 8:53 am

Hi,

Thanks for your sharing.

I tested your code and didn't get any errors. Since your code doesn't work directly, I modified it slightly which is limited to file paths, names, etc. Here is my modified test code. Please check the it out.
Also, I don't know what the version of Spire.Doc For Java you are using. If it is not the latest version 10.7.4, please try to upgrade and test again.

Full test code:
Code: Select all
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.ShapeObject;
import com.spire.license.LicenseProvider;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {

        Document document = new Document();
        document.loadFromFile("flowChart.docx");
        int index = 0;
        for (int i = 0; i < document.getSections().getCount();i++) {
            Section section = document.getSections().get(i);
            for (int p = 0; p < section.getParagraphs().getCount(); p++) {
                Paragraph paragraph = section.getParagraphs().get(p);
                for (int c = 0; c < paragraph.getChildObjects().getCount(); c++) {
                    DocumentObject dob = paragraph.getChildObjects().get(c);
                    if (dob instanceof ShapeObject){
                        ShapeObject shapeObj = (ShapeObject) dob;
                        String imageName = "Image_" + index + "." + "png";
                        File imageFile = new File(imageName);
                        float originalWidth  = shapeObj.getWidth();
                        float originalHeight = shapeObj.getHeight();
                        Image image = convertShapeToImage(shapeObj);

                        BufferedImage img = resizeImage((BufferedImage)image,originalWidth,originalHeight);
                        ImageIO.write( img, "png", imageFile);    //getting exception here
                        index++;
                    }
                }
            }
        }

    }

    public static Image convertShapeToImage(ShapeObject shapeObj) {
        Document document = new Document();
        Section section = document.addSection();
        // here you need to change the page size of the section and the shape style
        float linewidth = (float) shapeObj.getStrokeWeight();
        shapeObj.setTextWrappingStyle(TextWrappingStyle.Inline);
        shapeObj.setHorizontalOrigin(HorizontalOrigin.Page);
        shapeObj.setVerticalOrigin(VerticalOrigin.Page);
        // set the size based on the shape size and line width
        Dimension dimension = new Dimension();
        dimension.setSize((shapeObj.getWidth() + 2 * linewidth), (shapeObj.getHeight() + 2 * linewidth));
        section.getPageSetup().setPageSize(dimension);
        section.getPageSetup().setMargins(new MarginsF(0, linewidth, linewidth, 0));
        section.addParagraph().getChildObjects().add(shapeObj.deepClone());
        Image image = document.saveToImages(0, ImageType.Bitmap);
        document.close();
        return image;
    }
    public static BufferedImage resizeImage(BufferedImage image, float originalWidth, float originalHeight) {
        int targetWidth = Math.round(originalWidth);
        int targetHeight = Math.round(originalHeight);
        BufferedImage resizedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_ARGB);
        Graphics2D graphics2D = resizedImage.createGraphics();
        // background transparent
        graphics2D.setComposite(AlphaComposite.Src);
        graphics2D.fillRect(0, 0, targetWidth, targetHeight);

        Map<RenderingHints.Key, Object> hints = new HashMap<>();
        hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        hints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        graphics2D.addRenderingHints(hints);

        graphics2D.drawImage(image, 0, 0, targetWidth, targetHeight, null);
        graphics2D.dispose();
        return resizedImage;
    }
}
Sincerely,
Andy
E-iceblue support team
User avatar

Andy.Zhou
 
Posts: 483
Joined: Mon Mar 29, 2021 3:03 am

Return to Spire.Doc