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.
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.