Files
egtbeamwall/EgtBEAMWALL.ViewerOptimizer/PdfViewer/PDFViewerVM.vb
T
2023-11-08 09:08:28 +01:00

103 lines
3.5 KiB
VB.net

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
'Public Shared ReadOnly PdfPathProperty As DependencyProperty = DependencyProperty.Register("PdfPath",
' GetType(String),
' GetType(PDFViewerVM))
'Private Shared Sub OnPdfPathChanged(d As DependencyObject, e As DependencyPropertyChangedEventArgs)
' 'Dim pdfDrawer As PdfViewer = CType(d, PdfViewer)
' If Not String.IsNullOrEmpty(PdfPath) Then
' Dim path = System.IO.Path.GetFullPath(PdfPath)
' StorageFile.GetFileFromPathAsync(path).AsTask().ContinueWith(Function(t) PdfDocument.LoadFromFileAsync(t.Result).AsTask()).Unwrap().ContinueWith(Function(t2) PdfToImages(pdfDrawer, t2.Result), TaskScheduler.FromCurrentSynchronizationContext())
' End If
'End Sub
#End Region ' Field & Properties
Sub New()
End Sub
#Region "METHODS"
''' <summary>
''' Funzione che trasforma le pagine del pdf in immagine
''' </summary>
''' <param name="pdfViewer">PDF da trasformare</param>
''' <param name="pdfDoc">Singola pagina del PDf</param>
''' <returns></returns>
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
''' <summary>
''' Funzione che trasforma la singola pagina del PDF in bitmap
''' </summary>
''' <param name="page">Singola pagina del PDF che verrà trasformata in bitmap</param>
''' <returns></returns>
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