-
Notifications
You must be signed in to change notification settings - Fork 4
/
PdfViewer.ascx.vb
131 lines (109 loc) · 4.5 KB
/
PdfViewer.ascx.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports DevExpress.Pdf
Imports System.IO
Imports DevExpress.Web
Imports System.Drawing
Imports System.Drawing.Imaging
Namespace E5095
Public Partial Class PdfViewer
Inherits System.Web.UI.UserControl
Private _pdfData As Byte()
Private _pdfFilePath As String
Private _documentProcessor As DevExpress.Pdf.PdfDocumentProcessor
Public Sub New()
Me._pdfData = Nothing
Me._pdfFilePath = ""
Me._documentProcessor = New DevExpress.Pdf.PdfDocumentProcessor()
End Sub
Protected ReadOnly Property DocumentProcessor As PdfDocumentProcessor
Get
Return Me._documentProcessor
End Get
End Property
Public Property Width As Unit
Get
Return Me.dvDocument.Width
End Get
Set(ByVal value As Unit)
Me.dvDocument.Width = value
End Set
End Property
Public Property Height As Unit
Get
Return Me.dvDocument.Height
End Get
Set(ByVal value As Unit)
Me.dvDocument.Height = value
End Set
End Property
Public Property PdfFilePath As String
Get
Return Me._pdfFilePath
End Get
Set(ByVal value As String)
Try
Me._pdfFilePath = value
If Not System.[String].IsNullOrEmpty(value) Then
Me.DocumentProcessor.LoadDocument(Me.Server.MapPath(value), True)
Me.BindDataView()
End If
Catch ex As System.Exception
Me.ShowError(System.[String].Format("File Loading Failed: {0}", ex.Message))
End Try
End Set
End Property
Public Property PdfData As Byte()
Get
Return Me._pdfData
End Get
Set(ByVal value As Byte())
Try
Me._pdfData = value
If value IsNot Nothing Then
Using stream As System.IO.MemoryStream = New System.IO.MemoryStream(value)
Me.DocumentProcessor.LoadDocument(stream, True)
Me.BindDataView()
End Using
End If
Catch ex As System.Exception
Me.ShowError(System.[String].Format("File Loading Failed: {0}", ex.Message))
End Try
End Set
End Property
Protected Sub BindDataView()
If Me.DocumentProcessor.Document IsNot Nothing Then
Dim data As System.Collections.Generic.List(Of E5095.PdfViewer.PdfPageItem) = New System.Collections.Generic.List(Of E5095.PdfViewer.PdfPageItem)()
For pageNumber As Integer = 1 To Me.DocumentProcessor.Document.Pages.Count
data.Add(New E5095.PdfViewer.PdfPageItem() With {.PageNumber = pageNumber})
Next
Me.dvDocument.DataSource = data
Me.dvDocument.DataBind()
End If
Me.lbErrorMessage.Text = System.[String].Empty
End Sub
Protected Sub bimPdfPage_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)
Dim image As DevExpress.Web.ASPxBinaryImage = TryCast(sender, DevExpress.Web.ASPxBinaryImage)
Dim container As DevExpress.Web.DataViewItemTemplateContainer = TryCast(image.NamingContainer, DevExpress.Web.DataViewItemTemplateContainer)
Dim pageNumber As Integer = CInt(container.EvalDataItem("PageNumber"))
Using bitmap As System.Drawing.Bitmap = Me.DocumentProcessor.CreateBitmap(pageNumber, 900)
Using stream As System.IO.MemoryStream = New System.IO.MemoryStream()
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png)
image.ContentBytes = stream.ToArray()
End Using
End Using
End Sub
Protected Class PdfPageItem
Public Property PageNumber As Integer
End Class
Protected Sub ShowError(ByVal message As String)
Me.dvDocument.DataSource = Nothing
Me.dvDocument.DataBind()
Me.lbErrorMessage.Text = message
End Sub
End Class
End Namespace