how to run a .net command programme (word convert to pdf) on ubuntu 20.04

singi 0 Reputation points
2023-06-11T13:14:19.57+00:00

how to run a .net command programme on ubuntu 20.04

the programme is using to convert word to pdf

i tried follow:

  1. code in vs like:

dependence COM: Interop.Microsoft.Office.Interop.Word

using Microsoft.Office.Interop.Word;
using System;

namespace Word2Pdf
{
    internal class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < args.Length; i++)
            {
                Console.WriteLine(i + " : " + args[i]);
            }

            if (args.Length != 2)
            {
                Console.WriteLine("参数错误!,请输入\"exe xxx.docx xxx.pdf\"");
                return;
            }

            Console.WriteLine("转换开始");

            string sourcePath = args[0];
            string targetPath = args[1];

            WordToPDFWithOffice(sourcePath, targetPath);

            Console.WriteLine("转换结束");
        }

        /// <summary>
        /// 将word转成PDF office
        /// </summary>
        /// <param name="sourcePath"></param>
        /// <param name="targetPath"></param>
        /// <returns></returns>
        public static bool WordToPDFWithOffice(string sourcePath, string targetPath)
        {
            bool result = false;
            Application application = new();
            Document? document = null;
            try
            {
                application.Visible = false;
                document = application.Documents.Open(sourcePath);
                /*
                 参数参考 https://docs.microsoft.com/zh-cn/office/vba/api/visio.document.exportasfixedformat
                 */
                document.ExportAsFixedFormat(targetPath, WdExportFormat.wdExportFormatPDF, false);
                result = true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                result = false;
            }
            finally
            {
                document?.Close();
            }
            return result;
        }
    }
}
  1. build to exe
  2. push publish dir to server (without dependence) and run

api install aspnetcore-runtime-6.0 (done)

dotnet Word2Pdf.dll xxx.docx xxx.pdf

but got error:

Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 'type')
   at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions)
   at System.Activator.CreateInstance(Type type)
   at Word2Pdf.Program.WordToPDFWithOffice(String sourcePath, String targetPath) in D:\sharpProjects\Word2Pdf\Word2Pdf\Program.cs:line 40
   at Word2Pdf.Program.Main(String[] args) in D:\sharpProjects\Word2Pdf\Word2Pdf\Program.cs:line 23
Aborted
Not Monitored
Not Monitored
Tag not monitored by Microsoft.
43,973 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Olaf Helper 47,416 Reputation points
    2023-06-12T06:44:33.21+00:00

    Document? document = null; Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 'type')

    As the message says, type Document can't be nullable/null.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.