Code Samples - Neevia docCreator

Example 2: Convert a MS Word document into PDF - ASP.NET

1) Add a reference in your Visual Studio project to docCreator library.
To do this:
        a. On the Project menu, click Add Reference;
        b. On the COM tab, locate docCreator Library and then click Select;
        c. Click OK in the Add References dialog box to accept your selections.

2) Add a reference in your Visual Studio project to Microsoft Word.
To do this:
        a. On the Project menu, click Add Reference;
        b. On the COM tab, locate Microsoft Word and then click Select;
        c. Click OK in the Add References dialog box to accept your selections.

3) Configure MS Word like recommended below:
  • type dcomcnfg in the command prompt and press Enter;
  • expand the Component Services group, expand the Computers group, expand the My Computer group, expand the DCOM Config group, find and select the Microsoft Word Document->right mouse click->Properties.
  • click the Identity tab. Check the "This user" checkbox, press Browse and specify the Administrator account;
  • enter and re-enter the Administrator password;
  • click the Security tab. Check the "Use custom access permissions" checkbox, press Edit and add the ASPNET, IUSR_ and IWAM_ user accounts;
  • Note: If you have Windows 2003\2008 also add the "NETWORK SERVICE" user account;
  • check the "Use custom launch permissions" checkbox, press Edit and add the ASPNET, IUSR_ and IWAM_ user accounts;
  • Note: If you have Windows 2003\2008 also add the "NETWORK SERVICE" user account;
  • reboot the computer;
Visual Basic Copy 
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
   <SCRIPT runat="server" language="VB">

     Sub Page_Load(Source As Object, e As EventArgs)

       Dim docToConvert As String = "c:\users\public\test.doc"

       Dim DC As New Neevia.docCreator

       Dim tempFile As String = DC.getParameter("TempDir") & DC.GUID & ".ps"

       DC.setParameter("DocumentOutputFormat", "PDF")
       DC.setParameter("DocumentOutputName", "testDOC_VBNET")
       DC.setParameter("DocumentOutputFolder", "c:\users\public\")

       Dim MSWord As New Microsoft.Office.Interop.Word.Application
       MSWord.DisplayAlerts = False
       On Error Resume Next

       Dim NewDoc As Object
       NewDoc = MSWord.Documents.Open(docToConvert, False, True)
       If Err.Number <> 0 Then
         MSWord = Nothing
         Response.Write("MS Word was unable to open the document!!!")
         Response.End
       End If

       Dim MSWordDialog As Object : MSWordDialog = MSWord.Dialogs(97)
       MSWordDialog.Printer = "Neevia docCreator"
       MSWordDialog.DoNotSetAsSysDefault = 1
       MSWordDialog.Execute()

       NewDoc.PrintOut(False, , , tempFile, , , , , , , True)

       NewDoc.Close(False)
       MSWord.Quit(False)
       MSWord = Nothing

       DC.setInputDocument(tempFile)

       Dim RVal As Integer = DC.create ' Create output document
       DC.fileDelete(tempFile)

       DC = Nothing

       If (RVal <> 0) Then
         Response.Write("Error while creating document!!!")
       Else
         Response.Write("Done Converting !!!")
       End If

     End Sub

   </SCRIPT>

C# Copy 
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
   <SCRIPT runat="server" language="C#">

     void Page_Load(object Source, EventArgs e)
     {

      object fileToConvert = @"c:\users\public\test.doc";

      Neevia.docCreator DC = new Neevia.docCreator();

      DC.setParameter("DocumentOutputFormat", "PDF");
      DC.setParameter("DocumentOutputName", "testDOC_CSHARP");
      DC.setParameter("DocumentOutputFolder", @"c:\users\public\");
      DC.setParameter("PDFAutoRotatePage", "All");

      // This will work only with MS Office 2003+
      Microsoft.Office.Interop.Word._Application MSWord =
       new Microsoft.Office.Interop.Word.Application();
      Microsoft.Office.Interop.Word._Document wordDoc;
      MSWord.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone;
   
      // This will work only with MS Office 2000 or XP
      // Word._Application MSWord = new Word.Application();
      // Word._Document wordDoc;
      // MSWord.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;

      object RN = System.Reflection.Missing.Value;
      object refTrue = true; object refFalse = false;

      try
      {

        // This will work only with MS Word 2003+
        wordDoc = MSWord.Documents.Open(ref fileToConvert,
                    ref refFalse, ref refTrue,
                    ref RN, ref RN, ref RN, ref RN, ref RN, ref RN,
                    ref RN, ref RN, ref RN, ref RN, ref RN, ref RN, ref RN);

        // This will work only with MS Word XP
        // wordDoc = MSWord.Documents.Open(ref fileToConvert, 
        // ref refFalse, ref refTrue,
        // ref RN, ref RN, ref RN, ref RN, ref RN, ref RN,
        // ref RN, ref RN, ref RN, ref RN, ref RN, ref RN);

        // This will work only with MS Word 2000
        // wordDoc = MSWord.Documents.Open(ref fileToConvert, 
        // ref refFalse, ref refTrue,
        // ref RN, ref RN, ref RN, ref RN, ref RN, ref RN,
        // ref RN, ref RN, ref RN);

      } catch
        {
           MSWord.Quit(ref refFalse, ref RN, ref refFalse);
           MSWord = null;
           return;
        }

      object refStr = DC.getParameter("TempDir") + DC.GUID() + ".ps";
      object refRange = 0; object refFrom = 1; object refTo = 999;
      MSWord.ActivePrinter = "Neevia docCreator";

      wordDoc.PrintOutOld(ref refFalse, ref refFalse, ref refRange, ref refStr,
                          ref RN, ref RN, ref RN, ref RN, ref RN, ref RN, ref refFalse, ref RN,
                          ref RN, ref RN);

      wordDoc.Close(ref refFalse, ref RN, ref refFalse);

      MSWord.Quit(ref refFalse, ref RN, ref refFalse);
      MSWord = null;

      DC.setParameter("DocumentResolution", "300");

      DC.setInputDocument(refStr.ToString(), "");
      int RVal = DC.create(); // Create output document
      DC = null;

      if (RVal != 0)
      {
        Response.Write("Error while creating document!!!");
      }
      else
      {
        Response.Write("Done converting !!!");
      }

     }

   </SCRIPT>