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 MigraDoc.DocumentObjectModel.Shapes
Imports System.IO

Module PDFHelper

   Friend Enum ListTypes As Integer
      PART = 1
      MACHGROUP = 2
      RAWPART = 3
      MACHGROUPTOT = 4
      PART_LIST = 5
      PART_TOT = 6
   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 CreateStatReport(ActivePage As Pages, bPreview As Boolean) As Document
      ' creo nuovo documento MigraDoc
      Dim document As New Document()
      Dim Project As ProjectFileVM

      If ActivePage = Pages.VIEW OrElse (ActivePage = Pages.ONLYPRODPAGE AndAlso Map.refInstrumentPanelVM.bVisOtt_Selected = False) Then
         Project = ProjectManagerVM.CurrProj
         document.Info.Title = Project.sProjId & " - " & Project.BTLFileName_Msg & " - Statistics"
      ElseIf ActivePage = Pages.MACHINING OrElse (ActivePage = Pages.ONLYPRODPAGE AndAlso Map.refInstrumentPanelVM.bVisOtt_Selected) Then
         If ActivePage = Pages.CONFIG Then
            Project = ProjectManagerVM.CurrProj
            document.Info.Title = Project.sProjId & " - " & Project.BTLFileName_Msg & " - Statistics"
         ElseIf ActivePage = Pages.ONLYPRODPAGE Then
            Project = ProjectManagerVM.CurrProd
            document.Info.Title = Project.sProdId & " - " & Project.ListName_Msg & " - Statistics"
         End If
      End If

      document.Info.Subject = "Production statistics of the project"
      document.Info.Author = "EgtBEAMWALL"
      DefineStyles(document)
      'Logo
      Dim section As Section = document.AddSection()
      document.DefaultPageSetup.DifferentFirstPageHeaderFooter = True
      Dim sImageLogo As String = Map.refMainWindowVM.MainWindowM.sTempDir & "/Image" & "Logo landscape" & ".png"
      DefineLogo(section, sImageLogo)

      If ActivePage = Pages.VIEW OrElse (ActivePage = Pages.ONLYPRODPAGE AndAlso Map.refInstrumentPanelVM.bVisOtt_Selected = False) Then
         DefinePartTotals(document, section, bPreview)
         DefineItemList(document, ListTypes.PART, bPreview)
      ElseIf ActivePage = Pages.MACHINING OrElse (ActivePage = Pages.ONLYPRODPAGE AndAlso Map.refInstrumentPanelVM.bVisOtt_Selected) Then
         DefineMachGroupTotals(document, section, bPreview)
         DefineItemList(document, ListTypes.MACHGROUP, bPreview)
         DefineItemList(document, ListTypes.RAWPART, bPreview)
         If True Then
            CreateRawPartPages(document, bPreview)
         End If
      End If

      Return document
   End Function

   Public Sub DefinePartTotals(document As Document, section As Section, bPreview As Boolean)
      Dim pgSetup As PageSetup = document.DefaultPageSetup.Clone()
      pgSetup.Orientation = Orientation.Landscape
      section.PageSetup = pgSetup
      Dim paragraph As Paragraph = section.AddParagraph(EgtMsg(61734) & "s", "Heading2")
      paragraph.Format.SpaceBefore = "3.5cm"
      Dim sPartTot As ExpanderPDF = Map.refPDFEditorVM.ExpanderList.FirstOrDefault(Function(x) x.sNameCategory = EgtMsg(61734))                                '  Recupero lista Part Totals
      Dim sBTLTotParts As ExpanderElement = sPartTot.ExpanderElementList.FirstOrDefault(Function(x As ExpanderElement) x.sNameParameter = EgtMsg(61716))       '  Recupero Numero totale pezzi con relativo checkbox 
      Dim sBTLTotVolume As ExpanderElement = sPartTot.ExpanderElementList.FirstOrDefault(Function(x As ExpanderElement) x.sNameParameter = EgtMsg(61717))      '  Recupero Volume totale con relativo checkbox 
      Dim sBTLTotTime As ExpanderElement = sPartTot.ExpanderElementList.FirstOrDefault(Function(x As ExpanderElement) x.sNameParameter = EgtMsg(61718))        '  Recupero Tempo totale con relativo checkbox 
      Dim sBTLRemainingTime As ExpanderElement = sPartTot.ExpanderElementList.FirstOrDefault(Function(x As ExpanderElement) x.sNameParameter = EgtMsg(61720))  '  Recupero Tempo rimanente con relativo checkbox 
      If sBTLTotParts.bParameter_IsChecked Then paragraph = section.AddParagraph(Statistic_Messages.BTLTotParts_Msg & If(Not bPreview, Map.refProjectVM.BTLStructureVM.sBTLTotParts, "127"))
      If sBTLTotVolume.bParameter_IsChecked Then paragraph = section.AddParagraph(Statistic_Messages.BTLTotVolume_Msg & If(Not bPreview, Map.refProjectVM.BTLStructureVM.sBTLTotVolume, "9.156"))
      If sBTLTotTime.bParameter_IsChecked Then paragraph = section.AddParagraph(Statistic_Messages.BTLTotTime_Msg & If(Not bPreview, Map.refProjectVM.BTLStructureVM.sBTLTotTime, "00:01:55"))
      If sBTLRemainingTime.bParameter_IsChecked Then paragraph = section.AddParagraph(Statistic_Messages.BTLRemainingTime_Msg & If(Not bPreview, Map.refProjectVM.BTLStructureVM.sBTLRemainingTime, "00:01:55"))
   End Sub

   Public Sub DefineMachGroupTotals(ByVal document As Document, section As Section, bPreview As Boolean)
      ' cambio orientamento a orizzontale
      Dim pgSetup As PageSetup = document.DefaultPageSetup.Clone()
      pgSetup.Orientation = Orientation.Landscape
      section.PageSetup = pgSetup
      Dim paragraph As Paragraph = section.AddParagraph(EgtMsg(61729), "Heading2")
      paragraph.Format.SpaceBefore = "3.5cm"
      Dim sMachGroup As ExpanderPDF = Map.refPDFEditorVM.ExpanderList.FirstOrDefault(Function(x) x.sNameCategory = EgtMsg(61729))                                          '  Recupero lista Machgroup Totals
      Dim sNumberBlanks As ExpanderElement = sMachGroup.ExpanderElementList.FirstOrDefault(Function(x As ExpanderElement) x.sNameParameter = EgtMsg(61721))                '  Recupero Numero di grezzi con relativo checkbox 
      Dim sTotalVolume As ExpanderElement = sMachGroup.ExpanderElementList.FirstOrDefault(Function(x As ExpanderElement) x.sNameParameter = EgtMsg(61717))                 '  Recupero Volume totale con relativo checkbox 
      Dim sEstimatedTotalTime As ExpanderElement = sMachGroup.ExpanderElementList.FirstOrDefault(Function(x As ExpanderElement) x.sNameParameter = EgtMsg(61719))          '  Recupero Tempo totale stimato con relativo checkbox 
      Dim sTotalTime As ExpanderElement = sMachGroup.ExpanderElementList.FirstOrDefault(Function(x As ExpanderElement) x.sNameParameter = EgtMsg(61718))                   '  Recupero Tempo totale con relativo checkbox
      Dim sRemainingTime As ExpanderElement = sMachGroup.ExpanderElementList.FirstOrDefault(Function(x As ExpanderElement) x.sNameParameter = EgtMsg(61720))               '  Recupero Tempo rimanente con relativo checkbox
      Dim sNumberPiecesBLT As ExpanderElement = sMachGroup.ExpanderElementList.FirstOrDefault(Function(x As ExpanderElement) x.sNameParameter = EgtMsg(61722))             '  Recupero Numero di pezzi da BTL con relativo checkbox
      Dim sNumberPiecesAdded As ExpanderElement = sMachGroup.ExpanderElementList.FirstOrDefault(Function(x As ExpanderElement) x.sNameParameter = EgtMsg(61723))           '  Recupero Numero di pezzi aggiunti con relativo checkbox
      Dim sNumberPiecesInsertedBlanks As ExpanderElement = sMachGroup.ExpanderElementList.FirstOrDefault(Function(x As ExpanderElement) x.sNameParameter = EgtMsg(61724))  '  Recupero Numero di pezzi inseriti nei grezzi con relativo checkbox
      Dim sNumberPiecesCompleted As ExpanderElement = sMachGroup.ExpanderElementList.FirstOrDefault(Function(x As ExpanderElement) x.sNameParameter = EgtMsg(61725))       '  Recupero Numero di pezzi completati con relativo checkbox
      If sNumberBlanks.bParameter_IsChecked Then paragraph = section.AddParagraph(Statistic_Messages.TotMachGroups_Msg & If(Not bPreview, Map.refProjectVM.MachGroupPanelVM.sTotMachGroups, "11"))
      If sTotalVolume.bParameter_IsChecked Then paragraph = section.AddParagraph(Statistic_Messages.BTLTotVolume_Msg & If(Not bPreview, Map.refProjectVM.MachGroupPanelVM.sTotVolume, "3.168"))
      If sEstimatedTotalTime.bParameter_IsChecked Then paragraph = section.AddParagraph(Statistic_Messages.BTLTotEstimatedTime_Msg & If(Not bPreview, Map.refProjectVM.MachGroupPanelVM.sTotEstimatedTime, "00:22:30"))
      If sTotalTime.bParameter_IsChecked Then paragraph = section.AddParagraph(Statistic_Messages.BTLTotTime_Msg & If(Not bPreview, Map.refProjectVM.MachGroupPanelVM.sTotTime, "00:09:23"))
      If sRemainingTime.bParameter_IsChecked Then paragraph = section.AddParagraph(Statistic_Messages.BTLRemainingTime_Msg & If(Not bPreview, Map.refProjectVM.MachGroupPanelVM.sRemainingTime, "00:09:23"))
      If sNumberPiecesBLT.bParameter_IsChecked Then paragraph = section.AddParagraph(Statistic_Messages.TotCNTParts_Msg & If(Not bPreview, Map.refProjectVM.BTLStructureVM.sTotCNTParts, "127"))
      If sNumberPiecesAdded.bParameter_IsChecked Then paragraph = section.AddParagraph(Statistic_Messages.TotADDEDParts_Msg & If(Not bPreview, Map.refProjectVM.BTLStructureVM.sTotADDEDParts, "0"))
      If sNumberPiecesInsertedBlanks.bParameter_IsChecked Then paragraph = section.AddParagraph(Statistic_Messages.TotINPRODParts_Msg & If(Not bPreview, Map.refProjectVM.BTLStructureVM.sTotINPRODParts, "69"))
      If sNumberPiecesCompleted.bParameter_IsChecked Then paragraph = section.AddParagraph(Statistic_Messages.TotDONEParts_Msg & If(Not bPreview, Map.refProjectVM.BTLStructureVM.sTotDONEParts, "5"))
   End Sub

   Public Sub DefineItemList(ByVal document As Document, nListType As ListTypes, bPreview As Boolean)
      document.DefaultPageSetup.DifferentFirstPageHeaderFooter = True
      'Recupero la lista delle colonne delle tabelle
      Dim sMachGroupList As ExpanderTable = Nothing
      If nListType = ListTypes.MACHGROUP Then
         sMachGroupList = Map.refPDFEditorVM.ExpanderList.FirstOrDefault(Function(x) x.sNameCategory = EgtMsg(61730)).ExpanderElementList.FirstOrDefault()  '  Lista Tabella MachGroup List
      ElseIf nListType = ListTypes.RAWPART Then
         sMachGroupList = Map.refPDFEditorVM.ExpanderList.FirstOrDefault(Function(x) x.sNameCategory = EgtMsg(61732)).ExpanderElementList.FirstOrDefault()  '  Lista Tabella Raw Part List
      ElseIf nListType = ListTypes.PART Then
         sMachGroupList = Map.refPDFEditorVM.ExpanderList.FirstOrDefault(Function(x) x.sNameCategory = EgtMsg(61733)).ExpanderElementList.FirstOrDefault()  '  Lista Tabella Part List
      End If
      Dim sName As EgtDataGridColumn = sMachGroupList.RawPartColumns.FirstOrDefault(Function(x) x.Name = COL_NAME)            '  Recupero colonna 'colName' con relativo checkbox 
      Dim sW As EgtDataGridColumn = sMachGroupList.RawPartColumns.FirstOrDefault(Function(x) x.Name = COL_W)                  '  Recupero colonna 'colW' con relativo checkbox 
      Dim sH As EgtDataGridColumn = sMachGroupList.RawPartColumns.FirstOrDefault(Function(x) x.Name = COL_H)                  '  Recupero colonna 'colH' con relativo checkbox 
      Dim sL As EgtDataGridColumn = sMachGroupList.RawPartColumns.FirstOrDefault(Function(x) x.Name = COL_L)                  '  Recupero colonna 'colL' con relativo checkbox 
      Dim sMaterial As EgtDataGridColumn = sMachGroupList.RawPartColumns.FirstOrDefault(Function(x) x.Name = COL_MATERIAL)    '  Recupero colonna 'colMaterial' con relativo checkbox 
      Dim sUsage As EgtDataGridColumn = sMachGroupList.RawPartColumns.FirstOrDefault(Function(x) x.Name = COL_USAGE)          '  Recupero colonna 'colUsage' con relativo checkbox 
      Dim sWaste As EgtDataGridColumn = sMachGroupList.RawPartColumns.FirstOrDefault(Function(x) x.Name = COL_WASTE)          '  Recupero colonna 'colWaste' con relativo checkbox 
      Dim sUTime As EgtDataGridColumn = sMachGroupList.RawPartColumns.FirstOrDefault(Function(x) x.Name = COL_UNITTIME)       '  Recupero colonna 'colUnitTime' con relativo checkbox 
      Dim sQty As EgtDataGridColumn = sMachGroupList.RawPartColumns.FirstOrDefault(Function(x) x.Name = COL_QTY)              '  Recupero colonna 'colQuantity' con relativo checkbox 
      Dim sPDN As EgtDataGridColumn = sMachGroupList.RawPartColumns.FirstOrDefault(Function(x) x.Name = COL_PDN)              '  Recupero colonna 'colPDN' con relativo checkbox 
      Dim sDesc As EgtDataGridColumn = sMachGroupList.RawPartColumns.FirstOrDefault(Function(x) x.Name = COL_DESC)            '  Recupero colonna 'colDescription' con relativo checkbox 
      Dim sCNT As EgtDataGridColumn = sMachGroupList.RawPartColumns.FirstOrDefault(Function(x) x.Name = COL_CNT)              '  Recupero colonna 'colCNT' con relativo checkbox 
      Dim sAdded As EgtDataGridColumn = sMachGroupList.RawPartColumns.FirstOrDefault(Function(x) x.Name = COL_ADDED)          '  Recupero colonna 'colAdded' con relativo checkbox 
      Dim sDone As EgtDataGridColumn = sMachGroupList.RawPartColumns.FirstOrDefault(Function(x) x.Name = COL_DONE)            '  Recupero colonna 'colDone' con relativo checkbox 
      Dim sUVolume As EgtDataGridColumn = sMachGroupList.RawPartColumns.FirstOrDefault(Function(x) x.Name = COL_UNITVOLUME)   '  Recupero colonna 'colUnitVolume' con relativo checkbox 
      Dim sTVolume As EgtDataGridColumn = sMachGroupList.RawPartColumns.FirstOrDefault(Function(x) x.Name = COL_TOTVOLUME)    '  Recupero colonna 'colTotVolume' con relativo checkbox 
      Dim sTTime As EgtDataGridColumn = sMachGroupList.RawPartColumns.FirstOrDefault(Function(x) x.Name = COL_TOTTIME)        '  Recupero colonna 'colTotTime' con relativo checkbox 
      Dim sINProd As EgtDataGridColumn = sMachGroupList.RawPartColumns.FirstOrDefault(Function(x) x.Name = COL_INPROD)        '  Recupero colonna 'colInProd' con relativo checkbox 
      Dim section As Section = document.LastSection()
      DefineItem(document, nListType, bPreview, sMachGroupList, sName, sW, sH, sL, sMaterial, sUsage, sWaste, sUTime, sQty, sPDN, sDesc, sCNT, sAdded, sDone, sUVolume, sTVolume, sTTime, sINProd, section)
   End Sub

   Private Sub DefineItem(document As Document, nListType As ListTypes, bPreview As Boolean, sMachGroupList As ExpanderTable, sName As EgtDataGridColumn, sW As EgtDataGridColumn, sH As EgtDataGridColumn, sL As EgtDataGridColumn, sMaterial As EgtDataGridColumn, sUsage As EgtDataGridColumn, sWaste As EgtDataGridColumn, sUTime As EgtDataGridColumn, sQty As EgtDataGridColumn, sPDN As EgtDataGridColumn, sDesc As EgtDataGridColumn, sCNT As EgtDataGridColumn, sAdded As EgtDataGridColumn, sDone As EgtDataGridColumn, sUVolume As EgtDataGridColumn, sTVolume As EgtDataGridColumn, sTTime As EgtDataGridColumn, sINProd As EgtDataGridColumn, section As Section)
      Dim countItemList As Integer
      Select Case nListType
         Case ListTypes.PART
            section.AddParagraph(EgtMsg(61733), "Heading2")
         Case ListTypes.MACHGROUP
            section.AddParagraph(EgtMsg(61730), "Heading2")
         Case ListTypes.RAWPART
            section.AddParagraph(EgtMsg(61732), "Heading2")
      End Select
      Dim table As 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
      ' Controllo se bPreview è true mostra il PDF di preview se no il PDF completo
      If bPreview Then
         Itemlist = New List(Of Object)
      Else
         Select Case nListType
            Case ListTypes.PART
               Itemlist = Map.refProjectVM.BTLStructureVM.BTLPartVMList
            Case ListTypes.MACHGROUP
               Itemlist = Map.refProjectVM.MachGroupPanelVM.MachGroupVMList
            Case ListTypes.RAWPART
               If Map.refMainMenuVM.SelPage = Pages.ONLYPRODPAGE Then Map.refStatisticsVM.LoadRawPart()
               Itemlist = Map.refStatisticsVM.RawPartList
         End Select
      End If
      countItemList = If(bPreview, Itemlist.Count, Itemlist.Count - 1)

      For PartIndex = -2 To countItemList
         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
            If bPreview Then
               ' recupero elemento della riga
               Select Case nListType
                  Case ListTypes.PART
                     BTLPartVM = Nothing
                  Case ListTypes.MACHGROUP
                     MachGroupVM = Nothing
                  Case ListTypes.RAWPART
                     RawPart = Nothing
               End Select
            Else
               ' 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
         End If
         Dim Columnlist As ObservableCollection(Of EgtDataGridColumn) = Nothing
         Select Case nListType
            Case ListTypes.PART
               Columnlist = sMachGroupList.RawPartColumns
            Case ListTypes.MACHGROUP
               Columnlist = sMachGroupList.RawPartColumns
            Case ListTypes.RAWPART
               Columnlist = sMachGroupList.RawPartColumns
         End Select
         For ColumnIndex = 0 To Columnlist.Count - 1
            Select Case Columnlist(ColumnIndex).Name
               Case COL_PDN
                  If sPDN.bIsChecked Then
                     If PartIndex = -2 Then
                        table.AddColumn(Unit.FromCentimeter(1))
                     ElseIf PartIndex = -1 Then
                        row.Cells(ColumnIndex).AddParagraph(Statistic_Messages.PDN_Msg)
                     ElseIf nListType = ListTypes.PART Then
                        row.Cells(ColumnIndex).AddParagraph(If(bPreview, 1, BTLPartVM.nPDN))
                     End If
                  ElseIf PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(0))
                  End If
               Case COL_ID
                  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 COL_W
                  If sW.bIsChecked Then
                     If PartIndex = -2 Then
                        Select Case nListType
                           Case ListTypes.PART
                              table.AddColumn(Unit.FromCentimeter(1))
                           Case ListTypes.MACHGROUP, ListTypes.RAWPART
                              table.AddColumn(Unit.FromCentimeter(2))
                        End Select
                     ElseIf PartIndex = -1 Then
                        row.Cells(ColumnIndex).AddParagraph(Statistic_Messages.W_Msg)
                     ElseIf bPreview Then
                        row.Cells(ColumnIndex).AddParagraph(If(nListType = ListTypes.PART, 200, 120))
                     Else
                        Select Case nListType
                           Case ListTypes.PART
                              row.Cells(ColumnIndex).AddParagraph(BTLPartVM.sW)
                           Case ListTypes.MACHGROUP
                              row.Cells(ColumnIndex).AddParagraph(MachGroupVM.dW)
                           Case ListTypes.RAWPART
                              row.Cells(ColumnIndex).AddParagraph(RawPart.sW)
                        End Select
                     End If
                  ElseIf PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(0))
                  End If
               Case COL_H
                  If sH.bIsChecked Then
                     If PartIndex = -2 Then
                        Select Case nListType
                           Case ListTypes.PART
                              table.AddColumn(Unit.FromCentimeter(1))
                           Case ListTypes.MACHGROUP, ListTypes.RAWPART
                              table.AddColumn(Unit.FromCentimeter(2))
                        End Select
                     ElseIf PartIndex = -1 Then
                        row.Cells(ColumnIndex).AddParagraph(Statistic_Messages.H_Msg)
                     ElseIf bPreview Then
                        row.Cells(ColumnIndex).AddParagraph(If(nListType = ListTypes.PART, 360, 160))
                     Else
                        Select Case nListType
                           Case ListTypes.PART
                              row.Cells(ColumnIndex).AddParagraph(BTLPartVM.sH)
                           Case ListTypes.MACHGROUP
                              row.Cells(ColumnIndex).AddParagraph(MachGroupVM.dH)
                           Case ListTypes.RAWPART
                              row.Cells(ColumnIndex).AddParagraph(RawPart.sH)
                        End Select
                     End If
                  ElseIf PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(0))
                  End If
               Case COL_L
                  If sL.bIsChecked Then
                     If PartIndex = -2 Then
                        Select Case nListType
                           Case ListTypes.PART
                              table.AddColumn(Unit.FromCentimeter(1.6))
                           Case ListTypes.MACHGROUP, ListTypes.RAWPART
                              table.AddColumn(Unit.FromCentimeter(2))
                        End Select
                     ElseIf PartIndex = -1 Then
                        row.Cells(ColumnIndex).AddParagraph(Statistic_Messages.L_Msg)
                     ElseIf bPreview Then
                        row.Cells(ColumnIndex).AddParagraph(If(nListType = ListTypes.PART, 6672.6, 15000))
                     Else
                        Select Case nListType
                           Case ListTypes.PART
                              row.Cells(ColumnIndex).AddParagraph(BTLPartVM.sL)
                           Case ListTypes.MACHGROUP
                              row.Cells(ColumnIndex).AddParagraph(MachGroupVM.dL)
                           Case ListTypes.RAWPART
                              row.Cells(ColumnIndex).AddParagraph(RawPart.sL)
                        End Select
                     End If
                  ElseIf PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(0))
                  End If
               Case COL_DESC
                  If sDesc.bIsChecked Then
                     If PartIndex = -2 Then
                        table.AddColumn(Unit.FromCentimeter(4))
                     ElseIf PartIndex = -1 Then
                        row.Cells(ColumnIndex).AddParagraph(Statistic_Messages.NAM_Msg)
                     ElseIf bPreview Then
                        row.Cells(ColumnIndex).AddParagraph(If(nListType = ListTypes.PART, "Arc.in salita smussato", 1))
                     Else
                        row.Cells(ColumnIndex).AddParagraph(If(nListType = ListTypes.PART, BTLPartVM.sNAM, MachGroupVM.nName))
                     End If
                  ElseIf PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(0))
                  End If
               Case COL_NAME
                  If sName.bIsChecked Then
                     If PartIndex = -2 Then
                        table.AddColumn(Unit.FromCentimeter(4))
                     ElseIf PartIndex = -1 Then
                        row.Cells(ColumnIndex).AddParagraph(Statistic_Messages.NAM_Msg)
                     ElseIf bPreview Then
                        row.Cells(ColumnIndex).AddParagraph(If(nListType = ListTypes.PART, "Arc.in salita smussato", 1))
                     Else
                        row.Cells(ColumnIndex).AddParagraph(If(nListType = ListTypes.PART, BTLPartVM.sNAM, MachGroupVM.nName))
                     End If
                  ElseIf PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(0))
                  End If
               Case COL_MATERIAL
                  If sMaterial.bIsChecked Then
                     If PartIndex = -2 Then
                        table.AddColumn(Unit.FromCentimeter(2))
                     ElseIf PartIndex = -1 Then
                        row.Cells(ColumnIndex).AddParagraph(Statistic_Messages.MAT_Msg)
                     ElseIf bPreview Then
                        row.Cells(ColumnIndex).AddParagraph("GL24h")
                     Else
                        Select Case nListType
                           Case ListTypes.PART
                              row.Cells(ColumnIndex).AddParagraph(BTLPartVM.sMATERIAL)
                           Case ListTypes.MACHGROUP
                              row.Cells(ColumnIndex).AddParagraph(MachGroupVM.sMATERIAL)
                           Case ListTypes.RAWPART
                              row.Cells(ColumnIndex).AddParagraph(RawPart.sMaterial)
                        End Select
                     End If
                  ElseIf PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(0))
                  End If
               Case COL_CNT
                  If sCNT.bIsChecked Then
                     If PartIndex = -2 Then
                        table.AddColumn(Unit.FromCentimeter(1.6))
                     ElseIf PartIndex = -1 Then
                        row.Cells(ColumnIndex).AddParagraph(Statistic_Messages.CNT_Msg)
                     ElseIf nListType = ListTypes.PART Then
                        row.Cells(ColumnIndex).AddParagraph(If(bPreview, 1, BTLPartVM.sCNT))
                     End If
                  ElseIf PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(0))
                  End If
               Case COL_ADDED
                  If sAdded.bIsChecked Then
                     If PartIndex = -2 Then
                        table.AddColumn(Unit.FromCentimeter(1.5))
                     ElseIf PartIndex = -1 Then
                        row.Cells(ColumnIndex).AddParagraph(Statistic_Messages.ADDED_Msg)
                     ElseIf nListType = ListTypes.PART Then
                        row.Cells(ColumnIndex).AddParagraph(If(bPreview, 0, BTLPartVM.sADDED))
                     End If
                  ElseIf PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(0))
                  End If
               Case COL_INPROD
                  If sINProd.bIsChecked Then
                     If PartIndex = -2 Then
                        table.AddColumn(Unit.FromCentimeter(2.5))
                     ElseIf PartIndex = -1 Then
                        row.Cells(ColumnIndex).AddParagraph(Statistic_Messages.INPROD_Msg)
                     ElseIf nListType = ListTypes.PART Then
                        row.Cells(ColumnIndex).AddParagraph(If(bPreview, 0, BTLPartVM.nINPROD))
                     End If
                  ElseIf PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(0))
                  End If
               Case COL_DONE
                  If sDone.bIsChecked Then
                     If PartIndex = -2 Then
                        table.AddColumn(Unit.FromCentimeter(1.5))
                     ElseIf PartIndex = -1 Then
                        row.Cells(ColumnIndex).AddParagraph(Statistic_Messages.DONE_Msg)
                     ElseIf nListType = ListTypes.PART Then
                        row.Cells(ColumnIndex).AddParagraph(If(bPreview, 0, BTLPartVM.nDONE))
                     End If
                  ElseIf PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(0))
                  End If
               Case COL_USAGE
                  If sUsage.bIsChecked Then
                     If PartIndex = -2 Then
                        table.AddColumn(Unit.FromCentimeter(2))
                     ElseIf PartIndex = -1 Then
                        row.Cells(ColumnIndex).AddParagraph(Statistic_Messages.Used_Msg)
                     ElseIf nListType = ListTypes.MACHGROUP Then
                        row.Cells(ColumnIndex).AddParagraph(If(bPreview, 99.719, MachGroupVM.sUsage))
                     End If
                  ElseIf PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(0))
                  End If
               Case COL_WASTE
                  If sWaste.bIsChecked Then
                     If PartIndex = -2 Then
                        table.AddColumn(Unit.FromCentimeter(2))
                     ElseIf PartIndex = -1 Then
                        row.Cells(ColumnIndex).AddParagraph(Statistic_Messages.Waste_Msg)
                     ElseIf nListType = ListTypes.MACHGROUP Then
                        row.Cells(ColumnIndex).AddParagraph(If(bPreview, 0.5, MachGroupVM.sWaste))
                     End If
                  ElseIf PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(0))
                  End If
               Case COL_UNITVOLUME
                  If sUVolume.bIsChecked Then
                     If PartIndex = -2 Then
                        table.AddColumn(If(nListType = ListTypes.PART, Unit.FromCentimeter(1.5), Unit.FromCentimeter(2)))
                     ElseIf PartIndex = -1 Then
                        row.Cells(ColumnIndex).AddParagraph(Statistic_Messages.UNIT_VOLUME_Msg)
                     ElseIf bPreview Then
                        row.Cells(ColumnIndex).AddParagraph(0.48)
                     Else
                        row.Cells(ColumnIndex).AddParagraph(If(nListType = ListTypes.PART, BTLPartVM.sUnitVolume, MachGroupVM.dUnitVolume))
                     End If
                  ElseIf PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(0))
                  End If
               Case COL_TOTVOLUME
                  If sTVolume.bIsChecked Then
                     If PartIndex = -2 Then
                        table.AddColumn(Unit.FromCentimeter(1.5))
                     ElseIf PartIndex = -1 Then
                        row.Cells(ColumnIndex).AddParagraph(Statistic_Messages.TOT_VOLUME_Msg)
                     ElseIf nListType = ListTypes.PART Then
                        row.Cells(ColumnIndex).AddParagraph(If(bPreview, 0.48, BTLPartVM.sTotVolume))
                     End If
                  ElseIf PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(0))
                  End If
               Case COL_UNITTIME
                  If sUTime.bIsChecked Then
                     If PartIndex = -2 Then
                        table.AddColumn(Unit.FromCentimeter(2.2))
                     ElseIf PartIndex = -1 Then
                        row.Cells(ColumnIndex).AddParagraph(Statistic_Messages.UNIT_TIME_Msg)
                     ElseIf bPreview Then
                        row.Cells(ColumnIndex).AddParagraph(If(nListType = ListTypes.PART, "00:00:00", "00:13:07"))
                     Else
                        row.Cells(ColumnIndex).AddParagraph(If(nListType = ListTypes.PART, BTLPartVM.sUnitTime, MachGroupVM.sUnitTime))
                     End If
                  ElseIf PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(0))
                  End If
               Case COL_TOTTIME
                  If sTTime.bIsChecked Then
                     If PartIndex = -2 Then
                        table.AddColumn(Unit.FromCentimeter(2))
                     ElseIf PartIndex = -1 Then
                        row.Cells(ColumnIndex).AddParagraph(Statistic_Messages.TOT_TIME_Msg)
                     ElseIf nListType = ListTypes.PART Then
                        row.Cells(ColumnIndex).AddParagraph(If(bPreview, "00:00:00", BTLPartVM.sTotTime))
                     End If
                  ElseIf PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(0))
                  End If
               Case COL_QTY
                  If sQty.bIsChecked Then
                     If PartIndex = -2 Then
                        table.AddColumn(Unit.FromCentimeter(2))
                     ElseIf PartIndex = -1 Then
                        row.Cells(ColumnIndex).AddParagraph(Statistic_Messages.QTY_Msg)
                     ElseIf nListType = ListTypes.RAWPART Then
                        row.Cells(ColumnIndex).AddParagraph(If(bPreview, 11, RawPart.nQuantity))
                     End If
                  ElseIf PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(0))
                  End If
            End Select
         Next
      Next
      ' aggiungo tabella all'ultima sezione
      document.LastSection.Add(table)
   End Sub

   Public Sub CreateRawPartPages(ByVal document As Document, bPreview As Boolean)
      document.DefaultPageSetup.DifferentFirstPageHeaderFooter = False
      'Recupero la lista delle colonne delle tabelle
      Dim sMachGroupList As ExpanderTable = Map.refPDFEditorVM.ExpanderList.FirstOrDefault(Function(x) x.sNameCategory = EgtMsg(61731)).ExpanderElementList.FirstOrDefault()  '  Lista Tabella Raw Part
      Dim sPDN As EgtDataGridColumn = sMachGroupList.RawPartColumns.FirstOrDefault(Function(x) x.Name = COL_PDN)                                                              '  Recupero colonna 'colPDN' con relativo checkbox 
      Dim sW As EgtDataGridColumn = sMachGroupList.RawPartColumns.FirstOrDefault(Function(x) x.Name = COL_W)                                                                  '  Recupero colonna 'colW' con relativo checkbox 
      Dim sH As EgtDataGridColumn = sMachGroupList.RawPartColumns.FirstOrDefault(Function(x) x.Name = COL_H)                                                                  '  Recupero colonna 'colH' con relativo checkbox 
      Dim sL As EgtDataGridColumn = sMachGroupList.RawPartColumns.FirstOrDefault(Function(x) x.Name = COL_L)                                                                  '  Recupero colonna 'colL' con relativo checkbox 
      Dim sDesc As EgtDataGridColumn = sMachGroupList.RawPartColumns.FirstOrDefault(Function(x) x.Name = COL_DESC)                                                            '  Recupero colonna 'colDescription' con relativo checkbox 
      Dim sMaterial As EgtDataGridColumn = sMachGroupList.RawPartColumns.FirstOrDefault(Function(x) x.Name = COL_MATERIAL)                                                    '  Recupero colonna 'colMaterial' con relativo checkbox 
      Dim nOrigMachGroupId As Integer = EgtGetCurrMachGroup()
      ' Controllo se bPreview è true mostra il PDF di preview se no il PDF completo
      If bPreview Then
         CreateRawPartPage(document, sMachGroupList, sPDN, sW, sH, sL, sDesc, sMaterial, Nothing, bPreview)
      Else
         For Each RawPart As MyMachGroupVM In Map.refProjectVM.MachGroupPanelVM.MachGroupVMList
            CreateRawPartPage(document, sMachGroupList, sPDN, sW, sH, sL, sDesc, sMaterial, RawPart, bPreview)
         Next
      End If
   End Sub

   Private Sub CreateRawPartPage(document As Document, sMachGroupList As ExpanderTable, sPDN As EgtDataGridColumn, sW As EgtDataGridColumn, sH As EgtDataGridColumn, sL As EgtDataGridColumn, sDesc As EgtDataGridColumn, sMaterial As EgtDataGridColumn, RawPart As MyMachGroupVM, bPreview As Boolean)
      Dim section As Section = document.AddSection()
      ' cambio orientamento a verticale
      Dim pgSetup As PageSetup = document.DefaultPageSetup.Clone()
      pgSetup.Orientation = Orientation.Landscape
      section.PageSetup = pgSetup
      Dim PartVMList As IEnumerable(Of Object)
      Dim countPartVMList As Integer
      If IsNothing(RawPart) Then
         PartVMList = New List(Of Object)
         CreateRawPartImage(section, sMachGroupList, 44143, 1, bPreview)
         countPartVMList = PartVMList.Count
      Else
         PartVMList = RawPart.PartVMList
         CreateRawPartImage(section, sMachGroupList, RawPart.Id, RawPart.Name, bPreview)
         countPartVMList = PartVMList.Count - 1
      End If

      ' aggiungo tabella dei pezzi
      Dim table As Table = New Table()
      table.Borders.Width = 0.75
      For PartIndex = -2 To countPartVMList
         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 = PartVMList(PartIndex)
         End If
         Dim Columnlist As ObservableCollection(Of EgtDataGridColumn) = sMachGroupList.RawPartColumns
         For ColumnIndex = 0 To Columnlist.Count - 1
            Select Case Columnlist(ColumnIndex).Name
               Case COL_PDN
                  If sPDN.bIsChecked Then
                     If PartIndex = -2 Then
                        table.AddColumn(Unit.FromCentimeter(1.5))
                     ElseIf PartIndex = -1 Then
                        row.Cells(ColumnIndex).AddParagraph(Statistic_Messages.PDN_Msg)
                     Else
                        row.Cells(ColumnIndex).AddParagraph(If(IsNothing(RawPart), 55, PartVM.nPDN))
                     End If
                  ElseIf PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(0))
                  End If
               Case COL_W
                  If sW.bIsChecked Then
                     If PartIndex = -2 Then
                        table.AddColumn(Unit.FromCentimeter(2))
                     ElseIf PartIndex = -1 Then
                        row.Cells(ColumnIndex).AddParagraph(Statistic_Messages.W_Msg)
                     Else
                        row.Cells(ColumnIndex).AddParagraph(If(IsNothing(RawPart), 120, PartVM.sW))
                     End If
                  ElseIf PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(0))
                  End If
               Case COL_H
                  If sH.bIsChecked Then
                     If PartIndex = -2 Then
                        table.AddColumn(Unit.FromCentimeter(2))
                     ElseIf PartIndex = -1 Then
                        row.Cells(ColumnIndex).AddParagraph(Statistic_Messages.H_Msg)
                     Else
                        row.Cells(ColumnIndex).AddParagraph(If(IsNothing(RawPart), 160, PartVM.sH))
                     End If
                  ElseIf PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(0))
                  End If
               Case COL_L
                  If sL.bIsChecked Then
                     If PartIndex = -2 Then
                        table.AddColumn(Unit.FromCentimeter(2))
                     ElseIf PartIndex = -1 Then
                        row.Cells(ColumnIndex).AddParagraph(Statistic_Messages.L_Msg)
                     Else
                        row.Cells(ColumnIndex).AddParagraph(If(IsNothing(RawPart), 185.9, PartVM.sL))
                     End If
                  ElseIf PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(0))
                  End If
               Case COL_DESC
                  If sDesc.bIsChecked Then
                     If PartIndex = -2 Then
                        table.AddColumn(Unit.FromCentimeter(4))
                     ElseIf PartIndex = -1 Then
                        row.Cells(ColumnIndex).AddParagraph(Statistic_Messages.NAM_Msg)
                     Else
                        row.Cells(ColumnIndex).AddParagraph(If(IsNothing(RawPart), "Correntini", PartVM.sNAM))
                     End If
                  ElseIf PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(0))
                  End If
               Case COL_NAME
                  If PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(4))
                  ElseIf PartIndex = -1 Then
                     row.Cells(ColumnIndex).AddParagraph(Statistic_Messages.NAM_Msg)
                  Else
                     row.Cells(ColumnIndex).AddParagraph(PartVM.sNAM)
                  End If
               Case COL_MATERIAL
                  If sMaterial.bIsChecked Then
                     If PartIndex = -2 Then
                        table.AddColumn(Unit.FromCentimeter(2))
                     ElseIf PartIndex = -1 Then
                        row.Cells(ColumnIndex).AddParagraph(Statistic_Messages.MAT_Msg)
                     Else
                        row.Cells(ColumnIndex).AddParagraph(If(IsNothing(RawPart), "GL24h", PartVM.sMATERIAL))
                     End If
                  ElseIf PartIndex = -2 Then
                     table.AddColumn(Unit.FromCentimeter(0))
                  End If
            End Select
         Next
      Next
      ' aggiungo tabella all'ultima sezione
      section.Add(table)
   End Sub

   Private Sub CreateRawPartImage(section As Section, sMachGroupList As ExpanderTable, RawPartId As Integer, RawPartName As String, bPreview As Boolean)
      ' creo immagine
      Dim sImagePath As String
      If File.Exists(Map.refMainWindowVM.MainWindowM.sTempDir & "\Image" & RawPartId & ".png") Then
         sImagePath = Map.refMainWindowVM.MainWindowM.sTempDir & "\Image" & RawPartId & ".png"
      Else
         sImagePath = Map.refMainWindowVM.MainWindowM.sTempDir & "\ImageNotFound.png"
      End If
      If bPreview Then
         EgtSetCurrMachGroup(RawPartId)
         Core.ViewPanelVM.BWSetView(If(Core.ViewPanelVM.Type = BWType.BEAM, VT.FRONT, VT.TOP), False)
         EgtZoom(ZM.ALL, False)
         EgtGetImage(SM.HIDDENLINE, New Color3d(255, 255, 255), New Color3d(255, 255, 255), 3000, 1000, sImagePath)
      End If
      ' aggiungo immagine
      If sMachGroupList.bSection_IsChecked Then section.AddParagraph(RawPartName)
      If sMachGroupList.bImage_IsChecked Then
         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
      End If
   End Sub

   Public Sub DefineLogo(section As Section, sImageLogo As String)
      Dim Image As MigraDoc.DocumentObjectModel.Shapes.Image = section.Headers.FirstPage.AddImage(sImageLogo)
      Image.Width = Unit.FromMillimeter(245)
      Image.Height = Unit.FromMillimeter(40)
      Image.LockAspectRatio = True
      Image.RelativeVertical = RelativeVertical.Line
      Image.Top = ShapePosition.Top
      Image.Left = ShapePosition.Left
      Image.WrapFormat.Style = WrapStyle.Through
      Image.RelativeHorizontal = RelativeHorizontal.Margin
   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 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

#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 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 New Chart With {
         .Left = 0,
         .Width = Unit.FromCentimeter(16),
         .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
