How to generate shape as image in FixedHTML

Hi team,

I’m working on converting a Word document to fixed HTML. However, when I convert documents that contain shapes, they are generated as SVG elements. I’d like to know how to generate these shapes as <img> elements instead. Are there any HtmlFixedSaveOptions available to help with this?

Source Document :
Test_ImageIssue.docx (33.0 KB)

Output :
ImageIssue.zip (55.8 KB)

@AlpeshChaudhariDev I am afraid, there is no option to output shapes as img tag in HTML Fixed format.

Is there any other approach I can use to achieve this?

@AlpeshChaudhariDev You can try converting shapes in your document to images before converting document to HtmlFixed. Also, you can postprocess the generated HtmlFixed and replace SVG with the corresponding images.

1 Like

Thanks @alexey.noskov for quick response.

Could you please provide a code snippet for how to accomplish this using Aspose.Words ? I would like to extract a shape from a Word document, convert it into an image, and then replace the original shape with the image in the document. How can I do this ?

@AlpeshChaudhariDev You can use ShapeRenderer to render shape to image. For example see the following code:

Document doc = new Document("C:\\Temp\\in.docx");

foreach (Shape s in doc.GetChildNodes(NodeType.Shape, true))
{
    // Process only top level shapes.
    if (s.GetAncestor(NodeType.Shape) != null)
        continue;

    // Render the chart to image and save the result into a stream.
    MemoryStream shapeImageStream = new MemoryStream();
    s.GetShapeRenderer().Save(shapeImageStream, new ImageSaveOptions(SaveFormat.Png));

    // Crete a shape with chart image and insert it after the chart shape.
    Shape imageShape = new Shape(doc, ShapeType.Image);
    imageShape.ImageData.ImageBytes = shapeImageStream.ToArray();
    imageShape.Width = s.Width;
    imageShape.Height = s.Height;
    // There might be other shape properties that you need to set, like wrapping or absolute position.
    // ...........

    s.ParentNode.InsertAfter(imageShape, s);
    // Remove chart shape.
    s.Remove();
}
doc.Save("C:\\Temp\\out.docx");
1 Like

s.ParentNode.InsertAfter(imageShape, s);

this statement throwing the error : " Cannot insert a Shape into a GroupShape with a different markup language "

How can i resolve this issue.

Source Document :
Shape.docx (29.7 KB)

@AlpeshChaudhariDev Please modify the code like this:

Document doc = new Document("C:\\Temp\\in.docx");

// Get all shapes and group shapes in the document.
List<ShapeBase> shapes = doc.GetChildNodes(NodeType.Any, true)
    .Where(n => n.NodeType == NodeType.Shape || n.NodeType == NodeType.GroupShape)
    .Cast<ShapeBase>().ToList();

foreach (ShapeBase s in shapes)
{
    // Process only top level shapes.
    if (s.GetAncestor(NodeType.Shape) != null || s.GetAncestor(NodeType.GroupShape) != null)
        continue;

    // Render the chart to image and save the result into a stream.
    MemoryStream shapeImageStream = new MemoryStream();
    s.GetShapeRenderer().Save(shapeImageStream, new ImageSaveOptions(SaveFormat.Png));

    // Crete a shape with chart image and insert it after the chart shape.
    Shape imageShape = new Shape(doc, ShapeType.Image);
    imageShape.ImageData.ImageBytes = shapeImageStream.ToArray();
    imageShape.Width = s.Width;
    imageShape.Height = s.Height;
    // There might be other shape properties that you need to set, like wrapping or absolute position.
    // ...........

    s.ParentNode.InsertAfter(imageShape, s);
    // Remove chart shape.
    s.Remove();
}
doc.Save("C:\\Temp\\out.docx");
1 Like

thanks @alexey.noskov its working fine now.

1 Like