PDFRasterizer.NET 4.0 is now available. Supports .NET Core. Renders pixel-perfectly.

Render to Xamarin iOS Image view

Given the following Xamarin iOS MainPage.xaml (parts omitted):

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="RenderPDF.MainPage">
            
  <!-- parts omitted -->

  <Image x:Name="image"
         Source="Default.png"
         Aspect="AspectFill"
         VerticalOptions="EndAndExpand" />

</ContentPage>

The following MainPage partial class (code omitted), renders the first page of embedded resource tiger.pdf to the Image above:

public partial class MainPage : ContentPage
{
  private Stream imageStream;
  private Stream pdfStream;
  private Document document;

  public MainPage()
  {
    InitializeComponent();
    
    LoadPDF();
  }

  public void LoadPDF()
  {
    if (pdfStream == null)
    {
      var assembly = typeof(MainPage).Assembly;
      using (Stream resourceStream = assembly.GetManifestResourceStream("RenderPDF.tiger.pdf"))
      {
        if (resourceStream != null)
        {
          pdfStream = new MemoryStream();
          resourceStream.CopyTo(pdfStream);
          
          RenderFirstPage();
        }
      }
    }
  }

  public async void RenderFirstPage()
  {
    Task<ImageSource> result = Task<ImageSource>.Factory.StartNew(() => RenderPage(0));
    image.Source = await result;
  }

  public ImageSource RenderPage(int pageNum)
  {
    if (pdfStream == null)
      return null;

    if (document == null)
      document = new Document(pdfStream);

    var page = document.Pages[pageNum];
    imageStream = new MemoryStream();
    page.SaveAsBitmap(imageStream, ImageEncoding.Png, 72);
    imageStream.Position = 0;
    return ImageSource.FromStream(() => imageStream);
  }
}
Download PDFRasterizer.NET 4.0
We will send you a download link

  • This field is for validation purposes and should be left unchanged.
Why do we ask your email address?
We send tips that speed up your evaluation
We let you know about bug fixes
You can always unsubscribe with one click
We never share your address with a 3rd party
Thank you for your download

We have sent an email with a download link.