Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any free library to covert doc to pdf without using Microsoft.Office.Interop.Word in c# environment

Tags:

c#

pdf

itext

I meet a problem about how to using c# without using Microsoft.Office.Interop.Word to covert doc to pdf. I have tried some third party solution, like spire.Doc, but they are not free, and also I found DocX_Doc in nuget, but it seems there is no tutorial about that.Is anyone knows a free solution for this problem, or any instruction about DocX_Doc. Thanks a lot.

like image 210
abramhum Avatar asked Dec 19 '25 11:12

abramhum


1 Answers

you can use libreOffice is free license under apache 2.0 https://www.libreoffice.org/ i already tested it and it's working perefectly just you need to download soffice.exe file to convert to pdf you also can convert docx to image and other type.

here my example code that i tested it:

    static string getLibreOfficePath()
    {
        switch (Environment.OSVersion.Platform)
        {
            case PlatformID.Unix:
                return "/usr/bin/soffice";
            case PlatformID.Win32NT:
                string binaryDirectory = 
          System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                return @"C:\Program Files\LibreOffice\program\soffice.exe";
            default:
                throw new PlatformNotSupportedException("Your OS is not supported");
        }
    }

    static void Main(string[] args)
    {
        string libreOfficePath = getLibreOfficePath();

        ProcessStartInfo procStartInfo = new ProcessStartInfo(libreOfficePath, 
        string.Format("--convert-to pdf C:\\test.docx")); //test.docx => input path
        procStartInfo.RedirectStandardOutput = true;
        procStartInfo.UseShellExecute = false;
        procStartInfo.CreateNoWindow = true;
        procStartInfo.WorkingDirectory = Environment.CurrentDirectory;

        Process process = new Process() { StartInfo = procStartInfo, };
        process.Start();
        process.WaitForExit();

        // Check for failed exit code.
        if (process.ExitCode != 0)
        {
            throw new LibreOfficeFailedException(process.ExitCode);
        }
    }

i hope it's helpfull for you. Thanks.

like image 156
saleem Avatar answered Dec 21 '25 00:12

saleem