Articles in this section
Category / Section

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

3 mins read

New PDF documents can be created from scratch using the PdfDocument class and saved into a stream, and the stream can be loaded into PdfViewer. This way, saving the document to disk can be avoided.

 

Portable/.NET Standard

 

Add PdfViewer to the MainPage of the application.

 

XAML

<Grid x:Name="pdfViewGrid">
          <syncfusion:SfPdfViewer x:Name="pdfViewerControl" />
 </Grid>

 

Add a method named "CreateNewPDF" with the 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#

private Stream CreateNewPDF()
{
      MemoryStream memoryStream = new MemoryStream();
 
      // Create a new PDF document
      PdfDocument document = new PdfDocument();
 
      // Set the page size
      document.PageSettings.Size = PdfPageSize.A4;
 
      // Add a page to the document
      PdfPage page = document.Pages.Add();
 
      // Create PDF graphics for the page
      PdfGraphics graphics = page.Graphics;
 
      // Set the font
      PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);
 
      // Draw the text
      graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new PointF(0, 0));
 
      // Save the document
      document.Save(memoryStream);
 
      // Close the document
      document.Close(true);
 
      if (memoryStream.CanSeek)
               memoryStream.Position = 0;
            
      return memoryStream;
}

The previous example illustrates drawing a simple string on a single-page PDF. Refer to the following documentation for creating PDFs with more pages and different types of content, 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#

public MainPage()
{
    InitializeComponent();
    Stream fileStream = CreateNewPDF();
    pdfViewerControl.LoadDocument(fileStream);
}

 

Sample link:


Conclusion

I hope you enjoyed learning about how to create a PDF and load it into PdfViewer without saving to disk.

You can refer to our Xamarin PDF Viewer feature tour page to know about its other groundbreaking features. You can also explore our documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (1)
Please  to leave a comment
HP
Héctor Pérez

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.

SS
Sathish Sivakumar

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

Access denied
Access denied