none
a .NET dll for office 2010 to convert word to pdf and code in c# RRS feed

  • Question

  • Sums it up,

    asp.net app called by a browser

    Office 2010

    Server 2008

    Need to convert office word.doc to pdf.

    Having a heck of time on this one.

     


    Thanks, Doug
    Sunday, May 22, 2011 7:40 PM

All replies


  • Hi,

     

    If you want to convert a word document to a PDF file using Office 2010, you don’t need some custom methods. Office can do this by just saving a document as PDF file.

    The action performs in VBA looks like:

     

     ActiveDocument.ExportAsFixedFormat OutputFileName:= _
     "C:\Draft.pdf", ExportFormat:=wdExportFormatPDF, _
     OpenAfterExport:=True, OptimizeFor:=wdExportOptimizeForPrint, Range:= _
     wdExportAllDocument, From:=1, To:=1, Item:=wdExportDocumentContent, _
     IncludeDocProps:=True, KeepIRM:=True, CreateBookmarks:= _
     wdExportCreateNoBookmarks, DocStructureTags:=True, BitmapMissingFonts:= _
     True, UseISO19005_1:=False
    
    

     

    In C#, you can code like this:

                Word.Document doc = this.Application.ActiveDocument;

                string  path = @"D:\Test\" + "NewPDF" + ".pdf"; 

             

                doc.ExportAsFixedFormat(path, Word.WdExportFormat.wdExportFormatPDF,

                    false, Word.WdExportOptimizeFor.wdExportOptimizeForPrint,

                    Word.WdExportRange.wdExportAllDocument, 1, 1,

                    Word.WdExportItem.wdExportDocumentContent, true, true,

                    Word.WdExportCreateBookmarks.wdExportCreateNoBookmarks, true, true, false);

     

    For more information about ExportAsFixedFormat method, please refer to:

    Document.ExportAsFixedFormat Method

     

    However, if you want to automatic Office in server side, you need to read this KB article first:

    Considerations for server-side Automation of Office

    Developers can use Automation in Microsoft Office to build custom solutions that use the capabilities and the features that are built into the Office product. Although such programmatic development can be implemented on a client system with relative ease, a number of complications can occur if Automation takes place from server-side code such as Microsoft Active Server Pages (ASP), ASP.NET, DCOM, or a Windows NT service.

    I hope this helps.


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.




    Monday, May 23, 2011 4:58 AM
    Moderator
  • Hi Doug

    I don't think Calvin's suggestion is optimal. You really don't want to automate Office apps server-side if you can absolutely help it...

    You might want to check out the Word Automation Services available for Office 2010: http://blogs.office.com/b/microsoft-word/archive/2009/10/26/introducing-word-automation-services.aspx

    Another possibility would be to purchase the license for a 3rd-party conversion tool, rather than roll your own.

    But if you want to roll your own, then what you should be doing is transforming the Open XML docx file to PDF...


    Cindy Meister, VSTO/Word MVP
    Monday, May 23, 2011 5:42 AM
    Moderator
  • Thanks for responding Cindy,

     

    Time is of the essecense so a third party utility could be the answer.

    Do ya know of one that you would reccomend?


    Thanks, Doug
    Monday, May 23, 2011 6:46 AM
  • Hi Doug

    I've never used anything, but I see a lot of people mention the utilities from APOSE and have never heard anything negative about them.


    Cindy Meister, VSTO/Word MVP
    Monday, May 23, 2011 9:04 AM
    Moderator
  • I don't think Calvin's suggestion is optimal. You really don't want to automate Office apps server-side if you can absolutely help it...

    You might want to check out the Word Automation Services available for Office 2010: http://blogs.office.com/b/microsoft-word/archive/2009/10/26/introducing-word-automation-services.aspx

    Another possibility would be to purchase the license for a 3rd-party conversion tool, rather than roll your own.

    But if you want to roll your own, then what you should be doing is transforming the Open XML docx file to PDF...

     

    Hi Cindy,

     

    Thanks for the reminding :-) It is really a good solution to convert word to PDF in service side. However, it is a new feature in SharePoint2010. I noticed it before, but I’m not sure it can fit the request of Doug.

    Word Automation Services Overview

    Word Automation Services: How it Works

     

    Have a nice day,


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, May 23, 2011 1:19 PM
    Moderator
  • Hi Calvin

    Yes, the link I provided in my first response is for Word Automation Services...

    From Doug's response, he seems to prefer a direct conversion, however.


    Cindy Meister, VSTO/Word MVP
    Monday, May 23, 2011 2:37 PM
    Moderator

  • Hi Cindy,

    I am trying to integrate a 3rd party soloution but am having a bit of trouble integrating it as I did not personally write this app.

    Here is the breakdown.

    1. need to read variables fron URL string

    2. Insert into Data Base

    3. Create word doc and replace tokens with variables from step 1

    4. Convert word to PDF.

    5. Fax through InterFax

    The Code

    using System;
    
    
    
    using System.Configuration;
    
    
    
    using System.Collections.Generic;
    
    
    
    using System.Web;
    
    
    
    using System.Web.UI;
    
    
    
    using System.Web.UI.WebControls;
    
    
    
    using System.Data.Sql;
    
    
    
    using System.Data.SqlClient;
    
    
    
    using System.Data.SqlTypes;
    
    
    
    using System.Drawing;
    
    
    
    using System.IO;
    
    
    
    using System.Xml;
    
    
    
    using System.Diagnostics;
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    namespace InterfaxWS
    
    
    
    {
    
    
    
    
    
    
    
     public partial class SendCharFax : System.Web.UI.Page
    
    
    
     {
    
    
    
     protected void Page_Load(object sender, EventArgs e)
    
    
    
     {
    
    
    
      //Get/Set Variables
    
    
    
      string username = "***
      string password = "***";
    
    
    
      string faxNumber = Request.QueryString["faxNumber"];
    
    
    
      string phoneNumber = Request.QueryString["dialerPhone"];
    
    
    
      string firstName = Request.QueryString["firstName"];
    
    
    
      string lastName = Request.QueryString["lastName"];
    
    
    
      string companyName = Request.QueryString["companyName"];
    
    
    
    
    
    
    
      //Save to dB
    
    
    
      SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString);
    
    
    
      connection.Open();
    
    
    
    
    
    
    
      string sql = "Insert into Test_campaign (faxNumber,firstName,lastName,companyName) VALUES ('" + faxNumber + "','" + firstName + "','" + lastName + "','" + companyName + "')";
    
    
    
      SqlCommand cmd = new SqlCommand(sql, connection);
    
    
    
    
    
    
    
      cmd.ExecuteNonQuery();
    
    
    
    
    
    
    
      //Get/Set unique ID
    
    
    
      SqlCommand command2 = new SqlCommand("SELECT SCOPE_IDENTITY()", connection);
    
    
    
      decimal identVal = (decimal)command2.ExecuteScalar();
    
    
    
      string uniqID = "FX-00" + identVal;
    
    
    
    
    
    
    
      //Create the content--DOC and PDF
    
    
    
      string s = cretePdfFaxContentNew(username, password, faxNumber, firstName, lastName, companyName, phoneNumber, uniqID);
    
    
    
    
    
    
    
      //Open InterFax
    
    
    
      interfax.InterFax ifws = new interfax.InterFax();
    
    
    
    
    
    
    
      //chunck
    
    
    
      string UserID = username ;
    
    
    
      string Password = password;
    
    
    
      string FaxNumber = faxNumber;
    
    
    
      string Path = s;	// path to the file to be uploaded
    
    
    
      string SessionID = "";
    
    
    
      int chunkSize = 50000;
    
    
    
    
    
    
    
      // read the file into a byte array
    
    
    
      FileStream fs = new FileStream(Path, FileMode.Open, FileAccess.Read);
    
    
    
    
    
    
    
      interfax.InterFax ws = new interfax.InterFax();
    
    
    
    
    
    
    
      //start upload
    
    
    
      ws.StartFileUpload(UserID, Password, ref SessionID);
    
    
    
    
    
    
    
      // loop until all file data is read
    
    
    
      bool isLast = false;
    
    
    
      int readSoFar = 0;
    
    
    
      while (!isLast)
    
    
    
      {
    
    
    
      byte[] B = new byte[chunkSize];	//this is max buffer size
    
    
    
      int read = fs.Read(B, 0, B.Length);
    
    
    
      readSoFar += read;
    
    
    
      isLast = (readSoFar == fs.Length);
    
    
    
      if (read < B.Length)	// a partial buffer was read => trim B
    
    
    
      {
    
    
    
       byte[] B1 = new byte[read];	//this is max buffer size
    
    
    
       for (int i = 0; i < read; i++) B1[i] = B[i];
    
    
    
       ws.UploadFileChunk(SessionID, B1, isLast);
    
    
    
      }
    
    
    
      else
    
    
    
       ws.UploadFileChunk(SessionID, B, isLast);
    
    
    
      }
    
    
    
    
    
    
    
    
    
    
    
      //Send Fax
    
    
    
      long st = ws.SendfaxEx_2(UserID, Password, FaxNumber, "", null, "pdf", readSoFar.ToString() + "/sessionID=" + SessionID, DateTime.MinValue, 1, "CSID", "", "", "My Subject", "", "A4", "Portrait", true, true);
    
    
    
      
    
    
    
    
    
    
    
    
    
    
    
      //Update dB with the status
    
    
    
      string sql1 = "Update Test_campaign set status='" + st + "' where id=" + identVal;
    
    
    
      SqlCommand cmd1 = new SqlCommand(sql1, connection);
    
    
    
    
    
    
    
      cmd1.ExecuteNonQuery();
    
    
    
      connection.Close();
    
    
    
    
    
    
    
      Response.Write("Status is " + st);
    
    
    
    
    
    
    
    
    
    
    
     }
    
    
    
    
    
    
    
     static void Main(string[] args)
    
    
    
     {
    
    
    
    
    
    
    
      //Convert DOC file to PDF file
    
    
    
      //If you need more information about UseOffice .Net email us at:
    
    
    
      //support[at]sautinsoft.com 
    
    
    
    
    
    
    
    
    
    
    
      SautinSoft.UseOffice u = new SautinSoft.UseOffice();
    
    
    
    
    
    
    
      //Path to any local file
    
    
    
      string inputFilePath = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), @"..\..\..\..\..\..\Testing files\DOC.doc"));
    
    
    
      //Path to output resulted file
    
    
    
      string outputFilePath = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), @"..\..\..\..\..\..\Testing files\Result.pdf"));
    
    
    
    
    
    
    
      //Prepare UseOffice .Net, loads MS Word in memory
    
    
    
      int ret = u.InitWord();
    
    
    
    
    
    
    
      //Return values:
    
    
    
      //0 - Loading successfully
    
    
    
      //1 - Can't load MS Word� library in memory 
    
    
    
    
    
    
    
      if (ret == 1)
    
    
    
      return;
    
    
    
    
    
    
    
      //Converting
    
    
    
      ret = u.ConvertFile(inputFilePath, outputFilePath, SautinSoft.UseOffice.eDirection.DOC_to_PDF);
    
    
    
    
    
    
    
      //Release MS Word from memory
    
    
    
      u.CloseWord();
    
    
    
    
    
    
    
      //0 - Converting successfully
    
    
    
      //1 - Can't open input file. Check that you are using full local path to input file, URL and relative path are not supported
    
    
    
      //2 - Can't create output file. Please check that you have permissions to write by this path or probably this path already used by another application
    
    
    
      //3 - Converting failed, please contact with our Support Team
    
    
    
      //4 - MS Office isn't installed. The component requires that any of these versions of MS Office should be installed: 2000, XP, 2003, 2007 or 2010
    
    
    
      if (ret == 0)
    
    
    
      {
    
    
    
      //Show produced file
    
    
    
      System.Diagnostics.Process.Start(outputFilePath);
    
    
    
      }
    
    
    
     }
    
    
    
    
    
    
    
     private string cretePdfFaxContentNew(string username, string password, string faxNumber, string firstName, string lastName, string companyName, string phoneNumber, string uniqueId)
    
    
    
     {
    
    
    
      string sourcefile = Server.MapPath("DocFile/PacificHealthFaxCover_temp.doc");
    
    
    
      if (System.IO.File.Exists(sourcefile))
    
    
    
      System.IO.File.Delete(sourcefile);
    
    
    
      System.IO.File.Copy(Server.MapPath("DocFile/PacificHealthFaxCover.doc"), sourcefile);
    
    
    
    
    
    
    
    
    
    
    
      string outputfile = Server.MapPath("PDFFiles/" + uniqueId + ".pdf");
    
    
    
    
    
    
    
      if (System.IO.File.Exists(outputfile))
    
    
    
      System.IO.File.Delete(outputfile);
    
    
    
    
    
    
    
      //Create Word DOC
    
    
    
      DoSearchAndReplace(sourcefile, faxNumber, firstName, lastName, companyName, phoneNumber, uniqueId);
    
    
    
    
    
    
    
      ////Convert DOC to PDF
    
    
    
      // Main(); 
    
    
    
     }
    
    
    
    
    
    
    
    
    
    
    
     public void DoSearchAndReplace(String sourcefile, String faxNumber, String firstName, String lastName, String companyName, String phoneNumber, String uniqueId)
    
    
    
     {
    
    
    
      //string processName = "WINWORD";
    
    
    
      //Process[] processes = Process.GetProcessesByName(processName);
    
    
    
    
    
    
    
      //foreach (Process process in processes)
    
    
    
      //{
    
    
    
      // process.Kill();
    
    
    
      //}
    
    
    
    
    
    
    
      
    
    
    
      Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
    
    
    
    
    
    
    
      Microsoft.Office.Interop.Word.Document doc1 = new Microsoft.Office.Interop.Word.Document();
    
    
    
    
    
    
    
      object missing = System.Type.Missing;
    
    
    
    
    
    
    
    
    
    
    
      try
    
    
    
      {
    
    
    
    
    
    
    
      object fileName = sourcefile;
    
    
    
    
    
    
    
      doc1 = word.Documents.Open(ref fileName,
    
    
    
    
    
    
    
       ref missing, ref missing, ref missing, ref missing,
    
    
    
    
    
    
    
       ref missing, ref missing, ref missing, ref missing,
    
    
    
    
    
    
    
       ref missing, ref missing, ref missing, ref missing,
    
    
    
    
    
    
    
       ref missing, ref missing, ref missing);
    
    
    
    
    
    
    
    
    
    
    
      doc1.Activate();
    
    
    
    
    
    
    
    
    
    
    
      foreach (Microsoft.Office.Interop.Word.Range tmpRange in doc1.StoryRanges)
    
    
    
      {
    
    
    
    
    
    
    
    
    
    
    
       tmpRange.Find.Text = "[First Name]";
    
    
    
       tmpRange.Find.Replacement.Text = firstName;
    
    
    
       tmpRange.Find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
    
    
    
       object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
    
    
    
       tmpRange.Find.Execute(ref missing, ref missing, ref missing,
    
    
    
       ref missing, ref missing, ref missing, ref missing,
    
    
    
       ref missing, ref missing, ref missing, ref replaceAll,
    
    
    
       ref missing, ref missing, ref missing, ref missing);
    
    
    
    
    
    
    
       tmpRange.Find.Text = "[Fax Number]";
    
    
    
       tmpRange.Find.Replacement.Text = faxNumber;
    
    
    
       tmpRange.Find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
    
    
    
       replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
    
    
    
       tmpRange.Find.Execute(ref missing, ref missing, ref missing,
    
    
    
       ref missing, ref missing, ref missing, ref missing,
    
    
    
       ref missing, ref missing, ref missing, ref replaceAll,
    
    
    
       ref missing, ref missing, ref missing, ref missing);
    
    
    
    
    
    
    
       tmpRange.Find.Text = "[Phone Number]";
    
    
    
       tmpRange.Find.Replacement.Text = phoneNumber;
    
    
    
       tmpRange.Find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
    
    
    
       replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
    
    
    
       tmpRange.Find.Execute(ref missing, ref missing, ref missing,
    
    
    
       ref missing, ref missing, ref missing, ref missing,
    
    
    
       ref missing, ref missing, ref missing, ref replaceAll,
    
    
    
       ref missing, ref missing, ref missing, ref missing);
    
    
    
    
    
    
    
       tmpRange.Find.Text = "[Date]";
    
    
    
       tmpRange.Find.Replacement.Text = DateTime.Now.ToString("dd/MM/yyyy");
    
    
    
       tmpRange.Find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
    
    
    
       replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
    
    
    
       tmpRange.Find.Execute(ref missing, ref missing, ref missing,
    
    
    
       ref missing, ref missing, ref missing, ref missing,
    
    
    
       ref missing, ref missing, ref missing, ref replaceAll,
    
    
    
       ref missing, ref missing, ref missing, ref missing);
    
    
    
    
    
    
    
       tmpRange.Find.Text = "[Company Name]";
    
    
    
       tmpRange.Find.Replacement.Text = companyName;
    
    
    
       tmpRange.Find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
    
    
    
       replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
    
    
    
       tmpRange.Find.Execute(ref missing, ref missing, ref missing,
    
    
    
       ref missing, ref missing, ref missing, ref missing,
    
    
    
       ref missing, ref missing, ref missing, ref replaceAll,
    
    
    
       ref missing, ref missing, ref missing, ref missing);
    
    
    
    
    
    
    
       tmpRange.Find.Text = "[Number]";
    
    
    
       tmpRange.Find.Replacement.Text = uniqueId;
    
    
    
       tmpRange.Find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
    
    
    
       replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
    
    
    
       tmpRange.Find.Execute(ref missing, ref missing, ref missing,
    
    
    
       ref missing, ref missing, ref missing, ref missing,
    
    
    
       ref missing, ref missing, ref missing, ref replaceAll,
    
    
    
       ref missing, ref missing, ref missing, ref missing);
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
      }
    
    
    
    
    
    
    
      doc1.Save();
    
    
    
    
    
    
    
      }
    
    
    
    
    
    
    
      catch (Exception ex)
    
    
    
      {
    
    
    
    
    
    
    
    
    
    
    
      }
    
    
    
      finally
    
    
    
      {
    
    
    
      
    
    
    
      //doc1.Close(ref missing, ref missing, ref missing);
    
    
    
      ((Microsoft.Office.Interop.Word._Document)doc1).Close();
    
    
    
    
    
    
    
      // word.Application.Quit(ref missing, ref missing, ref missing);
    
    
    
    
    
    
    
      doc1 = null;
    
    
    
      word = null;
    
    
    
    
    
    
    
      }
    
    
    
    
    
    
    
     }
    
    
    
    
    
    
    
    
    
    
    
     }
    
    
    
    
    
    
    
    
    
    
    
    }
    
    
    
    

    Thanks, Doug

    Tuesday, May 24, 2011 2:10 AM
  • Hi Cindy and Calvin,

    Well, I went with a 3rd party dll but still am having issues.

    The problem now is the document not being open exclusively. I am getting the following error:

    The process cannot access the file 'C:\inetpub\InterfaxWS\DocFile\PacificHealthFaxCover_temp.doc' because it is being used by another process.

    Description:

    Line 127:      string sourcefile = Server.MapPath("DocFile/PacificHealthFaxCover_temp.doc");
    Line 128:      if (System.IO.File.Exists(sourcefile))
    Line 129:        System.IO.File.Delete(sourcefile);
    Line 130:      System.IO.File.Copy(Server.MapPath("DocFile/PacificHealthFaxCover.doc"), sourcefile);
    Line 131:
    An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.IO.IOException: The process cannot access the file 'C:\inetpub\InterfaxWS\DocFile\PacificHealthFaxCover_temp.doc' because it is being used by another process.

    Source Error:


    Source File: C:\inetpub\InterfaxWS\SendCharFax.aspx.cs Line: 129

    How would you resolve this?


    Thanks, Doug
    Tuesday, May 24, 2011 2:39 PM
  • <<How would you resolve this?>>

    Contact the manufacturer of the 3rd party app and ask them why their app would be causing this issue when it wasn't happening before. Probably the message you're getting is exactly the problem: that dll has a file lock on the file that needs to be released. Ask the manufacturer how to release it, or whether there's a property you have to query to find out when the dll has finished processing.


    Cindy Meister, VSTO/Word MVP
    Tuesday, May 24, 2011 2:52 PM
    Moderator
  • Hi Cindy and Calvin,

     

    Well, I tried a 3rd party dll and it works fine in a simple test but when a load gets put on it, I am getting permission errors so I assume a file or process is locked. Here is the error:

    Server Error in '/' Application.

    --------------------------------------------------------------------------------

     

    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).

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

     

    Exception Details: System.Runtime.InteropServices.COMException: 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).

     

    Source Error:

     

     

    Line 136:        {

    Line 137:

    Line 138:            Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();

    Line 139:

    Line 140:            Microsoft.Office.Interop.Word.Document doc1 = new Microsoft.Office.Interop.Word.Document();

     

     

    Source File: C:\inetpub\InterfaxWS\SendCharFax.aspx.cs    Line: 138

     

    Stack Trace:

     

     

    [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).]

       System.Runtime.Remoting.RemotingServices.AllocateUninitializedObject(RuntimeType objectType) +0

       System.Runtime.Remoting.Activation.ActivationServices.CreateInstance(RuntimeType serverType) +110

       System.Runtime.Remoting.Activation.ActivationServices.IsCurrentContextOK(RuntimeType serverType, Object[] props, Boolean bNewObj) +94

       System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0

       System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache) +117

       System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache) +247

       System.Activator.CreateInstance(Type type, Boolean nonPublic) +106

       InterfaxWS.SendCharFax.DoSearchAndReplace(String sourcefile, String faxNumber, String firstName, String lastName, String companyName, String phoneNumber, String uniqueId) in C:\inetpub\InterfaxWS\SendCharFax.aspx.cs:138

       InterfaxWS.SendCharFax.cretePdfFaxContentNew(String username, String password, String faxNumber, String firstName, String lastName, String companyName, String phoneNumber, String uniqueId) in C:\inetpub\InterfaxWS\SendCharFax.aspx.cs:117

       InterfaxWS.SendCharFax.Page_Load(Object sender, EventArgs e) in C:\inetpub\InterfaxWS\SendCharFax.aspx.cs:47

       System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +25

       System.Web.UI.Control.LoadRecursive() +71

       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3048

     

    This error is intermittend

    Can either of you shed some light on this?


    Thanks, Doug
    Saturday, May 28, 2011 1:55 PM
  • Hi Doug,

     

    It seems that you are still working on automation for Word in server-side.

    Have you read this : http://support.microsoft.com/kb/257757

    Office applications assume a user identity when the applications are run, even when Automation starts the applications. The applications try to initialize toolbars, menus, options, printers, and some add-ins based on settings in the user registry hive for the user who launches the application. Many services run under accounts that have no user profiles (such as the SYSTEM account or the IWAM_[servername] accounts). Therefore, Office may not initialize correctly on startup.

     

    This seems to lead to a permission denied error.

     

    I would recommend you to use Open XML as an alternative.

     

    I hope this helps


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, May 30, 2011 2:27 AM
    Moderator
  • Hi Calvin,

     

    Thank you for the reply. One Question, the error is occuring intermittently. Is the application initialized on each request? Thereby generating the error only on 3% of attempts.


    Thanks, Doug
    Monday, May 30, 2011 2:36 AM
  • Have a try Spire.Doc, for converting word doc/docx to pdf without ole automation so easy.
    Thursday, June 9, 2011 6:25 AM
  • There are many 3rd party libraries that support convert Word to PDF in C#.

    The tricky part is to export to PDF in ASP.NET, because PDF format requires font information that is usually contained in the system folder not available for medium trust level application like ASP.NET.

    Here is a code sample how to convert Word to PDF and use your custom font folder for reading font data:

    // Load a DOCX document from the file.
    DocumentModel document = DocumentModel.Load(Path.Combine(Server.MapPath("Files/"), "Document.docx"), LoadOptions.DocxDefault);
    
    // By specifying a location that is under ASP.NET application's control, 
    // GemBox.Document can use file system operations to retrieve font data 
    // when exporting to PDF in Medium Trust environment.
    FontSettings.FontsBaseDirectory = Server.MapPath("Fonts/");
     
    Response.Clear();
     
    // PDF exporter cannot write directly
    // to Response.OutputStream. Therefore we use temporary MemoryStream.
    using (MemoryStream documentStream = new MemoryStream())
    {
    	// Stream file to browser in PDF format.
            document.Save(documentStream, SaveOptions.PdfDefault);
    	Response.ContentType = "application/pdf";
    	Response.AddHeader("Content-Disposition", "attachment; filename=Document.pdf");
    	documentStream.WriteTo(Response.OutputStream);
    }
     
    Response.End();

    Thursday, August 2, 2012 6:54 AM