How to display PDF document in Xamarin.Forms and draw the ink annotation over the PDF document?
At present we do not have support for displaying the PDF document in Xamarin. However, as a workaround we can export the PDF document to Images in Windows Azure Service and display the exported images of PDF document in Xamarin.Forms.
The following code snippet illustrates the conversion of PDF document to Images in Windows Azure service.
C#
public MemoryStream GetPage(int pageNum)
{
if (pdfDocumentView != null && pageNum >= 0 && pageNum < pdfDocumentView.PageCount)
{
//Export the PDF pages to image
Bitmap bmp = pdfDocumentView.ExportAsImage(pageNum);
MemoryStream stream = new MemoryStream();
//Save the exported PDF page as Image
bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
return stream;
}
return null;
}
Please find the Windows Azure Service sample that converts PDF page to image in the following link.
https://www.syncfusion.com/downloads/support/directtrac/153866/ze/PdfViewerExportService-90689727
The following code snippet illustrates how to display the image of PDF page in Xamarin with the help of Windows Azure Service.
C#
//Initialize the Windows Azure Service which exports the PDF document to images
net.cloudapp.pdfviewerexportservice.Service1 service = new net.cloudapp.pdfviewerexportservice.Service1();
bool result;
//Load the Pdf Document into service
service.LoadDocument("Object C succinity.pdf", out m_totalPage, out result);
//Convert the specified page of the PDF document to Image
m_currentBitmap = Xamarin.Forms.DependencyService.Get<IPdfImage>().GetPage(0);
//Set the exported image to display on the viewport
this.SetImageBitmap(BitmapFactory.DecodeStream(m_currentBitmap));
This application can also be extended to add ink annotation to the PDF document and save the changes in PDF document in Xamarin platform. The following link contains the sample application which allows the user to add ink annotations to the PDF document.
https://www.syncfusion.com/downloads/support/directtrac/153866/ze/PDFViewer-789627111
The following screenshots illustrates the operations that can be performed in the above sample.
To navigate between pages | To add Ink annotation | To save the PDF document |