0

I want to use novacode-docx in cs-script. how can I give correct reference to the assembly. I tried following but didn't work around the assembly reference missing.

//css_reference D:\lib\DocX.dll;
using System;
using System.Diagnostics;
using System.Windows.Forms;

class Script
{
    [STAThread]
    static public void Main(string[] args)
    {
        using (DocX doc = DocX.Create(@"C:\Users\name\Desktop\test.docx"))
        {
             doc.PageLayout.Orientation = Orientation.Landscape;
             var table = doc.AddTable(12, 2); 
             doc.InsertTable(table);
             doc.Save();
        }
    }
}
Rahul
  • 10,830
  • 4
  • 53
  • 88

4 Answers4

2

You cannot reference an explicit path like that for presumably security reasons. The assembly must be placed in one of the following locations and referenced as //css_reference DocX.dll;

File location The assembly to be loaded must be from one of the following locations (the order indicates the assembly search priority):

  • the same directory where the script is
  • Default Script Library directory Script Library (%CSSCRIPT_DIR%\Lib)
  • Custom Script Library directory(s) (specified in the configuration console SearchDirs)
  • GAC

See here for more info: http://www.csscript.net/help/using_.net_assemblies.html

Drop the Docx.dll into the same folder as where the cs script is and try this:

//css_reference DocX.dll;
using System;
using System.Diagnostics;
using System.Windows.Forms;
using Novacode;

class Script
{
    [STAThread]
    static public void Main(string[] args)
    {
        using (DocX doc = DocX.Create(@"C:\Users\name\Desktop\test.docx"))
        {
             doc.PageLayout.Orientation = Orientation.Landscape;
             var table = doc.AddTable(12, 2); 
             doc.InsertTable(table);
             doc.Save();
        }
    }
}
Rodders
  • 2,425
  • 2
  • 20
  • 34
  • I already tried that. The problem is that the name of dll file is DocX.dll and using directive is using Novacode. I am not getting any of them working. – Rahul Jun 06 '16 at 10:53
  • So the DocX object name conflicts with another assembly? – Rodders Jun 06 '16 at 10:59
  • I don't know what is going on but its is not working. Error returned by cs-script is The type or namespace 'docx' could not be found (are you missing a using directive or assembly reference?). I think the problem is difference in name space and the dll file. – Rahul Jun 06 '16 at 11:09
  • I've updated your code to include the reference and a using, give that a try. – Rodders Jun 06 '16 at 11:17
  • I had the same Idea and it worked. Thanks. I will delete my answer. – Rahul Jun 06 '16 at 11:23
1

Have you read this link

To add a reference in Visual C# In Solution Explorer, right-click the project node and click Add Reference. In the Add Reference dialog box, select the tab indicating the type of component you want to reference. Select the components you want to reference, and then click OK.

Without VS:

Go to the csproj file there is a <ItemGroup> where references can be added:

<ItemGroup>
    <Content Include="libs\...">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
...

There you can add libs.

wake-0
  • 3,918
  • 5
  • 28
  • 45
1

DocX seems to be available on NuGet, so I would heavily recommend fetching the dependency from there rather than having it in a file on your local system. (This helps ensuring repeatable builds, should you share this code with others, with packaging your application, and it will also make it easier to upgrade DocX if a new version is released.)

If you're using Visual Studio, you can right-click the project in Solution Explorer and choose "Manage NuGet Packages..." to open a dialog that helps you install the package, or you can open Package Manager Console and enter Install-Package DocX.

If you're building on .NET Core without Visual Studio, just add "DocX": "1.0.0.19" to the dependencies node of your project.json.

When the package is installed, you can just do using DocX; like with any other namespace import.

Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
  • Thanks. Unfortunately I am using cs-script using notepad++. – Rahul Jun 06 '16 at 10:37
  • Hm. Quick browsing the documentation, it seems that the `//css_reference` combined with a `using` statement is what you're looking for. However, CS-Script seems to solve the same problems as .NET Core does for you, but in a less-standard (and probably soon obsolete) way - I would imagine you can quite easily convert your project to the [dotnet cli](http://dotnet.github.io/) if you wanted to. – Tomas Aschan Jun 06 '16 at 10:41
  • Is it not possible to add it to the `csproj` file? – wake-0 Jun 06 '16 at 10:43
  • @KevinWallis: Yes, of course it is. But manually editing `csproj` files is error-prone and tedious; it's better to find a solution where you don't need to :) – Tomas Aschan Jun 06 '16 at 10:45
0

both required in order to use docx.

//css_reference DocX.dll;
using Novacode;

You can also give reference to any place like

//css_reference D:\lib\DocX.dll;
using Novacode;
Rahul
  • 10,830
  • 4
  • 53
  • 88