Skip to content

vitalii-vov/Maui.PDFView

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Maui.PDFView

Library for display PDF files in .NET MAUI on Android, iOS, MacOS and Windows

NuGet Downloads GitHub License last commit

.NET MAUI .NET 8 .NET 9
Platform Android iOS MacOS Windows
Supported
maui-pdfview-480.mp4

 

Installation

Install-Package Vitvov.Maui.PDFView

 

Usage

Add .UseMauiPdfView() to MauiProgram

using Maui.PDFView;

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
            .UseMauiPdfView();   // <- Write this

        return builder.Build();
    }
}

 
Add PdfView to XAML

<ContentPage
    x:Class="Example.Business.UI.Pages.MainPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:pdf="clr-namespace:Maui.PDFView;assembly=Maui.PDFView">

    <!--
    IsHorizontal — display PDF horizontally
    Uri — path to the file on the device
    MaxZoom — max zoom level
    PageIndex — set the current page by index
    PageChangedCommand — event of changing the current page
    -->
    <pdf:PdfView
        IsHorizontal="{Binding IsHorizontal}"
        Uri="{Binding PdfSource}"
        MaxZoom="4"
        PageIndex="{Binding PageInex}"
        PageChangedCommand="{Binding PageChangedCommand}">
    </pdf:PdfView>

</ContentPage>

Important

To use a component with .net9 add HandlerProperties.DisconnectPolicy="Manual" to PdfView

<pdf:PdfView
    HandlerProperties.DisconnectPolicy="Manual" />

 
Set PdfSource in ViewModel

internal partial class MainPageViewModel : ObservableObject
{
    [ObservableProperty] private string _pdfSource;

    [RelayCommand] private void ChangeUri()
    {
        try 
        {
            //  See the example project to understand how to work with paths.
            PdfSource = "/path/to/file.pdf";
        }
        catch(Exception ex)
        {
             // handle exceptions
        }
    }
}

 

Personalization

You can customize the way pages are displayed by modifying the PageAppearance property in the PdfView component.

<pdf:PdfView Uri="{Binding PdfSource}">

    <!--
    Margin — Adds indents around pages
    ShadowEnabled — Adds a shadow to each page
    Crop — crops out the part around the page
    -->
    <pdf:PdfView.PageAppearance>
        <pdf:PageAppearance 
            Margin="16,8" 
            ShadowEnabled="True"
            Crop="64,32"/>
    </pdf:PdfView.PageAppearance>

</pdf:PdfView>

 

Helper classes implementing IPdfSource

The PdfView component works only with file paths. This is because the native platform components primarily operate with file paths, and handling different PDF data sources directly inside the component would significantly complicate the code.

Therefore, you must always provide a file path regardless of the form your PDF data takes—whether it’s a byte array, a stream, an asset, or a URL.

To simplify working with these data sources, the component includes helper classes that implement the IPdfSource interface:

  • AssetPdfSource
  • ByteArrayPdfSource
  • FilePdfSource
  • HttpPdfSource

Example of using PdfSource

[RelayCommand] private async Task UploadUri()
{      
    var source = new HttpPdfSource("https://www.adobe.com/support/products/enterprise/knowledgecenter/media/c4611_sample_explain.pdf");
    PdfSource = await source.GetFilePathAsync();
}

[RelayCommand] private async Task UploadAsset()
{
    var source = new AssetPdfSource("Example.Resources.PDF.pdf2.pdf");
    PdfSource = await source.GetFilePathAsync();
}

You can also create your own implementation of the IPdfSource interface to address your specific needs.

About

This repository contains a control for .NET MAUI and allows you to display PDF in View

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages