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. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to create a PDF and load to PdfViewer without saving to disk?

Platform: Xamarin.Forms |
Control: PdfViewer |
Published Date: June 21, 2018 |
Last Revised Date: September 24, 2018

New PDF documents can be created from scratch using the PdfDocument class and save it into a stream, and the stream can be loaded into PdfViewer. So that, saving the document in the disk can be avoided.

 

Portable/.NET Standard

 

Add PdfViewer to the MainPage of the application.

 

XAML

 

Add a method named “CreateNewPDF” with return type Stream in the code behind of the MainPage. In this method create a PdfDocument instance, set its page size, add a new page to it, and draw the string “Hello World!” on the page. Now, save this PdfDocument instance to a stream and return this stream.

C#

 

The previous example illustrates drawing a simple string on a single page PDF. Refer to the following documentation for creating PDF with more pages and different types of contents such as text, images, tables and more.

 

https://help.syncfusion.com/file-formats/pdf/working-with-document

 

In the constructor of the MainPage, call the CreateNewPDF method and load the acquired stream into the PdfViewer.

 

C#

 

Sample link:

http://www.syncfusion.com/downloads/support/directtrac/general/ze/CreateAndLoadDemo1314775267

ADD COMMENT
You must log in to leave a comment
Comments
Héctor Pérez
Sep 21, 2018

The sample works great with the nuget packages included, however, if I update to the new version of PdfViewer (16.3.0.21), it throws an exception:

System.NullReferenceException: Object reference not set to an instance of an object.

Regards.

Reply
Sathish Sivakumar [Syncfusion]
Sep 24, 2018

Hi Héctor,

 

Sorry for the inconvenience caused.

 

We are able to reproduce the issue in our end and we will fix this issue in 2018 Vol 3 SP 1 release, which will be available at the end of October 2018. However as a workaround we can reset the stream position of the created PDF document to 0 before loading it in the PDF viewer. We have also refreshed this change in this KB as well as sample attached in this KB.

 

Regards,

Sathish

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon