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.
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.
-
Load the source Office document. Optional: To load the document without a visual interface visible to the user, use the
headless
parameter. -
Optional: Make changes to the document. For example, add annotations.
-
Convert the source document to a PDF with the
ExportDocumentAsync
method. Optional: UsePDFAConformance
inExportConfiguration
to create a PDF/A document. For more information, see converting PDF to PDF/A. -
Save the output document. The
ExportDocumentAsync
method returns aTask
that resolves to abyte[]
that contains the output PDF document. You can use the resultingbyte[]
to save or persist the output PDF in storage. For more information on saving or persisting the exportedbyte[]
, 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.