How to Create PDF Document and Load It in the .NET MAUI PDF Viewer?
This article demonstrates how to create a PDF document using Syncfusion PDF library and load it in the Syncfusion PDF Viewer to view the PDF.
New PDF documents can be created from scratch using the PdfDocument class in the Syncfusion PDF library, saved into a stream, and then loaded into the PDF Viewer. This way, saving the document to disk can be avoided.
Step 1: Install Required NuGet Package
To get started, create a new .NET MAUI app and ensure the following packages are installed in your project:
You can install this package using the NuGet Package Manager or through the NuGet CLI.
Step 2: Initialize the PDF Viewer in XAML
Start by adding the Syncfusion PDF Viewer control to your XAML
file.
a. Add the Syncfusion namespace in your MainPage.xaml:
Define the XAML namespace to enable access to the PDF Viewer control.
XAML
xmlns:syncfusion="clr-namespace:Syncfusion.Maui.PdfViewer;assembly=Syncfusion.Maui.PdfViewer"
b. Add the PDF Viewer control to your layout:
Initialize the SfPdfViewer in the XAML file. This will display the PDF Viewer in your app. You can load any PDF document into this Viewer.
XAML
<syncfusion:SfPdfViewer x:Name="pdfViewer" DocumentSource="{Binding PdfDocumentStream}"/>
Step 3: Create a PDF document
Create a ViewModel class and add a method named CreatePDF with a return type of Stream
in the PdfViewerViewModel
class. In this method create a PdfDocument instance, add a new page to it, and draw the string “This PDF was created using the Syncfusion PDF Library.” on the page. Next, save the PdfDocument instance to a stream and return the stream.
C#
private Stream 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);
// Define the text to be drawn.
string text = "This PDF was created using the Syncfusion PDF Library.";
// Measure the text size.
Syncfusion.Drawing.SizeF textSize = font.MeasureString(text);
// Calculate the center position for the text.
float pageWidth = page.GetClientSize().Width;
float pageHeight = page.GetClientSize().Height;
float xPosition = (pageWidth - textSize.Width) / 2;
float yPosition = (pageHeight - textSize.Height) / 2;
// Draw the centered text.
graphics.DrawString(text, font, PdfBrushes.Black, new Syncfusion.Drawing.PointF(xPosition, yPosition));
// Creating the stream object.
MemoryStream stream = new MemoryStream();
// Save the document into memory stream.
document.Save(stream);
// Close the document.
document.Close(true);
if (stream.CanSeek)
stream.Position = 0;
return stream;
}
The above code 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
Step 4: View the PDF in PDF Viewer
In the constructor of the PdfViewerViewModel
class, assign the created PDF stream to the pdfDocumentStream field by calling the CreateNewPDF method and loading the acquired stream into the PDF Viewer.
C#
public PdfViewerViewModel()
{
//Set the created PDF stream to pdfDocumentStream for loading it in PDF Viewer
pdfDocumentStream = CreatePDF();
}
Step 5: Run the app
Finally, run your .NET MAUI app. The created PDF, containing the string “This PDF was created using the Syncfusion PDF Library.”, will be loaded in the PDF Viewer.
Output
Sample Link :
View sample in GitHub
Conclusion
I hope you enjoyed learning about how to create PDF document and load It in the .NET MAUI PDF Viewer.
You can refer to our MAUI PDFViewer feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.
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, Direct-Trac, or feedback portal. We are always happy to assist you!