We perform text replacements on a word doc and then once its completed, saves it as a pdf.
Upon saving the document as a pdf, we find that the document lost all it’s changes and reverts back to it’s original content.
It seemed odd to us, so we try saving it in other formats in the end (docx, rtf) and it works keeping our changes in the final document. This seems to only be the case for PDF.
Our aspose.word version is 14.11.0.0
This is our process:
1 - Memory stream of a word doc file comes in to a function
FileStream fileStream = File.OpenRead(filePath))
MemoryStream memStream = new MemoryStream();
memStream.SetLength(fileStream.Length);
fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length);
Stream stream = memStream;
2 - load this stream as an aspose words document
Aspose.Words.License license = new Aspose.Words.License();
license.SetLicense(“Aspose.Words.lic”);
Aspose.Words.Document doc = new Aspose.Words.Document(stream);
3 - perform some text replacement
foreach (KeyValuePair<string, string> entry in mappingCollection)
{
foreach (Node node in doc.GetChildNodes(NodeType.Any, true))
{
node.Range.Replace(entry.Key, entry.Value, false, false);
}
}
4 - save the doc into a new pdf stream
MemoryStream pdfStream = new MemoryStream();
wordDoc.Save(pdfStream, SaveFormat.Pdf); *NOTE
pdfStream.Position = 0;
return pdfStream;
*NOTE:
as soon as this line is performed, I check the text the the wordDoc variable by using wordDoc.Range.Text, at this point the the Text is revert to it’s original state before text are replaced on step 3. This does not happen when saveformat is docx or rtf.
Other steps taken:
I’ve seen a similar problem with another user:
this was an older version of aspose and the workaround did not seem to work for me.
I’ve also tried saving the replaced worddoc into a new stream by first creating a new stream AFTER the words are replaced:
MemoryStream docStream = new MemoryStream();
doc.Save(docStream, SaveFormat.Docx); – at this point the doc’s text replacement remains intact
Aspose.Words.Document wordDoc = new Aspose.Words.Document(docStream);
Then saving another to a new stream in the pdf format:
MemoryStream pdfStream = new MemoryStream();
wordDoc.Save(pdfStream, SaveFormat.Pdf); – at this point again the doc somehow reverts back to the original content.
I’m at a lost, any clues would be appreciated.