Graphics/images changing while using NodeImporter

Hi there,
I want to Import some of the paragraphs from one document into another document, So to achieve this i used NodeImporter class, but in this process i am loosing the order of the graphics in the source document.
PFA for my test document having arranged graphics in word. please suggest me to import the graphics properly onto another document.

foreach (Paragraph pars in oDoc.GetChildNodes(NodeType.Paragraph, true))
{
    NodeCollection imgNodes = pars.GetChildNodes(NodeType.Shape, true);
    if (imgNodes.Count > 0)
    {
        // Node dstNode = subDoc.ImportNode(tempChild, true, ImportFormatMode.KeepSourceFormatting);
        Node dstNode = importer.ImportNode(tempChild, true);
        subDoc.FirstSection.Body.AppendChild(dstNode);
    }
    else if (pars.ToTxt().ToString().Trim() != "")
    {
        // Node dstNode = subDoc.ImportNode(tempChild, true, ImportFormatMode.KeepSourceFormatting);
        Node dstNode = importer.ImportNode(tempChild, true);
        subDoc.FirstSection.Body.AppendChild(dstNode);
    }
}

Thanks,

Hello

Thanks for your request. I cannot reproduce the problem on my side using the latest version of Aspose.Words and the following code:

Document doc = new Document();
Node insertAfterNode = doc.FirstSection.Body.FirstParagraph;
Document srcDoc = new Document("Additions+and+changes+to+main+functionality.doc");
// Ste Top and Bottom Margins
doc.FirstSection.PageSetup.TopMargin = srcDoc.FirstSection.PageSetup.TopMargin;
doc.FirstSection.PageSetup.BottomMargin = srcDoc.FirstSection.PageSetup.BottomMargin;
ImportDocument(insertAfterNode, srcDoc);
doc.Save("out.doc");
public void ImportDocument(Node insertAfterNode, Document srcDoc)
{
    // Make sure that the node is either a pargraph or table.
    if ((!insertAfterNode.NodeType.Equals(NodeType.Paragraph)) & (!insertAfterNode.NodeType.Equals(NodeType.Table)))
        throw new ArgumentException("The destination node should be either a paragraph or table.");
    // We will be inserting into the parent of the destination paragraph.
    CompositeNode dstStory = insertAfterNode.ParentNode;
    // This object will be translating styles and lists during the import.
    NodeImporter importer = new NodeImporter(srcDoc, insertAfterNode.Document,
        ImportFormatMode.KeepSourceFormatting);
    // Loop through all sections in the source document.
    foreach(Section srcSection in srcDoc.Sections)
    {
        // Loop through all block level nodes (paragraphs and tables) in the body of the section.
        foreach(Node srcNode in srcSection.Body)
        {
            // Let's skip the node if it is a last empty paragarph in a section.
            if (srcNode.NodeType.Equals(NodeType.Paragraph))
            {
                Paragraph para = (Paragraph) srcNode;
                if (para.IsEndOfSection && !para.HasChildNodes)
                    continue;
            }
            // This creates a clone of the node, suitable for insertion into the destination document.
            Node newNode = importer.ImportNode(srcNode, true);
            // Insert new node after the reference node.
            dstStory.InsertAfter(newNode, insertAfterNode);
            insertAfterNode = newNode;
        }
    }
}

Best regards,

Hello there,

After spending couple of hours on this, finally i found the where the exact problem reside.
The code sent by me and sent by your are properly importing the nodes into another document. But the problem is while converting the document into HTML, the images are placing in different places.

please run the following code on my given document, and if you open the HTML in an IE, you can find the difference in original document and content in IE (images are not displaying in proper manner).

Document srcDoc = new Document(@“D:Additions+and+changes+to+main+functionality.doc”);

// Document subDoc = new Document();
string html = ConvertDocumentToHtml(srcDoc, @"D:\sri");
System.IO.File.WriteAllText(@"D:\out.html");
srcDoc.Save(@"D:\out.doc");
private string ConvertDocumentToHtml(Document doc, string imgPath)
{
    Node[] rows = doc.GetChildNodes(NodeType.Row, true).ToArray();
    foreach(Row row in rows)
    {
        row.RowFormat.HeadingFormat = false;
    }

    string html = string.Empty;
    using(MemoryStream htmlstream = new MemoryStream())
    {
        NodeCollection runs = doc.GetChildNodes(NodeType.Run, true);
        foreach(Run run in runs)
        {
            run.Font.Name = "Calibri";
            run.Font.Size = 12;
        }

        foreach(Section section in doc.Sections)
        {
            foreach(FormField field in section.Range.FormFields)
            {
                field.Font.Name = "Calibri";
                field.Font.Size = 12;
            }
        }
        doc.SaveOptions.HtmlExportImagesFolder = imgPath;
        doc.SaveOptions.HtmlExportImageSaving += new ExportImageSavingEventHandler(SaveOptions_HtmlExportImageSaving);
        doc.Save(htmlstream, SaveFormat.Html);
        html = System.Text.Encoding.UTF8.GetString(htmlstream.GetBuffer(), 3, (int) htmlstream.Length - 3);
        // html = html.Replace(strPath, ".../");
        // html = html.Replace("Times New Roman", "Calibri");
        if (!html.Contains("<img"))
        {
            if (doc.ToTxt().Trim() == "")
                html = "";
        }
    }
    return html;
}

int mImgCounter = 0;
void SaveOptions_HtmlExportImageSaving(object sender, ExportImageSavingEventArgs e)
{
    // string ext = Path.GetExtension(e.ImageFileName);
    string ext = ".jpg";
    e.ImageFileName = string.Format("img{0}{1}", mImgCounter, ext);
    mImgCounter++;
}

now you can find the out.html and out.doc.
So is this the way to convert doc into html? if not please suggest me about the conversion of doc into html to fix this…

Thanks,

Hello

Thanks for your inquiry. The problem occurs because, unfortunately, Aspose.Words does not support positioning of floating content upon converting documents to HTML. I linked your request to the appropriate issue. You will be notified as soon as this feature is supported.
Please follow the link to learn details about how Aspose.Words saves document in the HTML/XHTML and MHTML formats:
https://docs.aspose.com/words/net/save-in-html-xhtml-mhtml-formats/
Best regards,

The issues you have found earlier (filed as 1144) have been fixed in this update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(1)