Convert Office to PDF using C#

Nutrient MAUI SDK is a MAUI library that can be used for converting Office documents to PDF directly in your app. To convert Office documents such as DOCX, XLSX, and PPTX to PDF, Nutrient MAUI SDK relies entirely on its own technology built from the ground up, and it doesn’t depend on third-party tools such as LibreOffice or Microsoft Office. For more information on the supported Office formats, see the list of supported file types.

Try for Free

Converting Office documents to PDFs after loading

To convert an Office document to a PDF after loading it in the Nutrient viewer, follow these steps.

  1. Load the source Office document. Optional: To load the document without a visual interface visible to the user, use the headless parameter.

  2. Optional: Make changes to the document. For example, add annotations.

  3. Convert the source document to a PDF with the ExportDocumentAsync method. Optional: Use PDFAConformance in ExportConfiguration to create a PDF/A document. For more information, see converting PDF to PDF/A.

  4. Save the output document. The ExportDocumentAsync method returns a Task that resolves to a byte[] that contains the output PDF document. You can use the resulting byte[] to save or persist the output PDF in storage. For more information on saving or persisting the exported byte[], see the guides on saving a document.

The example below loads an Office document and exports it to a PDF:

var documentFromLocalStorage =
    await PSPDFKitController.LoadDocument(docxFilePath, PSPDFKitController.CreateViewerConfiguration());

var exportConfiguration = documentFromLocalStorage.CreateExportConfiguration();
var exportedDocumentAsPDF =
    await documentFromLocalStorage.ExportDocumentAsync(exportConfiguration);

To export it to a PDF with conformance level PDF/A-4f, set the PDFAConformance while exporting:

var exportConfiguration = documentFromLocalStorage.CreateExportConfiguration();
exportConfiguration.PDFAConformance = PDFAConformance.PDFA_4F;
var exportedDocumentAsPDF =
   await documentFromLocalStorage.ExportDocumentAsync(exportConfiguration);

For more control over exporting documents, use IExportConfiguration. If you pass null instead, the default configuration will be used:

var exportOptions = _document.CreateExportConfiguration();
{
    ExcludeAnnotations = true,
    ExportForPrinting = true,
    ExportIncrementally = false,
    Flatten = true,
};

var exportedDocumentContent = await _document.ExportDocumentAsync(exportConfiguration);

Other conversion guides

For more advanced scenarios, use advanced access APIs and the following guides to convert Office documents to PDFs.

  1. Converting Office documents with custom fonts to PDFs after loading

  2. Converting Office documents to PDFs without loading

  3. Converting Office documents with custom fonts to PDFs without loading