Есть ли способ вставить целый документ docx в другой, не используя altchunks? Проблема заключается в том, что после вставки мне нужно объединить полученный документ с другим, используя DocumentBuilder из OpenXml Powertools и не поддерживает документы, содержащие altchunks.Как вставить документ docx в другой docx в определенном месте без altchunk
1
A
ответ
0
Хорошо, мне удалось найти решение. Чтобы вставить документ в определенную позицию, я разделил исходный документ на два источника для DocumentBuilder, затем я создал источник из вставленного документа. В конце концов, я построил новый документ с этими тремя источниками и, похоже, работает нормально.
Я ищу абзац, чтобы разделить исходный документ на заполнитель, например «@@ insert @@».
Bellow - это код, если ему это нужно.
var paragraph = DestinationDocument.MainDocumentPart.Document.Descendants<OpenXmlParagraph>().FirstOrDefault(item => item.InnerText.Contains(placeHolder));
if (paragraph != null)
{
var idOfParagraph =
DestinationDocument.MainDocumentPart.Document.Descendants<OpenXmlParagraph>()
.ToList()
.IndexOf(paragraph);
//save and close current destination document
SaveChanges(destinationFilePath, false);
var sources = new List<Source>();
var originalDocument = new WmlDocument(destinationFilePath);
sources.Add(new Source(originalDocument, 0, idOfParagraph, true)); // add first part of initial document
var documentToBeInserted = new WmlDocument(docFilePath);
sources.Add(new Source(documentToBeInserted, true)); // add document to be inserted
sources.Add(new Source(originalDocument, idOfParagraph + 1, true)); // add rest of initial document
var newDestinationDocument = DocumentBuilder.BuildDocument(sources); // build new document
newDestinationDocument.SaveAs(destinationFilePath); // save
// re-open destination document
DestinationDocument = WordprocessingDocument.Open(Path.GetFullPath(destinationFilePath), true);
}