How to extract native Word shape objects, wordart or SmartArt as images?

Hi,
Is there a way to convert native word objects such as arrow, rectangle etc or Smart Art as image ?
They appear as Shape objects but we are unable to transform it to an image.
Thanks.
Manan

@manandmehta

Thanks for your query. It seems you have the Group Shapes in your document. You can render Group Shape to image using GetShapeRenderer object as following. Hopefully it will help you to accomplish the task. However, if it does not help then please share your sample word document as ZIP file here. We will look into it and will guide you accordingly.

Document doc = new Document(@"Input.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
int imageIndex = 1;
// Get collection of shapes
NodeCollection Gshapes = doc.GetChildNodes(NodeType.GroupShape, true);
// Loop through all Group Shapes and render to image
foreach (GroupShape shape in Gshapes) {
ImageSaveOptions imageOptions = new ImageSaveOptions(SaveFormat.Jpeg);
shape.GetShapeRenderer().Save("Groupimage" + imageIndex + ".jpeg", imageOptions);
}
1 Like

This is great. Thank you for the quick reply. It works like a charm when I have grouped the shapes together.
Is there a way I can group subsequent shapes together as a group ? I have attached an example. Shape1 is grouped and the code you provided works on it. Shape 2 is not grouped and I want to group both rectangle and arrow as one group. temp.zip (25.6 KB)

Also, it is not picking up a SmartArt. temp.zip (32.1 KB). Example attached.

@manandmehta

Thanks for your feedback. These shapes are not group shapes but child of a paragraph. You can use paragraph node to extract the shapes as a group. Please check following sample code to extract the parent paragraph of the shapes. Hopefully it will help you to accomplish the task.

Document doc = new Document("temp.docx");
String filename = null;
int i = 0;
Document dstDoc = new Document();
DocumentBuilder builder = new DocumentBuilder(dstDoc);

foreach (Shape shape in doc.GetChildNodes(NodeType.Shape, true))
{
    Paragraph paragraph = shape.ParentParagraph;
    if (paragraph != null && paragraph.GetChildNodes(NodeType.Shape, true).Count > 0 && paragraph.ParentNode != null)
    {
        NodeImporter importer = new NodeImporter(doc, dstDoc, ImportFormatMode.KeepSourceFormatting);
        Node newNode = importer.ImportNode(paragraph, true);
        Paragraph para = dstDoc.FirstSection.Body.FirstParagraph;
        builder.CurrentParagraph.ParentNode.InsertAfter(newNode, para);
        ImageSaveOptions options = new ImageSaveOptions(SaveFormat.Png);
        options.PageIndex = 0;
        filename = "output_" + i + ".png";
        dstDoc.Save(filename, options);
        i++;
        paragraph.Remove();
    }

}