Since shapes are convert to images while converting docx to HTML and back to docx. I am programitically adding unique id to each shape’s alternative text in the source document . When it is converted to Word again from HTML these shapes are images in the word . Then i programatically iterate through each shap in the new document and copy actual shape from the source document using alternative text as key and then remove the image from the target document . All this works fine but for some reason some shape is not maintaining it’s position in the target document same as source. See the screenshot below
Below is the function that copies the shape from source to target and removes the images which were origianlly converted from shape (textbox etc)
public void CopyShapes(ref Document doc, Document template)
{
List<Node> docShapeNodes = doc.GetChildNodes(NodeType.Shape, true).Where(s => ((Shape)s).AlternativeText.StartsWith("#shape#~")).ToList();
foreach (Node docShapeNode in docShapeNodes)
{
Shape docShape = (Shape)docShapeNode;
string shapeid = docShape.AlternativeText;
List<Node> templateShapeNodes = template.GetChildNodes(NodeType.Shape, true).Where(s => ((Shape)s).AlternativeText.Contains(shapeid)).ToList();
if (templateShapeNodes.Count > 0)
{
Shape templateShape = (Shape)templateShapeNodes[0];
Paragraph para = (Paragraph)docShape.ParentParagraph;
NodeImporter nodeImporter = new NodeImporter(template, doc, ImportFormatMode.KeepSourceFormatting);
Shape docShape_FromTemplate = (Shape)nodeImporter.ImportNode(templateShape, true);
para.InsertAfter(docShape_FromTemplate, docShape);//, docShape);
docShape_FromTemplate.ZOrder = templateShape.ZOrder;
docShape.Remove();
}
}
}
Can you please provide me how i can preserve postions as orginal document
Have also uploaded the in and out filesin.docx (6.9 MB)
out-rountrip.docx (6.3 MB)