c# - Convert Word document to pdf byte array in memory -
i need open microsoft word document, replace of text convert pdf byte array. have created code involves saving pdf disk , reading bytes memory. avoid writing disk not need save file.
below code have done far...
using system.io; using microsoft.office.interop.word; public byte[] convertwordtopdfarray(string filename, string newtext) { // temporary path save pdf string pdfname = filename.substring(0, filename.length - 4) + ".pdf"; // create new microsoft word application object , open document application app = new application(); document doc = app.documents.open(docname); // make necessary changes document selection selection = doc.activewindow.selection; selection.find.text = "{{newtext}}"; selection.find.forward = true; selection.find.matchwholeword = false; selection.find.replacement.text = newtext; selection.find.execute(replace: wdreplace.wdreplaceall); // save pdf disk doc.exportasfixedformat(pdfname, wdexportformat.wdexportformatpdf); // close document , exit word doc.close(false); app.quit(); app = null; // read pdf array of bytes byte[] bytes = file.readallbytes(pdfname); // delete pdf disk file.delete(pdfname); // return array of bytes return bytes; }
how can achieve same result without writing disk? whole operation needs run in memory.
to explain why need this, want users of asp.net mvc application able upload report template word document when returned browser rendered pdf.
there 2 problems:
the word interop assemblies not capable of writing source disk. because sdk ui based sdk, not meant background stuff since highly depends on ui. (in fact, wrapper around ui application, not logical layer behind it)
you should not use office interop assemblies on asp.net. read considerations server-side automation of office, states:
microsoft not recommend, , not support, automation of microsoft office applications unattended, non-interactive client application or component (including asp, asp.net, dcom, , nt services), because office may exhibit unstable behavior and/or deadlock when office run in environment.
so no go.
Comments
Post a Comment