PDF Document Visualization
The Telerik UI for .NET MAUI PDF Viewer control enables you to visualize PDF documents through the Source
property of type Telerik.Maui.Controls.PdfViewer.DocumentSource
.
Visualize Documents from a Fixed Document
The PDF Viewer allows you to load the PDF document from a stream by using the RadFixedDocument
element.
By using this approach, you have more control over the loading process, for example, you can modify the document after importing it and before assigning it as a Source
to the PDF Viewer control. For more details, check RadFixedDocument
topic from RadPdfProcessing
documentation.
The following example demonstrates how to visualize a document by using the RadFixedDocument
method.
private void ImportFixedDocument()
{
Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider provider = new Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider();
Assembly assembly = typeof(MinMaxZoomLevel).Assembly;
string fileName = assembly.GetManifestResourceNames().FirstOrDefault(n => n.Contains("pdf-processing.pdf"));
using (Stream stream = assembly.GetManifestResourceStream(fileName))
{
RadFixedDocument document = provider.Import(stream);
this.pdfViewer.Source = document;
}
}
The example shows a PDF document visualized as an embedded resource. This is one of the options for loading a PDF with the PDF Viewer control.
Visualize Documents from URI
The following example demonstrates how to visualize a document from a URI:
Uri uri = this.GetUri();
this.pdfViewer.Source = uri;
Alternatively, you can also the UriDocumentSource
class. In this example, the GetUri()
method returns a valid and accessible URL:
Uri uri = this.GetUri();
this.pdfViewer.Source = new UriDocumentSource(uri);
Visualize Documents from File
You can visualize the PDF document from a file located on a device by passing the file path to the Source
property of the PDF Viewer control:
this.pdfViewer.Source = filePath;
In the example above, the filePath
variable is a string that contains the path to the file location.
To validate that the file exists on the device, you can use the following code:
System.IO.File.OpenRead(filePath);
Your app must have all required permissions to use the file resources. Failing to grant these permissions will result in an error.
Visualize Documents from Byte Array
The following example demonstrates how to visualize documents from a byte array.
byte[] bytes = this.GetBytes();
this.pdfViewer.Source = bytes;
Alternatively, you can use the ByteArrayDocumentSource
class:
byte[] bytes = this.GetBytes();
this.pdfViewer.Source = new ByteArrayDocumentSource(bytes, true);
Visualize Documents from a Stream
The following example demonstrates how to visualize documents from a stream:
Func<CancellationToken, Task<Stream>> streamFunc = ct => Task.Run(() =>
{
Assembly assembly = typeof(MinMaxZoomLevel).Assembly;
string fileName = assembly.GetManifestResourceNames().FirstOrDefault(n => n.Contains("pdf-processing.pdf"));
Stream stream = assembly.GetManifestResourceStream(fileName);
return stream;
});
this.pdfViewer.Source = streamFunc;
Alternatively, you can use the StreamDocumentSource
class:
Assembly assembly = typeof(KeyFeatures).Assembly;
string fileName = assembly.GetManifestResourceNames().FirstOrDefault(n => n.Contains("pdfviewer-overview.pdf"));
Stream stream = assembly.GetManifestResourceStream(fileName);
var streamDocumentSource = new StreamDocumentSource();
streamDocumentSource.Import(stream);
this.pdfViewer.Source = streamDocumentSource;
When using
StreamDocumentSource
, the stream must stay open while the PDF Viewer is in use because the PDF import operates through read on demand. As a result, you must manually close the stream when the PDF Viewer is no longer used.
Read-On-Demand Loading
The PDF Viewer control implements read-on-demand loading, and each page of the document loads dynamically only when it is shown in the PDF Viewer. When that page isn't in the view area, it gets unloaded. The stream that holds the document stays opened while the document is used in PDF Viewer.
Get Document Reference
By using the Document
property of RadPdfViewer
, you can get a reference to the RadFixedDocument
imported by the document's source. For more details, check RadFixedDocument
topic in the documentation of the RadPdfProcessing library.