This folder contains a sample project that demonstrates how to create a PDF document using the Syncfusion PDF library and load it into the PDF Viewer directly, without saving it to disk.
Steps to create a PDF using the Syncfusion PDF library and load it in PdfViewer without saving to disk
- Create a PDF document using the Syncfusion PDF library.
- Load the created PDF stream to the PDF Viewer.
- .NET MAUI installed.
- Syncfusion.Maui.PdfViewer NuGet package.
In this example, we will load the PDF document through MVVM binding. Create a new C# file named PdfViewerViewModel.cs. Create the PDF using the PDF library and save the PDF as a stream. Create the following method in the PdfViewerViewModel class. Create the PDF using the PDF library and save the PDF as stream. Create the below method in the PdfViewerViewModel class.
private MemoryStream CreatePDF()
{
//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Set the standard font.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);
//Draw the text.
graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new Syncfusion.Drawing.PointF(0, 0));
//Creating the stream object.
MemoryStream stream = new MemoryStream();
//Save the document into memory stream.
document.Save(stream);
//Close the document.
document.Close(true);
return stream;
}
Initialize the PDF Viewer control in the .NET MAUI application and load the PDF stream into the PDF Viewer by binding the PDF Viewer's DocumentSource
to the PdfDocumentStream property of the PdfViewerViewModel class.
<syncfusion:SfPdfViewer x:Name="pdfViewer" DocumentSource="{Binding PdfDocumentStream}">
</syncfusion:SfPdfViewer>
In your PdfViewerViewModel class, set the created stream to the PdfDocumentStream property from the CreatePDF method.
public class PdfViewerViewModel
{
public Stream PdfDocumentStream { get; private set; }
public PdfViewerViewModel()
{
PdfDocumentStream = CreatePDF();
}
}
- Build and deploy your .NET MAUI application on your preferred platform (Android, iOS, Windows, and Mac Catalyst).
- The PDF Viewer should display the "This PDF was created using the Syncfusion PDF Library." PDF document directly from the stream.