Hi,
we have some problems with KeepSourceFormatting option when importing textboxes from one document into another.
Please have a look on the three attached Documents: Aspose_NodeImporter_Example.zip (73.7 KB)
The source document “DocToImport3.docx” has some equal formatted Text in paragraphs and textboxes.
The test is set to “Normal” style which is set to “Times new Roman, Size 10”.
The target document (DocBase2.docx) has a different setting for “Normal” (Calibri, size 11).
After import with option KeepSourceFormatting set using the code below the resulting document (ResultP2.docx) is showing the paragraph text with formatting of source document as expected, but the textboxes are in new “Normal” style.
Best regards
Christian
Code:
public void AsposeImportDocInDoc()
{
Aspose.Words.License lic = new Aspose.Words.License();
lic.SetLicense("Aspose.Total.lic");
var docBase = new Aspose.Words.Document(@"C:\@@tmp\DocBase2.docx");
var docToImport = new Aspose.Words.Document(@"C:\@@tmp\DocToImport3.docx");
//Simualtion
var id = Guid.NewGuid();
List<string> fields = new List<string>() { "Dokumentinhalt" };
List<object> fieldsdata = new List<object>() { id };
docBase.MailMerge.Execute(fields.ToArray(), fieldsdata.ToArray());
Aspose.Words.Node insertAfterNode = null;
//We need to find placeholder for content
{
foreach (Section section in docBase.Sections)
{
if (section.Body != null)
{
insertAfterNode = section.Body.ChildNodes.ToArray().ToList().Find(x => x.ToString(SaveFormat.Text).Contains(id.ToString()));
if (insertAfterNode != null)
break;
}
}
if (insertAfterNode == null)
insertAfterNode = docBase.LastSection.Body.LastChild;
}
InsertDocument(insertAfterNode, docToImport);
docBase.Clone().Save(@"C:\@@tmp\ResultP2.docx");
}
/// <summary>
/// Inserts content of the external document after the specified node.
/// Section breaks and section formatting of the inserted document are ignored.
/// </summary>
/// <param name="insertAfterNode">Node in the destination document after which the content
/// should be inserted. This node should be a block level node (paragraph or table).</param>
/// <param name="srcDoc">The document to insert.</param>
static void InsertDocument(Node insertAfterNode, Document srcDoc)
{
// Make sure that the node is either a paragraph 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.OfType<Section>())
{
// 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 paragraph 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;
}
}
}