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
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;
}
}
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;
}
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;
}