Search code examples
c#novacode-docxcs-script

How to use external library in c# script having different namespace and dll


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();
        }
    }
}

Solution

  • 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();
            }
        }
    }