Imports System.Threading.Tasks
Imports EgtWPFLib5
Imports Windows.Storage
Imports System.IO
Imports Windows.Data.Pdf
Imports Windows.Storage.Streams
Public Class PDFViewerVM
Inherits VMBase
#Region "FIELDS & PROPERTIES"
Private m_sPdfPath As String
Public Property PdfPath As String
Get
Return m_sPdfPath
End Get
Set(value As String)
m_sPdfPath = value
If Not String.IsNullOrEmpty(m_sPdfPath) Then
Dim path = System.IO.Path.GetFullPath(m_sPdfPath)
StorageFile.GetFileFromPathAsync(path).AsTask().ContinueWith(Function(t) PdfDocument.LoadFromFileAsync(t.Result).AsTask()).Unwrap().ContinueWith(Function(t2) PdfToImages(t2.Result), TaskScheduler.FromCurrentSynchronizationContext())
End If
NotifyPropertyChanged(NameOf(PdfPath))
End Set
End Property
Private Shared m_Items As List(Of Image)
Public Shared ReadOnly Property Items As List(Of Image)
Get
Return m_Items
End Get
End Property
#End Region ' Field & Properties
Sub New()
End Sub
#Region "METHODS"
'''
''' Funzione che trasforma le pagine del pdf in immagine
'''
''' Singola pagina del PDf
'''
Private Shared Async Function PdfToImages(pdfDoc As PdfDocument) As Task
'Dim items As ItemCollection = pdfViewer.PagesContainer.Items
Items.Clear()
If IsNothing(pdfDoc) Then Return
For i As Integer = 0 To pdfDoc.PageCount - 1
Using page As PdfPage = pdfDoc.GetPage(i)
Dim bitmap As BitmapImage = Await PageToBitmapAsync(page)
Dim image As New Image With {
.Source = bitmap,
.HorizontalAlignment = HorizontalAlignment.Center,
.Width = bitmap.Width,
.Height = bitmap.Height
}
Items.Add(image)
End Using
Next
End Function
'''
''' Funzione che trasforma la singola pagina del PDF in bitmap
'''
''' Singola pagina del PDF che verrà trasformata in bitmap
'''
Private Shared Async Function PageToBitmapAsync(page As PdfPage) As Task(Of BitmapImage)
Dim image As New BitmapImage()
Using stream = New InMemoryRandomAccessStream()
Await page.RenderToStreamAsync(stream)
image.BeginInit()
image.CacheOption = BitmapCacheOption.OnLoad
image.StreamSource = stream.AsStream()
image.EndInit()
End Using
Return image
End Function
#End Region ' Methods
End Class