We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to render a PDF on the screen, using Xamarin.Forms, WPF and Syncfusion controls?

I'm writing a Xamarin.Forms application that targets WPF and MacOS. In the shared Xamarin.Forms project I have a basic custom control with properties such as the Source, holding the stream that represents the PDF that I've gathered from a web service or the file system. In the platform specific projects I have custom renderers that render the PDF file according to the platform specific needs.

For MacOS the problem was easily solved. However after some time I've found out that the WPF toolkit doesn't provide a native control for PDF. After some research I've opted for using the Syncfusion PDF control for WPF.

I don't need something very fancy, just be able to display a PDF file on the screen, without toolbars or sidebars, so, following the instructions in the documentation I chose to use the PdfDocumentView to load PDF files.

However when using the PdfViwerControl or the PdfDocumentView I don't seem to be able to load more than two pages at a time. The vertical scroll bar of these controls never show up, however the horizontal scroll bar works just fine.

Here's the code I have so far (Loading a local hardcoded local file for testing purposes):

View

<?xml version="1.0" encoding="UTF-8" ?>
<Grid
    x:Class="MyApp.Views.DocumentOverview"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:controls="MyApp.Controls">

    <Grid.RowDefinitions>
        <RowDefinition Height="auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <Grid Grid.Row="0">
        
    </Grid>

    <Grid Grid.Row="1" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
        <controls:CustomPdfViewer x:Name="myPdfViewer"
                BackgroundColor="Transparent"
                HorizontalOptions="FillAndExpand"
                VerticalOptions="FillAndExpand" />
    </Grid>
</Grid>

Control

namespace MyApp.Controls
{
    public class CustomPdfViewer : ContentView
    {
        //Still to implement the Stream property
    }
}

Custom Renderer

[assembly: ExportRenderer(typeof(CustomPdfViewer), typeof(PdfLoaderRenderer))]
namespace MyApp.WPF.CustomRenderers
{
    class PdfLoaderRenderer : ViewRenderer<CustomPdfViewer, PdfDocumentView>
    {
        PdfDocumentView _pdfDocument = new PdfDocumentView();

        protected override void OnElementChanged(ElementChangedEventArgs<CustomPdfViewer> e)
        {
            base.OnElementChanged(e);

            if (e.NewElement != null)
            {
                SetNativeControl(_pdfDocument);
            }
        }

        protected async override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName.Equals(nameof(Element.Source)))
            {
                await LoadFile();
            }

            base.OnElementPropertyChanged(sender, e);
        }

        private async Task LoadFile()
        {
            if (Element != null && Element.Source != null)
            {
                _pdfDocument.Load(@"C:\Sources\Document.pdf");
            }
        }
    }
}

I can see a second page when I put the control inside of a ScrollView and force the HeightRequest to something like 3000 or 4000. But no matter what, even when there's space for other pages by forcing the height, they are not rendered. I believe that happens because of the virtualization features of the Syncfusion control, in order to improve performance:



But it seems like in this case that might be affecting the way I should use the control. How can I make proper use of the Syncfusion PDF Viewer for WPF in order to render a PDF with several pages on the screen? Using a WebView right now isn't much of an option, and as far as third party controls, I only have access to Syncfusion controls.

2 Replies 1 reply marked as answer

NK Navaneetha Kannan Sudalai Muthu Syncfusion Team August 7, 2020 08:39 PM UTC

Hi Arthur,  

We will analyze your requirement by creating a sample from the code you have shared and will update the details on August 11, 2020.  

Best, 
Navaneetha Kannan 



AV Ashokkumar Viswanathan Syncfusion Team August 11, 2020 12:01 PM UTC

Hi Arthur,  
We have created a workable sample to make use of Syncfusion WPF PDF viewer in the Xamarin.Forms project that targets WPF based on the code and scenario that you have described in your last update. The document is rendered completely, and the scrollbar issues have not occurred. The sample can be downloaded from the following location. 
Note: As your requirement is to simply display the PDF document rendering all the pages, we have used PdfDocumentView control in the above sample. However, we have also tested the sample with using PdfViewerControl in the same and it is worked as expected.  
Please try the above sample and let us know whether provided sample helps you. If you are still facing the same issue with the above provided sample or in your environment, kindly share the following details to analyze more on this and assist you with the better solution.  
1. Simple sample/ modify the above sample to reproduce the issue.     
2. Target framework.  
3. Xamarin forms version.  
4. Replication procedure/video to reproduce the issue.  
5. Syncfusion Essential Studio version that you are using. 
 
Regards, 
Ashok Kumar Viswanathan. 


Marked as answer
Loader.
Live Chat Icon For mobile
Up arrow icon