-1

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 "";
}
techstack
  • 1,367
  • 4
  • 30
  • 61
  • 1
    Did you search for "c# convert word to pdf"? I did using Google, and got lots of useful links, including [this question on StackOverflow](https://stackoverflow.com/questions/607669/how-do-i-convert-word-files-to-pdf-programmatically). What havbe you tried, and what's not working about your attempt? – mason Feb 22 '22 at 16:02

1 Answers1

0

Write should be able to convert docx to PDF on windows 10/11 (I have used it many times), however here on 7 I have to use XPS to show how to do similar enter image description herebut could print to my remote GhostScript printer (if it was switched on) so the key to using write is to specify printer & driver then output filename so in your case it would be:-

write /pt input.docx "Microsoft Print to PDF" "Microsoft Print to PDF" output.pdf

Many users complain but its not headless or silent , my answer is try running many printers silent and you will have issues since they heavily need/use WINDOWS workstation GDI. The other complaint is write will not handle my Word DocX its too specialist, well in that case nothing other than Word is likely to cut it either. Keep your templates simple and Universally acceptable.

As to your other problem docx files are simply zips so use Windows 10/11 Tar -a blah blah or on Windows 7/8/9 then use powershell to re-zip the file after your FnR changes.

K J
  • 8,045
  • 3
  • 14
  • 36