Free Support Forum - aspose.com

Text replacement lost while converting to PDF

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:
http://www.aspose.com/community/forums/thread/403093/range.replace-misses-text-in-textboxes-when-source-document-is-docx.aspx

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.







Just to clarify a little more:

MemoryStream docStream2 = new MemoryStream();
MemoryStream pdfStream = new MemoryStream();
wordDoc.Save(docStream2, SaveFormat.Docx); // docx saved with text replacements
wordDoc.Save(pdfStream, SaveFormat.Pdf); // pdf saved without text replacement, on top of that, wordDoc’s wordDoc.Range.Text is reverted back to original
Hi Michael,

Thanks for your inquiry. I think, calling Document.UpdatePageLayout method before saving the document to PDF will help.

Please also upgrade to the latest version from the following link:
http://www.aspose.com/community/files/51/.net-components/aspose.words-for-.net/default.aspx

In case the problem still remains and to ensure timely and accurate response, please create a standalone runnable console application (source code without compilation errors) that helps us reproduce your problem on our end and attach it here for testing. As soon as you get this simple application ready, we'll start further investigation into your issue and provide you more information. Also, please attach your input Word document you're getting this problem with here for our reference. Thanks for your cooperation.

Best regards,

I’ve updated to 15.7 and there isn’t a problem anymore. Thanks.