none
Converion of word to PDF to render it in the PDF (ASP.NET C#) RRS feed

  • Question

  • Dear Team,

    I have an asp.net application, in that one functionality I need to show the word document in the browser by highlighting few keywords in the word document.

    Finding the keyword to highlight I am using below code.

    Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();

    public Microsoft.Office.Interop.Word.Document wordDocument { get; set; }  

    string dummyFile = “File path”;

    wordDocument = appWord.Documents.Open(dummyFile); 

    var range = wordDocument.Range();

    while (range.Find.Execute(word))

    {

    Microsoft.Office.Interop.Word.Range tmpRange = wordDocument.Range(range.Start, range.End);

    tmpRange.HighlightColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdDarkYellow;

    tmpRange.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdYellow;

    matchCount++;

    }

    Microsoft.Office.Interop.Word.Range tmpRange1 = wordDocument.Range(0, 0);

    if (matchCount > 0)

    {

    tmpRange1.Text = "Match Count : " + matchCount + "\n\n";

           tmpRange1.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdBlue;

           tmpRange1.Font.Size = 12;

           tmpRange1.Font.Bold = 1;

           tmpRange1.InsertParagraphBefore();

    }

    wordDocument.ExportAsFixedFormat(dummyFile + ".pdf", WdExportFormat.wdExportFormatPDF);

    var bytes = System.IO.File.ReadAllBytes(dummyFile.ToLower().Replace(".doc","").Replace("x", "") + "" + ".pdf");

    Context.Response.ContentType = "application/pdf";

    Context.Response.AddHeader("Content-disposition", "inline");

    Context.Response.BinaryWrite(bytes.ToArray());

    The above code is working as expected, Finding the keyword occurrences count and inserting the text in 0<sup>th</sup> position of word document. Then converting the word to PDF using WdExportFormat.wdExportFormatPDF.

    The problem here to be solved is, this code shows me the PDF only if a administrator logged in the server. After log off it shows the below exception.

    System.Runtime.InteropServices.COMException (0x8000401A): Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 8000401a The server process could not be started because the configured identity is incorrect. Check the username and password. (Exception from HRESULT: 0x8000401A). at System.Runtime.Remoting.RemotingServices.AllocateUninitializedObject(RuntimeType objectType) at System.Runtime.Remoting.Activation.ActivationServices.CreateInstance(RuntimeType serverType) at System.Runtime.Remoting.Activation.ActivationServices.IsCurrentContextOK(RuntimeType serverType, Object[] props, Boolean bNewObj) at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) at System.Activator.CreateInstance(Type type, Boolean nonPublic) at System.Activator.CreateInstance(Type type

    Even, I have created a local user and configured DCOM given the credentials. Still it is not working.

    Any help in this would be highly appreciable.


    Selvakumar Rathinam

    Thursday, February 2, 2017 10:01 AM

Answers

  • Hi Selvakumar Rathinam,

    This error is not related with word interop but it is related with Windows server.

    so I don't have much information regarding that to solve the issue.

    you can refer the link below to view the documentation which describes the help to solve this issue.

    Windows Server Troubleshooting: "The RPC server is unavailable"

    to avoid all this issue I suggest to try to use Open XML.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 6, 2017 6:32 AM
    Moderator
  • Hi,

    Thanks

    I have created the local admin in the server and configured the Folders,Dcom configurations & RPS locator Service

    now it working fine.

    Thanks

    Selva


    Selvakumar Rathinam

    Monday, February 13, 2017 9:22 AM

All replies

  • Hi Selvakumar Rathinam,

    in some conditions and situations Office interop will not work properly on serverside.

    All current versions of Microsoft Office were designed, tested, and configured to run as end-user products on a client workstation. They assume an interactive desktop and user profile. They do not provide the level of reentrancy or security that is necessary to meet the needs of server-side components that are designed to run unattended.

    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.

    If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution.

    for more information please visit link below.

    Considerations for server-side Automation of Office

    you get error ,"The server process could not be started because the configured identity is incorrect.".

    so it's looks like "Identity issue". (please refer point "User Identity" in link above).

    in this type of situation you can find alternative ways to fulfil your requirements like you can try to use Open XML.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, February 3, 2017 3:26 AM
    Moderator
  • Hi Deepak,

    Thanks for your response. to solve the identity issue I have created a local user with administrator rights and configured DCOM (this user) and put the username and password. 

    now, I am getting the below exception, 

    System.Runtime.InteropServices.COMException (0x800706BA): The RPC server is unavailable

     Can you please help me in this ?

    Thanks

    Selva


    Selvakumar Rathinam

    Friday, February 3, 2017 12:15 PM
  • Hi Selvakumar Rathinam,

    This error is not related with word interop but it is related with Windows server.

    so I don't have much information regarding that to solve the issue.

    you can refer the link below to view the documentation which describes the help to solve this issue.

    Windows Server Troubleshooting: "The RPC server is unavailable"

    to avoid all this issue I suggest to try to use Open XML.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 6, 2017 6:32 AM
    Moderator
  • Hi,

    Thanks

    I have created the local admin in the server and configured the Folders,Dcom configurations & RPS locator Service

    now it working fine.

    Thanks

    Selva


    Selvakumar Rathinam

    Monday, February 13, 2017 9:22 AM
  • I used Spire.Doc to convert Word to PDF using C# in ASP.NET and it works fine for my requirements.

    //create document object
    Document document = new Document();
    //load file
    document.LoadFromFile(@"E:\work\Documents\example.docx");
    //convert Word to PDF
    document.SaveToFile("Wrod2PDF.pdf", FileFormat.PDF);

     
    Friday, March 31, 2017 9:53 AM