С помощью приведенного ниже кода я пытаюсь изменить слово документ, который я получаю как байт []. При отладке я вижу, что mainPart.Document содержит изменения, внесенные методом replaceText. Но когда я возвращаю выходной поток и загружаю файл, он все еще находится в исходном состоянии. Произведенные изменения отсутствуют. Есть идеи, почему изменения не сохраняются? Любая помощь/идеи приветствуются.
Код: Выделить всё
public MemoryStream ReplaceTextInWordDocument(byte[] fileBytes, Dictionary replacements)
{
// Create a new MemoryStream for the output
MemoryStream outputStream = new MemoryStream();
// Copy the byte array to the output MemoryStream
outputStream.Write(fileBytes, 0, fileBytes.Length);
outputStream.Position = 0; // Reset the position to the beginning
// Open the Word document from the MemoryStream
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(outputStream, true))
{
MainDocumentPart mainPart = wordDoc.MainDocumentPart;
// Perform replacements in the document
foreach (var replacement in replacements)
{
ReplaceText(mainPart.Document, replacement.Key, replacement.Value);
}
// Save changes to the document
mainPart.Document.Save();
}
// Reset the position of the output stream to the beginning before returning
outputStream.Position = 0;
return outputStream;
}
Подробнее здесь: https://stackoverflow.com/questions/790 ... er-modifyi