Skip to main content
A newer version of this page is available. .

Use Office File API on Linux

  • 4 minutes to read

This tutorial describes how to create a simple Office File API application for .NET on Linux.

Prerequisites

  • .NET Core 3.1 / .NET 5.0 SDK / .NET 6.0 SDK or later

  • A code editor. This tutorial uses Visual Studio Code.

  • Libgdiplus library. This library is required to use System.Drawing in Office File API applications.

    Add the Mono repository to your system to install the latest libgdiplus version or use the corresponding terminal commands below (depending on your Linux distribution):

    sudo apt install -y libgdiplus libc6 libc6-dev
    
  • Install the font libraries below (based on your Linux distribution) to measure, render, and export document text to PDF within your Office File API application:

    sudo apt install -y fontconfig libharfbuzz0b libfreetype6
    

    Tip

    You can also install the ttf-mscorefonts-installer package to add Microsoft TrueType core fonts (Arial, Times New Roman, Courier New, and so on) to your system. Run the following terminal command to install this package in Debian / Ubuntu and accept the license agreement:

    sudo apt-get install ttf-mscorefonts-installer

  • Optional. If your application needs to render JPEG images within PDF files, install the packages below based on your Linux distribution (or any other packages that implement the libjpeg API v6.2 or v8.0):

    sudo apt install -y libjpeg-turbo8
    

Create an Application

  1. Create a folder for you project (OfficeConsoleApp in this example) and open this folder in Visual Studio Code.

  2. Click ViewTerminal in the main menu to open the integrated terminal.

  3. Use the dotnet new command to create a new console application.

    dotnet new console
    

    This command adds the following files to your folder:

    • OfficeConsoleApp.csproj / OfficeConsoleApp.vbproj

    • Program.cs / Program.vb.

  4. Obtain your NuGet Feed URL and install the DevExpress.Document.Processor NuGet package as described in the following topic: Use NuGet Packages to Install Office File API Components.

    Important

    You need a license for the DevExpress Office File API Subscription or DevExpress Universal Subscription to use this package in production code.

    You should also install the DevExpress.Pdf.SkiaRenderer package to enable rendering of PDF page content within your app.

  5. Paste the code below in the Main method of the Program.cs file (or the Program.vb file’s Main procedure for Visual Basic). The following example converts a DOCX file to PDF:

    using DevExpress.XtraRichEdit;
    // ...
    
    static void Main(string[] args) {
        using (var wordProcessor =
            new RichEditDocumentServer()) {
            // Load a document from a file.
            wordProcessor.LoadDocument("Document.docx", DocumentFormat.OpenXml);
            // Export the document to PDF.
            wordProcessor.ExportToPdf("PdfDocument.pdf");
        }
    }
    

Run the Application

Type dotnet run in the terminal window to run the program.

dotnet run

The following image illustrates the result:

Office_Getting_Started_On-Linux_Result

Troubleshooting

System.Drawing.Common is Supported on Windows Only

With the .NET 6 release, the System.Drawing.Common library is compatible with Windows only. Applications that are built for non-Windows platforms now throw the TypeInitializationException and PlatformNotSupportedException exceptions. Refer to the following breaking change for more information: System.Drawing.Common only supported on Windows.

You can set the System.Drawing.EnableUnixSupport runtime configuration switch to true in the runtimeconfig.json file to enable support for non-Windows operating systems and to avoid runtime exceptions:

{
   "configProperties": {
      "System.Drawing.EnableUnixSupport": true
   }
}

However, this runtime configuration switch will not be supported in .NET 7 and higher, and all apps that use Sytem.Drawing.Common on non-Windows platforms will throw PlatformNotSupportedException. To address this issue, we plan to implement our own cross-platform rendering engine based on the SkiaSharp library. See the following blog post for details: DevExpress Cross-Platform Products – Getting Ready for .NET 7.