Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open source solution to convert office documents to PDF without using Office automation [closed]

Is there any open-source sdk that I can use in an ASP.Net application to convert any office document to pdf. (I specifically need to convert a DOCX to PDF, but would like the ability to convert Excel and powerpoint files too).

I know that I could use Office automation using code shown below, but I don't want to use Office automation, as its not recommended for use in non-interactive applicationsKB257757

I have found that Aspose has a component that can be used for this (paid solution), but I was wondering if there were any open-source solutions out there.

//reference: Microsoft.Office.Interop.Word.dll
//using Word = Microsoft.Office.Interop.Word;
public static void Convert(string documentFilePath, string outputPath)
    {
        var ap = new Word.Application {Visible = false};

        var document = ap.Documents.Open(documentFilePath);

        document.ExportAsFixedFormat(outputPath,
                       WdExportFormat.wdExportFormatPDF,
                       OptimizeFor: WdExportOptimizeFor.wdExportOptimizeForPrint,
                       BitmapMissingFonts: true, DocStructureTags: false);

        document.Close();
    }

NOTE: I have seen some people recommend using OpenXML for this. But OpenXML does not provide you any method to convert an Office document to a PDF document.

like image 640
Raj Rao Avatar asked Oct 22 '22 14:10

Raj Rao


1 Answers

you can use libreOffice is free license under apache 2.0 i make an example for convert docx to ppt and it's work perfectly , and you can convert to many types like pdf

here my exmaple:

    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 binaryDirectory + "\\Windows\\program\\soffice.exe";
                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();
        //to convert docx to pdf just change input file to docx
        ProcessStartInfo procStartInfo = new ProcessStartInfo(libreOfficePath, 
        string.Format("--convert-to pdf  C:\\test.ppt"));
        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);
        }
    }

thanks , i hope it helpful to you.

like image 78
saleem Avatar answered Oct 27 '22 20:10

saleem