Imports MigraDoc.DocumentObjectModel
Imports MigraDoc.DocumentObjectModel.Shapes.Charts
Imports MigraDoc.DocumentObjectModel.Tables
Imports EgtBEAMWALL.Core
Imports EgtBEAMWALL.Core.ConstGen
Imports EgtUILib
Imports System.Collections.ObjectModel
Imports PdfSharp.Drawing
Imports MigraDoc.DocumentObjectModel.Shapes

Module PDFHelper

   Friend Enum ListTypes As Integer
      PART = 1
      MACHGROUP = 2
      RAWPART = 3
   End Enum

   Private m_TestText As String = "Testo di prova per testare MigraDoc nuget package"
   Private m_TestMediumText As String = "Testo di prova MEDIO per testare MigraDoc nuget package"
   Private m_TestShortText As String = "Testo di prova CORTO"

   'Public Function CreateDocument() As Document
   '   ' creo nuovo documento MigraDoc
   '   Dim document As Document = New Document()
   '   document.Info.Title = "Hello, MigraDoc"
   '   document.Info.Subject = "Demonstrates an excerpt of the capabilities of MigraDoc."
   '   document.Info.Author = "Stefan Lange"
   '   DefineStyles(document)
   '   DefineCover(document)
   '   DefineTableOfContents(document)
   '   DefineContentSection(document)
   '   DefineParagraphs(document)
   '   DefineTables(document)
   '   DefineCharts(document)
   '   Return document
   'End Function

   Public Function CreateStatReport(ActivePage As Pages) As Document
      ' creo nuovo documento MigraDoc
      Dim document As Document = New Document()
      Dim Project As ProjectFileVM = Map.refProdManagerVM.CurrProd
      Select Case ActivePage
         Case Pages.VIEW
            Project = Map.refProjManagerVM.CurrProj
            document.Info.Title = Project.sProjId & " - " & Project.BTLFileName_Msg & " - Statistics"
         Case Pages.MACHINING
            Project = Map.refProdManagerVM.CurrProd
            document.Info.Title = Project.sProdId & " - " & Project.ListName_Msg & " - Statistics"
      End Select
      document.Info.Subject = "Production statistics of the project"
      document.Info.Author = "EgtBEAMWALL"
      DefineStyles(document)
      Select Case ActivePage
         Case Pages.VIEW
            DefinePartTotals(document)
            DefineItemList(document, ListTypes.PART)
         Case Pages.MACHINING
            DefineMachGroupTotals(document)
            DefineItemList(document, ListTypes.MACHGROUP)
            DefineItemList(document, ListTypes.RAWPART)
            If True Then
               DefineRawPartPictures(document)
            End If
      End Select
      Return document
   End Function

   Public Sub DefinePartTotals(ByVal document As Document)
      Dim section As Section = document.AddSection()
      ' cambio orientamento a orizzontale
      Dim pgSetup As PageSetup = document.DefaultPageSetup.Clone()
      pgSetup.Orientation = Orientation.Landscape
      section.PageSetup = pgSetup
      Dim paragraph As Paragraph = section.AddParagraph("Part Totals", "Heading2")
      paragraph = section.AddParagraph(Map.refStatisticsVM.BTLTotParts_Msg & Map.refProjectVM.BTLStructureVM.sBTLTotParts)
      paragraph = section.AddParagraph(Map.refStatisticsVM.BTLTotVolume_Msg & Map.refProjectVM.BTLStructureVM.sBTLTotVolume)
      paragraph = section.AddParagraph(Map.refStatisticsVM.BTLTotTime_Msg & Map.refProjectVM.BTLStructureVM.sBTLTotTime)
      paragraph = section.AddParagraph(Map.refStatisticsVM.BTLRemainingTime_Msg & Map.refProjectVM.BTLStructureVM.sBTLRemainingTime)
   End Sub

   Public Sub DefineMachGroupTotals(ByVal document As Document)
      Dim section As Section = document.AddSection()
      ' cambio orientamento a orizzontale
      Dim pgSetup As PageSetup = document.DefaultPageSetup.Clone()
      pgSetup.Orientation = Orientation.Landscape
      section.PageSetup = pgSetup
      Dim paragraph As Paragraph = section.AddParagraph("Machgroup Totals", "Heading2")
      paragraph = section.AddParagraph(Map.refStatisticsVM.TotMachGroups_Msg & Map.refProjectVM.MachGroupPanelVM.sTotMachGroups)
      paragraph = section.AddParagraph(Map.refStatisticsVM.BTLTotVolume_Msg & Map.refProjectVM.MachGroupPanelVM.sTotVolume)
      paragraph = section.AddParagraph(Map.refStatisticsVM.BTLTotEstimatedTime_Msg & Map.refProjectVM.MachGroupPanelVM.sTotEstimatedTime)
      paragraph = section.AddParagraph(Map.refStatisticsVM.BTLTotTime_Msg & Map.refProjectVM.MachGroupPanelVM.sTotTime)
      paragraph = section.AddParagraph(Map.refStatisticsVM.BTLRemainingTime_Msg & Map.refProjectVM.MachGroupPanelVM.sRemainingTime)
      paragraph = section.AddParagraph(Map.refStatisticsVM.TotCNTParts_Msg & Map.refProjectVM.BTLStructureVM.sTotCNTParts)
      paragraph = section.AddParagraph(Map.refStatisticsVM.TotADDEDParts_Msg & Map.refProjectVM.BTLStructureVM.sTotADDEDParts)
      paragraph = section.AddParagraph(Map.refStatisticsVM.TotINPRODParts_Msg & Map.refProjectVM.BTLStructureVM.sTotINPRODParts)
      paragraph = section.AddParagraph(Map.refStatisticsVM.TotDONEParts_Msg & Map.refProjectVM.BTLStructureVM.sTotDONEParts)
   End Sub

   Public Sub DefineItemList(ByVal document As Document, nListType As ListTypes)
      Dim section As Section = document.LastSection()
      Select Case nListType
         Case ListTypes.PART
            section.AddParagraph("Part List", "Heading2")
         Case ListTypes.MACHGROUP
            section.AddParagraph("MachGroup List", "Heading2")
         Case ListTypes.RAWPART
            section.AddParagraph("RawPart List", "Heading2")
      End Select
      Dim table As Table = New Table()
      table.Borders.Width = 0.75
      ' ciclo sulle colonne per creare la tabella e riempirne i campi
      ' -2 -> creo colonne
      ' -1 -> riempio colonne della riga degli header
      ' >=0 -> riempio le colonne con i valori dei pezzi
      Dim Itemlist As IEnumerable(Of Object) = Nothing
      Select Case nListType
         Case ListTypes.PART
            Itemlist = Map.refProjectVM.BTLStructureVM.BTLPartVMList
         Case ListTypes.MACHGROUP
            Itemlist = Map.refProjectVM.MachGroupPanelVM.MachGroupVMList
         Case ListTypes.RAWPART
            Itemlist = Map.refStatisticsVM.RawPartList
      End Select
      'If m_ActivePage = Pages.VIEW Then
      '   Itemlist = Map.refProjectVM.BTLStructureVM.BTLPartVMList.ToList()
      'ElseIf m_ActivePage = Pages.MACHINING Then
      '   Itemlist = Map.refProjectVM.MachGroupPanelVM.MachGroupVMList.ToList()
      'End If
      For PartIndex = -2 To Itemlist.Count - 1
         Dim BTLPartVM As BTLPartVM = Nothing
         Dim MachGroupVM As MyMachGroupVM = Nothing
         Dim RawPart As SParam = Nothing
         Dim row As Row = Nothing
         If PartIndex <> -2 Then
            ' aggiungo riga
            row = table.AddRow()
         End If
         If PartIndex = -1 Then
            ' imposto la riga come header che si ripetera' all'inizio di ogni pagina
            row.HeadingFormat = True
            ' coloro sfondo riga degli header
            row.Shading.Color = Colors.Aqua
         ElseIf PartIndex >= 0 Then
            ' recupero elemento della riga
            Select Case nListType
               Case ListTypes.PART
                  BTLPartVM = Map.refProjectVM.BTLStructureVM.BTLPartVMList(PartIndex)
               Case ListTypes.MACHGROUP
                  MachGroupVM = Map.refProjectVM.MachGroupPanelVM.MachGroupVMList(PartIndex)
               Case ListTypes.RAWPART
                  RawPart = Map.refStatisticsVM.RawPartList(PartIndex)
            End Select
         End If
         Dim Columnlist As ObservableCollection(Of EgtDataGridColumn) = Nothing
         Select Case nListType
            Case ListTypes.PART
               Columnlist = Map.refStatisticsVM.StatisticsColumns
            Case ListTypes.MACHGROUP
               Columnlist = Map.refStatisticsVM.OptimizerStatisticsColumns
            Case ListTypes.RAWPART
               Columnlist = Map.refStatisticsVM.RawPartStatisticsColumns
         End Select
         For ColumnIndex = 0 To Columnlist.Count - 1
            Select Case Columnlist(ColumnIndex).Name
               Case "colPDN"
                  If PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(1.5))
                  ElseIf PartIndex = -1 Then
                     row.Cells(ColumnIndex).AddParagraph(Map.refStatisticsVM.PDN_Msg)
                  ElseIf nListType = ListTypes.PART Then
                     row.Cells(ColumnIndex).AddParagraph(BTLPartVM.nPDN)
                  End If
               Case "colID"
                  If PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(1.5))
                  ElseIf PartIndex = -1 Then
                     row.Cells(ColumnIndex).AddParagraph(Map.refStatisticsVM.ID_Msg)
                  ElseIf nListType = ListTypes.MACHGROUP Then
                     row.Cells(ColumnIndex).AddParagraph(MachGroupVM.Id)
                  End If
               Case "colW"
                  If PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(2))
                  ElseIf PartIndex = -1 Then
                     row.Cells(ColumnIndex).AddParagraph(Map.refStatisticsVM.W_Msg)
                  ElseIf nListType = ListTypes.PART Then
                     row.Cells(ColumnIndex).AddParagraph(BTLPartVM.sW)
                  ElseIf nListType = ListTypes.MACHGROUP Then
                     row.Cells(ColumnIndex).AddParagraph(MachGroupVM.dW)
                  ElseIf nListType = ListTypes.RAWPART Then
                     row.Cells(ColumnIndex).AddParagraph(RawPart.sW)
                  End If
               Case "colH"
                  If PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(2))
                  ElseIf PartIndex = -1 Then
                     row.Cells(ColumnIndex).AddParagraph(Map.refStatisticsVM.H_Msg)
                  ElseIf nListType = ListTypes.PART Then
                     row.Cells(ColumnIndex).AddParagraph(BTLPartVM.sH)
                  ElseIf nListType = ListTypes.MACHGROUP Then
                     row.Cells(ColumnIndex).AddParagraph(MachGroupVM.dH)
                  ElseIf nListType = ListTypes.RAWPART Then
                     row.Cells(ColumnIndex).AddParagraph(RawPart.sH)
                  End If
               Case "colL"
                  If PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(2))
                  ElseIf PartIndex = -1 Then
                     row.Cells(ColumnIndex).AddParagraph(Map.refStatisticsVM.L_Msg)
                  ElseIf nListType = ListTypes.PART Then
                     row.Cells(ColumnIndex).AddParagraph(BTLPartVM.sL)
                  ElseIf nListType = ListTypes.MACHGROUP Then
                     row.Cells(ColumnIndex).AddParagraph(MachGroupVM.dL)
                  ElseIf nListType = ListTypes.RAWPART Then
                     row.Cells(ColumnIndex).AddParagraph(RawPart.sL)
                  End If
               Case "colDESC"
                  If PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(4))
                  ElseIf PartIndex = -1 Then
                     row.Cells(ColumnIndex).AddParagraph(Map.refStatisticsVM.NAM_Msg)
                  ElseIf nListType = ListTypes.PART Then
                     row.Cells(ColumnIndex).AddParagraph(BTLPartVM.sNAM)
                  ElseIf nListType = ListTypes.MACHGROUP Then
                     row.Cells(ColumnIndex).AddParagraph(MachGroupVM.nName)
                  End If
               Case "colNAME"
                  If PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(4))
                  ElseIf PartIndex = -1 Then
                     row.Cells(ColumnIndex).AddParagraph(Map.refStatisticsVM.NAM_Msg)
                  ElseIf nListType = ListTypes.PART Then
                     row.Cells(ColumnIndex).AddParagraph(BTLPartVM.sNAM)
                  ElseIf nListType = ListTypes.MACHGROUP Then
                     row.Cells(ColumnIndex).AddParagraph(MachGroupVM.nName)
                  End If
               Case "colMATERIAL"
                  If PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(2))
                  ElseIf PartIndex = -1 Then
                     row.Cells(ColumnIndex).AddParagraph(Map.refStatisticsVM.MAT_Msg)
                  ElseIf nListType = ListTypes.PART Then
                     row.Cells(ColumnIndex).AddParagraph(BTLPartVM.sMATERIAL)
                  ElseIf nListType = ListTypes.MACHGROUP Then
                     row.Cells(ColumnIndex).AddParagraph(MachGroupVM.sMATERIAL)
                  ElseIf nListType = ListTypes.RAWPART Then
                     row.Cells(ColumnIndex).AddParagraph(RawPart.sMaterial)
                  End If
               Case "colCNT"
                  If PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(1))
                  ElseIf PartIndex = -1 Then
                     row.Cells(ColumnIndex).AddParagraph(Map.refStatisticsVM.CNT_Msg)
                  ElseIf nListType = ListTypes.PART Then
                     row.Cells(ColumnIndex).AddParagraph(BTLPartVM.sCNT)
                  End If
               Case "colADDED"
                  If PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(1))
                  ElseIf PartIndex = -1 Then
                     row.Cells(ColumnIndex).AddParagraph(Map.refStatisticsVM.ADDED_Msg)
                  ElseIf nListType = ListTypes.PART Then
                     row.Cells(ColumnIndex).AddParagraph(BTLPartVM.sADDED)
                  End If
               Case "colINPROD"
                  If PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(1))
                  ElseIf PartIndex = -1 Then
                     row.Cells(ColumnIndex).AddParagraph(Map.refStatisticsVM.INPROD_Msg)
                  ElseIf nListType = ListTypes.PART Then
                     row.Cells(ColumnIndex).AddParagraph(BTLPartVM.nINPROD)
                  End If
               Case "colDONE"
                  If PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(1))
                  ElseIf PartIndex = -1 Then
                     row.Cells(ColumnIndex).AddParagraph(Map.refStatisticsVM.DONE_Msg)
                  ElseIf nListType = ListTypes.PART Then
                     row.Cells(ColumnIndex).AddParagraph(BTLPartVM.nDONE)
                  End If
               Case "colUSAGE"
                  If PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(2))
                  ElseIf PartIndex = -1 Then
                     row.Cells(ColumnIndex).AddParagraph(Map.refStatisticsVM.Used_Msg)
                  ElseIf nListType = ListTypes.MACHGROUP Then
                     row.Cells(ColumnIndex).AddParagraph(MachGroupVM.sUsage)
                  End If
               Case "colWASTE"
                  If PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(2))
                  ElseIf PartIndex = -1 Then
                     row.Cells(ColumnIndex).AddParagraph(Map.refStatisticsVM.Waste_Msg)
                  ElseIf nListType = ListTypes.MACHGROUP Then
                     row.Cells(ColumnIndex).AddParagraph(MachGroupVM.sWaste)
                  End If
               Case "colUNITVOLUME"
                  If PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(2))
                  ElseIf PartIndex = -1 Then
                     row.Cells(ColumnIndex).AddParagraph(Map.refStatisticsVM.UNIT_VOLUME_Msg)
                  ElseIf nListType = ListTypes.PART Then
                     row.Cells(ColumnIndex).AddParagraph(BTLPartVM.sUnitVolume)
                  ElseIf nListType = ListTypes.MACHGROUP Then
                     row.Cells(ColumnIndex).AddParagraph(MachGroupVM.dUnitVolume)
                  End If
               Case "colTOTVOLUME"
                  If PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(2))
                  ElseIf PartIndex = -1 Then
                     row.Cells(ColumnIndex).AddParagraph(Map.refStatisticsVM.TOT_VOLUME_Msg)
                  ElseIf nListType = ListTypes.PART Then
                     row.Cells(ColumnIndex).AddParagraph(BTLPartVM.sTotVolume)
                  End If
               Case "colUNITTIME"
                  If PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(2))
                  ElseIf PartIndex = -1 Then
                     row.Cells(ColumnIndex).AddParagraph(Map.refStatisticsVM.UNIT_TIME_Msg)
                  ElseIf nListType = ListTypes.PART Then
                     row.Cells(ColumnIndex).AddParagraph(BTLPartVM.sUnitTime)
                  ElseIf nListType = ListTypes.MACHGROUP Then
                     row.Cells(ColumnIndex).AddParagraph(MachGroupVM.sUnitTime)
                  End If
               Case "colTOTTIME"
                  If PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(2))
                  ElseIf PartIndex = -1 Then
                     row.Cells(ColumnIndex).AddParagraph(Map.refStatisticsVM.TOT_TIME_Msg)
                  ElseIf nListType = ListTypes.PART Then
                     row.Cells(ColumnIndex).AddParagraph(BTLPartVM.sTotTime)
                  End If
               Case "colQTY"
                  If PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(2))
                  ElseIf PartIndex = -1 Then
                     row.Cells(ColumnIndex).AddParagraph(Map.refStatisticsVM.QTY_Msg)
                  ElseIf nListType = ListTypes.RAWPART Then
                     row.Cells(ColumnIndex).AddParagraph(RawPart.nQuantity)
                  End If
            End Select
         Next
      Next
      ' aggiungo tabella all'ultima sezione
      document.LastSection.Add(table)
   End Sub

   Public Sub DefineRawPartPictures(ByVal document As Document)
      Dim nOrigMachGroupId As Integer = EgtGetCurrMachGroup()
      For Each RawPart As MyMachGroupVM In Map.refProjectVM.MachGroupPanelVM.MachGroupVMList
         Dim section As Section = document.AddSection()
         ' cambio orientamento a verticale
         Dim pgSetup As PageSetup = document.DefaultPageSetup.Clone()
         pgSetup.Orientation = Orientation.Landscape
         section.PageSetup = pgSetup
         ' creo immagine
         EgtSetCurrMachGroup(RawPart.Id)
         'If Map.refProjectVM.BTLStructureVM.nPROJTYPE = BWType.BEAM Then
         '   EgtSetView(VT.FRONT, False)
         'Else
         '   EgtSetView(VT.TOP, False)
         'End If
         Core.ViewPanelVM.BWSetView(If(Core.ViewPanelVM.Type = BWType.BEAM, VT.FRONT, VT.TOP), False)
         EgtZoom(ZM.ALL, False)
         Dim sImagePath As String = Map.refMainWindowVM.MainWindowM.sTempDir & "/Image" & RawPart.Id & ".png"
         EgtGetImage(SM.HIDDENLINE, New Color3d(255, 255, 255), New Color3d(255, 255, 255), 3000, 1000, sImagePath)
         ' aggiungo immagine
         section.AddParagraph(RawPart.Name)
         Dim Image As Image = section.AddImage(sImagePath)
         Image.LockAspectRatio = True
         Image.Width = Unit.FromMillimeter(275)
         Image.RelativeHorizontal = RelativeHorizontal.Page
         Image.RelativeVertical = RelativeHorizontal.Page
         Image.Left = 30
         ' aggiungo tabella dei pezzi
         Dim table As Table = New Table()
         table.Borders.Width = 0.75
         For PartIndex = -2 To RawPart.PartVMList.Count - 1
            Dim PartVM As PartVM = Nothing
            Dim row As Row = Nothing
            If PartIndex <> -2 Then
               ' aggiungo riga
               row = table.AddRow()
            End If
            If PartIndex = -1 Then
               ' imposto la riga come header che si ripetera' all'inizio di ogni pagina
               row.HeadingFormat = True
               ' coloro sfondo riga degli header
               row.Shading.Color = Colors.Aqua
            ElseIf PartIndex >= 0 Then
               ' recupero elemento della riga
               PartVM = RawPart.PartVMList(PartIndex)
            End If
            Dim Columnlist As ObservableCollection(Of EgtDataGridColumn) = Nothing
            Columnlist = Map.refStatisticsVM.StatisticsColumns
            For ColumnIndex = 0 To Columnlist.Count - 1
               Select Case Columnlist(ColumnIndex).Name
                  Case "colPDN"
                     If PartIndex = -2 Then
                        table.AddColumn(Unit.FromCentimeter(1.5))
                     ElseIf PartIndex = -1 Then
                        row.Cells(ColumnIndex).AddParagraph(Map.refStatisticsVM.PDN_Msg)
                     Else
                        row.Cells(ColumnIndex).AddParagraph(PartVM.nPDN)
                     End If
                  Case "colW"
                     If PartIndex = -2 Then
                        table.AddColumn(Unit.FromCentimeter(2))
                     ElseIf PartIndex = -1 Then
                        row.Cells(ColumnIndex).AddParagraph(Map.refStatisticsVM.W_Msg)
                     Else
                        row.Cells(ColumnIndex).AddParagraph(PartVM.sW)
                     End If
                  Case "colH"
                     If PartIndex = -2 Then
                        table.AddColumn(Unit.FromCentimeter(2))
                     ElseIf PartIndex = -1 Then
                        row.Cells(ColumnIndex).AddParagraph(Map.refStatisticsVM.H_Msg)
                     Else
                        row.Cells(ColumnIndex).AddParagraph(PartVM.sH)
                     End If
                  Case "colL"
                     If PartIndex = -2 Then
                        table.AddColumn(Unit.FromCentimeter(2))
                     ElseIf PartIndex = -1 Then
                        row.Cells(ColumnIndex).AddParagraph(Map.refStatisticsVM.L_Msg)
                     Else
                        row.Cells(ColumnIndex).AddParagraph(PartVM.sL)
                     End If
                  Case "colDESC"
                     If PartIndex = -2 Then
                        table.AddColumn(Unit.FromCentimeter(4))
                     ElseIf PartIndex = -1 Then
                        row.Cells(ColumnIndex).AddParagraph(Map.refStatisticsVM.NAM_Msg)
                     Else
                        row.Cells(ColumnIndex).AddParagraph(PartVM.sNAM)
                     End If
                  Case "colNAME"
                     If PartIndex = -2 Then
                        table.AddColumn(Unit.FromCentimeter(4))
                     ElseIf PartIndex = -1 Then
                        row.Cells(ColumnIndex).AddParagraph(Map.refStatisticsVM.NAM_Msg)
                     Else
                        row.Cells(ColumnIndex).AddParagraph(PartVM.sNAM)
                     End If
                  Case "colMATERIAL"
                     If PartIndex = -2 Then
                        table.AddColumn(Unit.FromCentimeter(2))
                     ElseIf PartIndex = -1 Then
                        row.Cells(ColumnIndex).AddParagraph(Map.refStatisticsVM.MAT_Msg)
                     Else
                        row.Cells(ColumnIndex).AddParagraph(PartVM.sMATERIAL)
                     End If
               End Select
            Next
         Next
         ' aggiungo tabella all'ultima sezione
         section.Add(table)

      Next

      'Dim paragraph As Paragraph = section.AddParagraph("Machgroup Totals", "Heading2")
      'paragraph = section.AddParagraph(Map.refStatisticsVM.TotMachGroups_Msg & Map.refProjectVM.MachGroupPanelVM.sTotMachGroups)
      'paragraph = section.AddParagraph(Map.refStatisticsVM.BTLTotVolume_Msg & Map.refProjectVM.MachGroupPanelVM.sTotVolume)
      'paragraph = section.AddParagraph(Map.refStatisticsVM.BTLTotEstimatedTime_Msg & Map.refProjectVM.MachGroupPanelVM.sTotEstimatedTime)
      'paragraph = section.AddParagraph(Map.refStatisticsVM.BTLTotTime_Msg & Map.refProjectVM.MachGroupPanelVM.sTotTime)
      'paragraph = section.AddParagraph(Map.refStatisticsVM.BTLRemainingTime_Msg & Map.refProjectVM.MachGroupPanelVM.sRemainingTime)
      'paragraph = section.AddParagraph(Map.refStatisticsVM.TotCNTParts_Msg & Map.refProjectVM.BTLStructureVM.sTotCNTParts)
      'paragraph = section.AddParagraph(Map.refStatisticsVM.TotADDEDParts_Msg & Map.refProjectVM.BTLStructureVM.sTotADDEDParts)
      'paragraph = section.AddParagraph(Map.refStatisticsVM.TotINPRODParts_Msg & Map.refProjectVM.BTLStructureVM.sTotINPRODParts)
      'paragraph = section.AddParagraph(Map.refStatisticsVM.TotDONEParts_Msg & Map.refProjectVM.BTLStructureVM.sTotDONEParts)
   End Sub

   Private Sub DrawImage(gfx As XGraphics, sImagePath As String)
      Dim image As XImage = XImage.FromFile(sImagePath)
      Dim x As Double = (250 - image.PixelWidth * 72 / image.HorizontalResolution) / 2
      gfx.DrawImage(image, x, 0)
   End Sub
   ''' <summary>
   ''' Defines the styles used in the document.
   ''' </summary>
   Public Sub DefineStyles(ByVal document As Document)
      ' recupera lo stile predefinito Normal.
      Dim style As Style = document.Styles("Normal")
      ' Because all styles are derived from Normal, the next line changes the
      ' font of the whole document. Or, more exactly, it changes the font of
      ' all styles and paragraphs that do not redefine the font.
      style.Font.Name = "Arial"

      ' Heading1 to Heading9 are predefined styles with an outline level. An outline level
      ' other than OutlineLevel.BodyText automatically creates the outline (or bookmarks) 
      ' in PDF.

      style = document.Styles("Heading1")
      style.Font.Name = "Tahoma"
      style.Font.Size = 14
      style.Font.Bold = True
      style.Font.Color = Colors.DarkBlue
      style.ParagraphFormat.PageBreakBefore = True
      style.ParagraphFormat.SpaceAfter = 6

      style = document.Styles("Heading2")
      style.Font.Size = 12
      style.Font.Bold = True
      style.ParagraphFormat.PageBreakBefore = False
      style.ParagraphFormat.SpaceBefore = 6
      style.ParagraphFormat.SpaceAfter = 6

      style = document.Styles("Heading3")
      style.Font.Size = 10
      style.Font.Bold = True
      style.Font.Italic = True
      style.ParagraphFormat.SpaceBefore = 6
      style.ParagraphFormat.SpaceAfter = 3

      style = document.Styles(StyleNames.Header)
      style.ParagraphFormat.AddTabStop("16cm", TabAlignment.Right)

      style = document.Styles(StyleNames.Footer)
      style.ParagraphFormat.AddTabStop("8cm", TabAlignment.Center)

      ' Create a new style called TextBox based on style Normal
      style = document.Styles.AddStyle("TextBox", "Normal")
      style.ParagraphFormat.Alignment = ParagraphAlignment.Justify
      style.ParagraphFormat.Borders.Width = 2.5
      style.ParagraphFormat.Borders.Distance = "3pt"
      style.ParagraphFormat.Shading.Color = Colors.SkyBlue

      ' Create a new style called TOC based on style Normal
      style = document.Styles.AddStyle("TOC", "Normal")
      style.ParagraphFormat.AddTabStop("16cm", TabAlignment.Right, TabLeader.Dots)
      style.ParagraphFormat.Font.Color = Colors.Blue
   End Sub


   '''' <summary>
   '''' Defines the styles used in the document.
   '''' </summary>
   'Public Sub DefineStyles(ByVal document As Document)
   '   ' recupera lo stile predefinito Normal.
   '   Dim style As Style = document.Styles("Normal")
   '   ' Because all styles are derived from Normal, the next line changes the
   '   ' font of the whole document. Or, more exactly, it changes the font of
   '   ' all styles and paragraphs that do not redefine the font.
   '   style.Font.Name = "Times New Roman"

   '   ' Heading1 to Heading9 are predefined styles with an outline level. An outline level
   '   ' other than OutlineLevel.BodyText automatically creates the outline (or bookmarks) 
   '   ' in PDF.

   '   style = document.Styles("Heading1")
   '   style.Font.Name = "Tahoma"
   '   style.Font.Size = 14
   '   style.Font.Bold = True
   '   style.Font.Color = Colors.DarkBlue
   '   style.ParagraphFormat.PageBreakBefore = True
   '   style.ParagraphFormat.SpaceAfter = 6

   '   style = document.Styles("Heading2")
   '   style.Font.Size = 12
   '   style.Font.Bold = True
   '   style.ParagraphFormat.PageBreakBefore = False
   '   style.ParagraphFormat.SpaceBefore = 6
   '   style.ParagraphFormat.SpaceAfter = 6

   '   style = document.Styles("Heading3")
   '   style.Font.Size = 10
   '   style.Font.Bold = True
   '   style.Font.Italic = True
   '   style.ParagraphFormat.SpaceBefore = 6
   '   style.ParagraphFormat.SpaceAfter = 3

   '   style = document.Styles(StyleNames.Header)
   '   style.ParagraphFormat.AddTabStop("16cm", TabAlignment.Right)

   '   style = document.Styles(StyleNames.Footer)
   '   style.ParagraphFormat.AddTabStop("8cm", TabAlignment.Center)

   '   ' Create a new style called TextBox based on style Normal
   '   style = document.Styles.AddStyle("TextBox", "Normal")
   '   style.ParagraphFormat.Alignment = ParagraphAlignment.Justify
   '   style.ParagraphFormat.Borders.Width = 2.5
   '   style.ParagraphFormat.Borders.Distance = "3pt"
   '   style.ParagraphFormat.Shading.Color = Colors.SkyBlue

   '   ' Create a new style called TOC based on style Normal
   '   style = document.Styles.AddStyle("TOC", "Normal")
   '   style.ParagraphFormat.AddTabStop("16cm", TabAlignment.Right, TabLeader.Dots)
   '   style.ParagraphFormat.Font.Color = Colors.Blue
   'End Sub


   ''' <summary>
   ''' Defines the cover page.
   ''' </summary>
   Public Sub DefineCover(ByVal document As Document)
      Dim section As Section = document.AddSection()
      Dim paragraph As Paragraph = section.AddParagraph()
      paragraph.Format.SpaceAfter = "3cm"
      Dim image As Shapes.Image = section.AddImage("../../images/Logo landscape.png")
      image.Width = "10cm"
      paragraph = section.AddParagraph("A sample document that demonstrates the" & vbLf & "capabilities of MigraDoc")
      paragraph.Format.Font.Size = 16
      paragraph.Format.Font.Color = Colors.DarkRed
      paragraph.Format.SpaceBefore = "8cm"
      paragraph.Format.SpaceAfter = "3cm"
      paragraph = section.AddParagraph("Rendering date: ")
      paragraph.AddDateField()
   End Sub


   ''' <summary>
   ''' Defines table of contents.
   ''' </summary>
   Public Sub DefineTableOfContents(ByVal document As Document)
      Dim section As Section = document.LastSection
      section.AddPageBreak()
      Dim paragraph As Paragraph = section.AddParagraph("Table of Contents")
      paragraph.Format.Font.Size = 14
      paragraph.Format.Font.Bold = True
      paragraph.Format.SpaceAfter = 24
      paragraph.Format.OutlineLevel = OutlineLevel.Level1
      paragraph = section.AddParagraph()
      paragraph.Style = "TOC"
      Dim hyperlink As Hyperlink = paragraph.AddHyperlink("Paragraphs")
      hyperlink.AddText("Paragraphs" & vbTab)
      hyperlink.AddPageRefField("Paragraphs")
      paragraph = section.AddParagraph()
      paragraph.Style = "TOC"
      hyperlink = paragraph.AddHyperlink("Tables")
      hyperlink.AddText("Tables" & vbTab)
      hyperlink.AddPageRefField("Tables")
      paragraph = section.AddParagraph()
      paragraph.Style = "TOC"
      hyperlink = paragraph.AddHyperlink("Charts")
      hyperlink.AddText("Charts" & vbTab)
      hyperlink.AddPageRefField("Charts")
   End Sub


   ''' <summary>
   ''' Defines page setup, headers, and footers.
   ''' </summary>
   Private Sub DefineContentSection(ByVal document As Document)
      Dim section As Section = document.AddSection()
      section.PageSetup.OddAndEvenPagesHeaderFooter = True
      section.PageSetup.StartingNumber = 1

      Dim header As HeaderFooter = section.Headers.Primary
      header.AddParagraph(vbTab & "Odd Page Header")

      header = section.Headers.EvenPage
      header.AddParagraph("Even Page Header")

      ' Create a paragraph with centered page number. See definition of style "Footer".
      Dim paragraph As Paragraph = New Paragraph()
      paragraph.AddTab()
      paragraph.AddPageField()
      ' Add paragraph to footer for odd pages.
      section.Footers.Primary.Add(paragraph)
      ' Add clone of paragraph to footer for odd pages. Cloning is necessary because an object must
      ' not belong to more than one other object. If you forget cloning an exception is thrown.
      section.Footers.EvenPage.Add(paragraph.Clone())
   End Sub


#Region "Paragraphs"

   Public Sub DefineParagraphs(ByVal document As Document)
      Dim paragraph As Paragraph = document.LastSection.AddParagraph("Paragraph Layout Overview", "Heading1")
      paragraph.AddBookmark("Paragraphs")
      DemonstrateParagraphsAlignment(document)
      DemonstrateIndent(document)
      DemonstrateFormattedText(document)
      DemonstrateBordersAndShading(document)
   End Sub

   Private Sub DemonstrateParagraphsAlignment(ByVal document As Document)
      document.LastSection.AddParagraph("Alignment", "Heading2")
      document.LastSection.AddParagraph("Left Aligned", "Heading3")
      Dim paragraph As Paragraph = document.LastSection.AddParagraph()
      paragraph.Format.Alignment = ParagraphAlignment.Left
      paragraph.AddText(m_TestText)
      document.LastSection.AddParagraph("Right Aligned", "Heading3")
      paragraph = document.LastSection.AddParagraph()
      paragraph.Format.Alignment = ParagraphAlignment.Right
      paragraph.AddText(m_TestText)
      document.LastSection.AddParagraph("Centered", "Heading3")
      paragraph = document.LastSection.AddParagraph()
      paragraph.Format.Alignment = ParagraphAlignment.Center
      paragraph.AddText(m_TestText)
      document.LastSection.AddParagraph("Justified", "Heading3")
      paragraph = document.LastSection.AddParagraph()
      paragraph.Format.Alignment = ParagraphAlignment.Justify
      paragraph.AddText(m_TestMediumText)
   End Sub

   Private Sub DemonstrateIndent(ByVal document As Document)
      document.LastSection.AddParagraph("Indent", "Heading2")
      document.LastSection.AddParagraph("Left Indent", "Heading3")
      Dim paragraph As Paragraph = document.LastSection.AddParagraph()
      paragraph.Format.LeftIndent = "2cm"
      paragraph.AddText(m_TestText)
      document.LastSection.AddParagraph("Right Indent", "Heading3")
      paragraph = document.LastSection.AddParagraph()
      paragraph.Format.RightIndent = "1in"
      paragraph.AddText(m_TestText)
      document.LastSection.AddParagraph("First Line Indent", "Heading3")
      paragraph = document.LastSection.AddParagraph()
      paragraph.Format.FirstLineIndent = "12mm"
      paragraph.AddText(m_TestText)
      document.LastSection.AddParagraph("First Line Negative Indent", "Heading3")
      paragraph = document.LastSection.AddParagraph()
      paragraph.Format.LeftIndent = "1.5cm"
      paragraph.Format.FirstLineIndent = "-1.5cm"
      paragraph.AddText(m_TestText)
   End Sub

   Private Sub DemonstrateFormattedText(ByVal document As Document)
      document.LastSection.AddParagraph("Formatted Text", "Heading2")
      Dim paragraph As Paragraph = document.LastSection.AddParagraph()
      paragraph.AddText("Text can be formatted ")
      paragraph.AddFormattedText("bold", TextFormat.Bold)
      paragraph.AddText(", ")
      paragraph.AddFormattedText("italic", TextFormat.Italic)
      paragraph.AddText(", or ")
      paragraph.AddFormattedText("bold & italic", TextFormat.Bold Or TextFormat.Italic)
      paragraph.AddText(".")
      paragraph.AddLineBreak()
      paragraph.AddText("You can set the ")
      Dim formattedText As FormattedText = paragraph.AddFormattedText("size ")
      formattedText.Size = 15
      paragraph.AddText("the ")
      formattedText = paragraph.AddFormattedText("color ")
      formattedText.Color = Colors.Firebrick
      paragraph.AddText("the ")
      formattedText = paragraph.AddFormattedText("font", New Font("Verdana"))
      paragraph.AddText(".")
      paragraph.AddLineBreak()
      paragraph.AddText("You can set the ")
      formattedText = paragraph.AddFormattedText("subscript")
      formattedText.Subscript = True
      paragraph.AddText(" or ")
      formattedText = paragraph.AddFormattedText("superscript")
      formattedText.Superscript = True
      paragraph.AddText(".")
   End Sub

   Private Sub DemonstrateBordersAndShading(ByVal document As Document)
      document.LastSection.AddPageBreak()
      document.LastSection.AddParagraph("Borders and Shading", "Heading2")
      document.LastSection.AddParagraph("Border around Paragraph", "Heading3")
      Dim paragraph As Paragraph = document.LastSection.AddParagraph()
      paragraph.Format.Borders.Width = 2.5
      paragraph.Format.Borders.Color = Colors.Navy
      paragraph.Format.Borders.Distance = 3
      paragraph.AddText(m_TestMediumText)
      document.LastSection.AddParagraph("Shading", "Heading3")
      paragraph = document.LastSection.AddParagraph()
      paragraph.Format.Shading.Color = Colors.LightCoral
      paragraph.AddText(m_TestText)
      document.LastSection.AddParagraph("Borders & Shading", "Heading3")
      paragraph = document.LastSection.AddParagraph()
      paragraph.Style = "TextBox"
      paragraph.AddText(m_TestMediumText)
   End Sub

#End Region    ' Paragraphs


#Region "Tables"

   Public Sub DefineTables(ByVal document As MigraDoc.DocumentObjectModel.Document)
      Dim paragraph As Paragraph = document.LastSection.AddParagraph("Table Overview", "Heading1")
      paragraph.AddBookmark("Tables")
      DemonstrateSimpleTable(document)
      DemonstrateTableAlignment(document)
      DemonstrateCellMerge(document)
   End Sub

   Public Sub DemonstrateSimpleTable(ByVal document As Document)
      document.LastSection.AddParagraph("Simple Tables", "Heading2")
      Dim table As Table = New Table()
      table.Borders.Width = 0.75
      Dim column As Column = table.AddColumn(Unit.FromCentimeter(2))
      column.Format.Alignment = ParagraphAlignment.Center
      table.AddColumn(Unit.FromCentimeter(5))
      Dim row As Row = table.AddRow()
      row.Shading.Color = Colors.PaleGoldenrod
      Dim cell As Cell = row.Cells(0)
      cell.AddParagraph("Itemus")
      cell = row.Cells(1)
      cell.AddParagraph("Descriptum")
      row = table.AddRow()
      cell = row.Cells(0)
      cell.AddParagraph("1")
      cell = row.Cells(1)
      cell.AddParagraph(m_TestShortText)
      row = table.AddRow()
      cell = row.Cells(0)
      cell.AddParagraph("2")
      cell = row.Cells(1)
      cell.AddParagraph(m_TestText)
      table.SetEdge(0, 0, 2, 3, Edge.Box, BorderStyle.Single, 1.5, Colors.Black)
      document.LastSection.Add(table)
   End Sub


   Public Sub DemonstrateTableAlignment(ByVal document As MigraDoc.DocumentObjectModel.Document)
      document.LastSection.AddParagraph("Cell Alignment", "Heading2")
      Dim table As Table = document.LastSection.AddTable()
      table.Borders.Visible = True
      table.Format.Shading.Color = Colors.LavenderBlush
      table.Shading.Color = Colors.Salmon
      table.TopPadding = 5
      table.BottomPadding = 5
      Dim column As Column = table.AddColumn()
      column.Format.Alignment = ParagraphAlignment.Left
      column = table.AddColumn()
      column.Format.Alignment = ParagraphAlignment.Center
      column = table.AddColumn()
      column.Format.Alignment = ParagraphAlignment.Right
      table.Rows.Height = 35
      Dim row As Row = table.AddRow()
      row.VerticalAlignment = VerticalAlignment.Top
      row.Cells(0).AddParagraph("Text")
      row.Cells(1).AddParagraph("Text")
      row.Cells(2).AddParagraph("Text")
      row = table.AddRow()
      row.VerticalAlignment = VerticalAlignment.Center
      row.Cells(0).AddParagraph("Text")
      row.Cells(1).AddParagraph("Text")
      row.Cells(2).AddParagraph("Text")
      row = table.AddRow()
      row.VerticalAlignment = VerticalAlignment.Bottom
      row.Cells(0).AddParagraph("Text")
      row.Cells(1).AddParagraph("Text")
      row.Cells(2).AddParagraph("Text")
   End Sub

   Public Sub DemonstrateCellMerge(ByVal document As Document)
      document.LastSection.AddParagraph("Cell Merge", "Heading2")
      Dim table As Table = document.LastSection.AddTable()
      table.Borders.Visible = True
      table.TopPadding = 5
      table.BottomPadding = 5
      Dim column As Column = table.AddColumn()
      column.Format.Alignment = ParagraphAlignment.Left
      column = table.AddColumn()
      column.Format.Alignment = ParagraphAlignment.Center
      column = table.AddColumn()
      column.Format.Alignment = ParagraphAlignment.Right
      table.Rows.Height = 35
      Dim row As Row = table.AddRow()
      row.Cells(0).AddParagraph("Merge Right")
      row.Cells(0).MergeRight = 1
      row = table.AddRow()
      row.VerticalAlignment = VerticalAlignment.Bottom
      row.Cells(0).MergeDown = 1
      row.Cells(0).VerticalAlignment = VerticalAlignment.Bottom
      row.Cells(0).AddParagraph("Merge Down")
      table.AddRow()
   End Sub

#End Region    ' Tables

   Public Sub DefineCharts(ByVal document As Document)
      Dim paragraph As Paragraph = document.LastSection.AddParagraph("Chart Overview", "Heading1")
      paragraph.AddBookmark("Charts")
      document.LastSection.AddParagraph("Sample Chart", "Heading2")
      Dim chart As Chart = New Chart()
      chart.Left = 0
      chart.Width = Unit.FromCentimeter(16)
      chart.Height = Unit.FromCentimeter(12)
      Dim series As Series = chart.SeriesCollection.AddSeries()
      series.ChartType = ChartType.Column2D
      series.Add(New Double() {1, 17, 45, 5, 3, 20, 11, 23, 8, 19})
      series.HasDataLabel = True
      series = chart.SeriesCollection.AddSeries()
      series.ChartType = ChartType.Line
      series.Add(New Double() {41, 7, 5, 45, 13, 10, 21, 13, 18, 9})
      Dim xseries As XSeries = chart.XValues.AddXSeries()
      xseries.Add("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N")
      chart.XAxis.MajorTickMark = TickMarkType.Outside
      chart.XAxis.Title.Caption = "X-Axis"
      chart.YAxis.MajorTickMark = TickMarkType.Outside
      chart.YAxis.HasMajorGridlines = True
      chart.PlotArea.LineFormat.Color = Colors.DarkGray
      chart.PlotArea.LineFormat.Width = 1
      document.LastSection.Add(chart)
   End Sub

End Module
