Hi Team,
Please let me know how to extract image which has flow chart ,I have attached sample file.
Thanks in advance
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++;
}
}
}
}
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++;
}
}
}
}
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;
}
}