4

I need to open a Microsoft Word document, replace some of the text then convert to a pdf byte array. I have created code to do this but it involves saving the pdf to disk and reading the bytes back into memory. I would like to avoid writing anything to the disk as I do not need to save the file.

Below is the code I have done so far...

using System.IO;
using Microsoft.Office.Interop.Word;

public byte[] ConvertWordToPdfArray(string fileName, string newText)
{
    // Temporary path to save pdf
    string pdfName = fileName.Substring(0, fileName.Length - 4) + ".pdf";

    // Create a new Microsoft Word application object and open the document
    Application app = new Application();
    Document doc = app.Documents.Open(docName);

    // Make any necessary changes to the 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 the pdf to disk
    doc.ExportAsFixedFormat(pdfName, WdExportFormat.wdExportFormatPDF);

    // Close the document and exit Word
    doc.Close(false);
    app.Quit();
    app = null;

    // Read the pdf into an array of bytes
    byte[] bytes = File.ReadAllBytes(pdfName);

    // Delete the pdf from the disk
    File.Delete(pdfName);

    // Return the array of bytes
    return bytes;
}

How can I achieve the same result without writing to the disk? The whole operation needs to run in memory.

To explain why I need to do this, I want users of an ASP.NET MVC application to be able to upload a report template as a word document which when returned to the browser is rendered as a pdf.

1
  • 1
    As a reply to your comment you can try GemBox.Document. Here is a code for converting your documents to PDF, here is a code for downloading document to ASP.NET MVC client's browser (without first saving it to physical file) and here is a find and replace sample code. Commented Jan 22, 2016 at 9:07

1 Answer 1

4

There are two problems:

  • The Word interop assemblies are usually not capable of writing to another source than disk. This is mainly because the SDK is an UI based SDK, it is not meant to do background stuff since it highly depends on the UI. (In fact, it is just a wrapper around the UI application, not the logical layer behind it)

  • You should not use the Office interop assemblies on ASP.NET. Read Considerations for server-side Automation of Office, which states:

    Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

So this is a no go.

Sign up to request clarification or add additional context in comments.

1 Comment

This makes it clear how to edit a Word document thanks but I still need to know how to convert to PDF. Are there any assemblies which can do the conversion and return an array of bytes which are not expensive?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.