Imports GrapeCity.ActiveReports

Public Class usrcntlPrint


    Public Printvorlage As String = ""
    Public dt As DataTable
    Public meh As Boolean = True


    Private Sub usrcntlPrint_Load(sender As Object, e As EventArgs) Handles Me.Load

        Dim tsbPDF As New ToolStripButton("PDF")
        tsbPDF.Image = My.Resources.pdf512
        Viewer.Toolbar.MainBar.Items.Insert(5, tsbPDF)
        AddHandler tsbPDF.Click, AddressOf tsbPDF_Click

        '  Viewer.Toolbar.MainBar.Items.RemoveAt(0)

        Print()
    End Sub


    Private Sub Print()

        Select Case Printvorlage
            Case "Mittagsmenue"
                Dim report As New rptMittagsMenue
                report.dt = dt
                Viewer.LoadDocument(report)
                Dim tempreport As New rptMittagsMenue
                tempreport.dt = dt
                ' Dim tempviewer As New GrapeCity.ActiveReports.Viewer.Win.Viewer
                'tempviewer.LoadDocument(tempreport)
                AddHandler report.ReportEnd, Function()
                                                 exportPDF()
                                                 Export(Viewer.Document, "C:\Gastro\Menues\")

                                             End Function
            Case "Test"
                Dim report As New rptMittagsMenue
                report.dt = dt
                Viewer.LoadDocument(report)
        End Select

    End Sub



    Function exportPDF()
        Dim filename As String = "C:\Gastro\Menues\" & dt.Rows.Item(0).Item("MenuDateVon").ToString.Substring(0, 10) & ".pdf"
        Dim filename2 As String = cListe.PDFUploadPath & dt.Rows.Item(0).Item("MenuDateVon").ToString.Substring(0, 10) & ".pdf"
        Dim pdfe As New GrapeCity.ActiveReports.Export.Pdf.Section.PdfExport

        pdfe.Export(Viewer.Document, filename)
        pdfe.Export(Viewer.Document, filename2)


    End Function

    Function exportJPG()
        Dim filename As String = "C:\Gastro\Menues\" & dt.Rows.Item(0).Item("MenuDateVon").ToString.Substring(0, 10) & ".jpg"
        Dim jpge As New Export.Image.Tiff.Section.TiffExport


        jpge.export(Viewer.Document, filename)
    End Function


    Public Sub Export(ByVal ReportDocument As GrapeCity.ActiveReports.Document.SectionDocument, ByVal folderSavePath As String)
        ' Dim pageindex As Integer = 0

        For Each _pg As GrapeCity.ActiveReports.Document.Section.Page In ReportDocument.Pages
            _pg.Units = GrapeCity.ActiveReports.Document.Section.Units.Pixels
            Dim bmp As Bitmap = New Bitmap(CInt(_pg.Width), CInt(_pg.Height))
            Dim g As Graphics = Graphics.FromImage(bmp)
            g.FillRectangle(New SolidBrush(Color.White), 0, 0, bmp.Width, bmp.Height)
            _pg.Draw(g, New RectangleF(0, 0, _pg.Width, _pg.Height))
            bmp.Save(folderSavePath & dt.Rows.Item(0).Item("MenuDateVon").ToString.Substring(0, 10) & ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
            'pageindex += 1
            _pg.Units = GrapeCity.ActiveReports.Document.Section.Units.Inches
        Next


    End Sub




    Private Sub tsbPDF_Click(sender As Object, e As EventArgs)
        'Dim Dateiname As String
        Dim savefiledialog As New SaveFileDialog
        savefiledialog.Filter = "PDF files (*.pdf)|*.PDF"
        savefiledialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
        savefiledialog.ShowDialog()

        Dim p As New GrapeCity.ActiveReports.Export.Pdf.Section.PdfExport
        If savefiledialog.FileName <> "" Then p.Export(Viewer.Document, savefiledialog.FileName)

        If System.IO.File.Exists(savefiledialog.FileName) = True Then Process.Start(savefiledialog.FileName)
    End Sub


End Class
