Imports System.Collections
Imports System.ComponentModel
Imports System.IO
Imports System.Reflection
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports C1.C1Zip
Imports C1.Silverlight.FlexGrid
Imports C1.Silverlight.Pdf

Namespace MultiGridPdf
	Public Class PdfExportOptions
		Public Sub New()
			Margin = New Thickness(96)
			ScaleMode = ScaleMode.PageWidth
			DocumentTitle = "ComponentOne FlexGrid"
		End Sub
		Private privateMargin As Thickness
		Public Property Margin() As Thickness
			Get
				Return privateMargin
			End Get
			Set(ByVal value As Thickness)
				privateMargin = value
			End Set
		End Property
		Private privateScaleMode As ScaleMode
		Public Property ScaleMode() As ScaleMode
			Get
				Return privateScaleMode
			End Get
			Set(ByVal value As ScaleMode)
				privateScaleMode = value
			End Set
		End Property
		Private privateDocumentTitle As String
		Public Property DocumentTitle() As String
			Get
				Return privateDocumentTitle
			End Get
			Set(ByVal value As String)
				privateDocumentTitle = value
			End Set
		End Property
		Private privateKnownPageCount As Boolean
		Public Property KnownPageCount() As Boolean
			Get
				Return privateKnownPageCount
			End Get
			Set(ByVal value As Boolean)
				privateKnownPageCount = value
			End Set
		End Property
	End Class

	Friend NotInheritable Class GridExport
		'-------------------------------------------------------------------------------------------------------
		#Region "** object model"

		Private Sub New()
		End Sub
		Public Shared Sub SavePdf(ByVal flex As C1FlexGrid, ByVal s As Stream)
			Dim options = New PdfExportOptions()
			SavePdf(flex, s, options)
		End Sub
		Public Shared Sub SavePdf(ByVal flex As C1FlexGrid, ByVal s As Stream, ByVal options As PdfExportOptions)
			Dim pdf = New C1PdfDocument()
			options.KnownPageCount = False
			RenderGrid(pdf, flex, options)

			' save the PDF document and close the stream
			pdf.Save(s)
			s.Close()
		End Sub

		Public Shared Sub RenderGrid(ByVal pdf As C1PdfDocument, ByVal flex As C1FlexGrid)
			RenderGrid(pdf, flex, Nothing)
		End Sub
		Public Shared Sub RenderGrid(ByVal pdf As C1PdfDocument, ByVal flex As C1FlexGrid, ByVal options As PdfExportOptions)
			' get rendering options
			If options Is Nothing Then
				options = New PdfExportOptions()
			End If

			' get root element to lay out the PDF pages
			Dim root As Panel = Nothing
			Dim parent = TryCast(flex.Parent, FrameworkElement)
			Do While parent IsNot Nothing
				If TypeOf parent Is Panel Then
					root = TryCast(parent, Panel)
				End If
				parent = TryCast(parent.Parent, FrameworkElement)
			Loop

			' get page size
			Dim rc = pdf.PageRectangle

			' create panel to hold elements while they render
			Dim pageTemplate_Renamed = New PageTemplate()
			pageTemplate_Renamed.Width = rc.Width
			pageTemplate_Renamed.Height = rc.Height
			pageTemplate_Renamed.SetPageMargin(options.Margin)
			root.Children.Add(pageTemplate_Renamed)

			' render grid into PDF document
			Dim m = options.Margin
			Dim sz = New Size(rc.Width - m.Left - m.Right, rc.Height - m.Top - m.Bottom)
			Dim pages = flex.GetPageImages(options.ScaleMode, sz, 100)
			For i As Integer = 0 To pages.Count - 1
				' skip a page when necessary
				If i > 0 Then
					pdf.NewPage()
				End If

				' set content
				pageTemplate_Renamed.PageContent.Child = pages(i)
                pageTemplate_Renamed.PageContent.Stretch = If(options.ScaleMode = ScaleMode.ActualSize, System.Windows.Media.Stretch.None, System.Windows.Media.Stretch.Uniform)

				' set header/footer text
				pageTemplate_Renamed.HeaderLeft.Text = options.DocumentTitle
				If options.KnownPageCount Then
					pageTemplate_Renamed.FooterRight.Text = String.Format("Page {0} of {1}", pdf.CurrentPage + 1, pages.Count)
				Else
					pageTemplate_Renamed.FooterRight.Text = String.Format("Page {0}", pdf.CurrentPage + 1)
				End If

				' measure page element
				pageTemplate_Renamed.UpdateLayout()
				pageTemplate_Renamed.Arrange(New Rect(0, 0, rc.Width, rc.Height))

				' add to PDF
				pdf.DrawElement(pageTemplate_Renamed, rc)
			Next i

			' done with template
			root.Children.Remove(pageTemplate_Renamed)
		End Sub

		#End Region
	End Class
End Namespace
