Skip to main content

Use Office File API to Combine PDF Documents

  • 3 minutes to read

This topic explains how to use PDF Document API to merge multiple PDF files into a single file and then display this file in the DevExpress PDF viewer for .NET MAUI.

DevExpress Office File API in .NET MAUI app - Merge multiple PDF docs

Create a .NET MAUI App

Follow the instructions in the following section to create an app with our .NET MAUI components: Get Started.

Note that the DevExpress library for .NET MAUI targets only Android and iOS platforms. See also: Supported Platforms.

Add Office File API NuGet Packages

Install the following NuGet packages to use PDF Processing API in your app:

See also: Use Office File API in .NET MAUI Applications (macOS, iOS, Android).

Combine PDF Documents

The PdfDocumentProcessor allows you to manipulate PDF documents (for example, you can add new pages to a PDF document, remove pages, or customize document content).

Create a PdfDocumentProcessor instance and call the CreateEmptyDocument(String) method to create an empty PDF document (ResultingDoc.pdf) used to store other PDF document content.

Call the AppendDocument(Stream) method to append desired PDF files to the newly created document. Note that in this tutorial, PDF files are stored in the project’s Resources/Raw folder.

using DevExpress.Maui.Pdf;
using DevExpress.Pdf;

namespace OfaProcessPdfDocs {
    public partial class MainPage : ContentPage {
        ///...
        async Task CombineDocs(string fileName1, string fileName2) {
            string targetFile = Path.Combine(FileSystem.Current.AppDataDirectory, "ResultingDoc.pdf");
            using PdfDocumentProcessor pdfDocumentProcessor = new PdfDocumentProcessor();
            pdfDocumentProcessor.CreateEmptyDocument(targetFile);
            await AppendDoc(pdfDocumentProcessor, fileName1);
            await AppendDoc(pdfDocumentProcessor, fileName2);
        }
        async Task AppendDoc(PdfDocumentProcessor pdfDocumentProcessor, string fileName) {
            using Stream fileStream = await FileSystem.Current.OpenAppPackageFileAsync(fileName);
            using MemoryStream ms = new MemoryStream();
            await fileStream.CopyToAsync(ms);
            pdfDocumentProcessor.AppendDocument(ms);
        }
    }
}

Show the Resulting PDF Document

In this tutorial, the PDF viewer shows the resulting PDF document. The following markup adds a PdfViewer instance to the page:

<ContentPage ...
             xmlns:dx="http://schemas.devexpress.com/maui"
             ...>
    ...
        <dx:PdfViewer x:Name="pdfViewer"/>
    ...
</ContentPage>

Assign the resulting document’s file path to the PdfViewer.DocumentSource property to display it in the PDF Viewer:

using DevExpress.Maui.Pdf;
using DevExpress.Pdf;

namespace OfaProcessPdfDocs {
    public partial class MainPage : ContentPage {
        public MainPage() {
            InitializeComponent();
            ShowCombinedPdfDocs();
        }
        async void ShowCombinedPdfDocs() {
            await CombineDocs("FileName1.pdf", "FileName2.pdf");
            string resultingPath = Path.Combine(FileSystem.Current.AppDataDirectory, "ResultingDoc.pdf");
            pdfViewer.DocumentSource = PdfDocumentSource.FromFile(resultingPath);
        }
        //...
    }
}

Results

The code snippets below contain the complete code mentioned in the previous sections:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:dx="http://schemas.devexpress.com/maui"
             xmlns:ios="clr-namespace:Microsoft.Maui.Controls.PlatformConfiguration.iOSSpecific;assembly=Microsoft.Maui.Controls"
             ios:Page.UseSafeArea="true"
             x:Class="OfaProcessPdfDocs.MainPage"
             xmlns:local="clr-namespace:OfaProcessPdfDocs">
    <Grid>
        <dx:PdfViewer x:Name="pdfViewer"/>
    </Grid>
</ContentPage>
using DevExpress.Maui.Pdf;
using DevExpress.Pdf;

namespace OfaProcessPdfDocs {
    public partial class MainPage : ContentPage {
        public MainPage() {
            InitializeComponent();
            ShowCombinedPdfDocs();
        }
        async void ShowCombinedPdfDocs() {
            await CombineDocs("FileName1.pdf", "FileName2.pdf");
            string resultingPath = Path.Combine(FileSystem.Current.AppDataDirectory, "ResultingDoc.pdf");
            pdfViewer.DocumentSource = PdfDocumentSource.FromFile(resultingPath);
        }
        async Task CombineDocs(string fileName1, string fileName2) {
            string targetFile = Path.Combine(FileSystem.Current.AppDataDirectory, "ResultingDoc.pdf");
            using PdfDocumentProcessor pdfDocumentProcessor = new PdfDocumentProcessor();
            pdfDocumentProcessor.CreateEmptyDocument(targetFile);
            await AppendDoc(pdfDocumentProcessor, fileName1);
            await AppendDoc(pdfDocumentProcessor, fileName2);
        }
        async Task AppendDoc(PdfDocumentProcessor pdfDocumentProcessor, string fileName) {
            using Stream fileStream = await FileSystem.Current.OpenAppPackageFileAsync(fileName);
            using MemoryStream ms = new MemoryStream();
            await fileStream.CopyToAsync(ms);
            pdfDocumentProcessor.AppendDocument(ms);
        }
    }
}