Hi,
I have different behavior when inserting one document into another with OpenXml old code and new Aspose code.
In old code we are inserting document over AltChunk before specific element - Sdt or Paragraph. That works fine. With Aspose I am using InsertDocument with DocumentBuilder and have different behavior. Issues :
1. I can not move to Sdt to insert before or after, I have to add paragraph to move DocumentBuilder’s cursor
2. When I move cursor to specific Paragraph and have InsertDocument it inserts in that paragraph, does not append to the parent element - Body per sample
3. In Aspose, when document is inserted there is always added one more empty paragraph after inserted content - that is not OK.
I was suggested for this approach over DocumentBuilder as for more complex scenarios I have to preserve sections and section breaks.
Code samples :
Classic code
public static void InsertFileClassic()
{
string documentPath = “placeholder.docx”;
string insertDocumentPath = “nsertDoc.docx”;
using (WordprocessingDocument document = WordprocessingDocument.Open(documentPath, true))
{
string altChunkId = “AltChunkId” + DateTime.Now.Ticks.ToString().Substring(0, 2);
MainDocumentPart mainPart = document.MainDocumentPart;
AlternativeFormatImportPart chunk =
mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.WordprocessingML,
altChunkId);
using (FileStream fileStream = File.Open(insertDocumentPath, FileMode.Open))
chunk.FeedData(fileStream);
AltChunk altChunk = new AltChunk();
altChunk.Id = altChunkId;
SdtBlock sdt =
(SdtBlock)document.MainDocumentPart.Document.Body.ChildElements.First(it => it is SdtBlock);
sdt.InsertBeforeSelf(altChunk);
sdt.Remove();
mainPart.Document.Save();
}
}
Aspose code
public static void InsertFileAspose()
{
string documentPath = “placeholder.docx”;
string insertDocumentPath = “insertDoc.docx”;
Aspose.Words.Document document = new Aspose.Words.Document(documentPath);
StructuredDocumentTag nodeToInsertBefore =
(StructuredDocumentTag)
document.GetChildNodes(NodeType.StructuredDocumentTag, true).ToArray().FirstOrDefault();
using (FileStream fileStream = new FileStream(insertDocumentPath, FileMode.Open))
{
Aspose.Words.Document documentToInsert = new Aspose.Words.Document(fileStream);
DocumentBuilder builder = new DocumentBuilder(document);
// I need empty paragraph to move cursor to it as I can not move to Sdt that is not inline
Aspose.Words.Paragraph newParagraph = new Aspose.Words.Paragraph(nodeToInsertBefore.Document);
newParagraph.InsertNodeBefore(nodeToInsertBefore);
builder.MoveTo(nodeToInsertBefore);
builder.MoveTo(newParagraph);
builder.InsertDocument(documentToInsert, ImportFormatMode.UseDestinationStyles);
nodeToInsertBefore.RemoveNodeSafely();
newParagraph.RemoveNodeSafely();
}
document.Save(documentPath);
}
Sample documents are in attachment :
1. placeholder.docx is destination document where should be inserted another document
2. insertDoc.docx is source document that should be inserted on specific place
Thanks,
Rastko