I want to find and replace some variables in a Word document to form a new document (the old one is still intact). The new Word document is then sent as a PDF to customers.
I have been able to do the find and replace and created a new XML newWordText
but I am stuck at converting it back to Word document / PDF or to byte array which I can return back client or send PDF as email.
Examples I saw on stackoverflow/internet have been helpful up to this moment
But I can't get to work as I expected
UPDATE: I have tried to convert the xml to byte and get base64String. I use online base64 convert to pdf but I got
Something went wrong couldn't open the file
public async Task<string> CreatePolicyDocument(string PolicyNumber)
{
// create document
var files = @"C:\Users\pathtodocument\Testdocument.docx";
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(files, true))
{
// Insert other code here.
string docText;
using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
{
docText = sr.ReadToEnd();
}
Regex regexText = new Regex("XCONCLUSION_DATEX");
var newWordText = regexText.Replace(docText, "Hi Everyone!");
using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
{
sw.Write(newWordText);
Encoding encoding = Encoding.UTF8;
byte[] docAsBytes = encoding.GetBytes(newWordText);
File.WriteAllBytes("hello.pdf", docAsBytes);
var file = Convert.ToBase64String(docAsBytes);
}
}
// send message
//
return "";
}