Documentimo.ps1
using namespace Xceed.Document.NET using namespace Xceed.Words.NET function Documentimo { [CmdletBinding()] [alias('Doc', 'New-Documentimo')] param([Parameter(Position = 0)][ValidateNotNull()][ScriptBlock] $Content = $(Throw "Documentimo requires opening and closing brace."), [string] $FilePath, [switch] $Open) $WordDocument = New-WordDocument -FilePath $FilePath if ($null -ne $Content) { $Array = Invoke-Command -ScriptBlock $Content New-WordProcessing -Content $Array -WordDocument $WordDocument } Save-WordDocument -WordDocument $WordDocument -Supress $true -Language 'en-US' -Verbose -OpenDocument:$Open } function DocChart { [CmdletBinding()] [alias('DocumentimoChart', 'New-DocumentimoChart')] param([Array] $DataTable, [string] $Title, [string] $Key, [string] $Value, [ChartLegendPosition] $LegendPosition = [ChartLegendPosition]::Bottom, [bool] $LegendOverlay) [PSCustomObject] @{ObjectType = 'ChartPie' DataTable = $DataTable Title = $Title Key = $Key Value = $Value LegendPosition = $LegendPosition LegendOverlay = $LegendOverlay } } function DocList { [CmdletBinding()] [alias('DocumentimoList', 'New-DocumentimoList')] param([ScriptBlock] $ListItems, [alias('ListType')][ListItemType] $Type = [ListItemType]::Bulleted) [PSCustomObject] @{ObjectType = 'List' ListItems = Invoke-Command -ScriptBlock $ListItems Type = $Type } } function DocListItem { [CmdletBinding()] [alias('DocumentimoListItem', 'New-DocumentimoListItem')] param([ValidateRange(0, 8)] [int] $Level, [string] $Text, [nullable[int]] $StartNumber, [bool]$TrackChanges = $false, [bool]$ContinueNumbering = $false, [bool]$Supress = $false) [PSCustomObject] @{ObjectType = 'ListItem' Level = $Level Text = $Text StartNumber = $StartNumber TrackChanges = $TrackChanges ContinueNumbering = $ContinueNumbering } } function DocNumbering { [CmdletBinding()] [alias('DocumentimoNumbering', 'New-DocumentimoNumbering')] param([Parameter(Position = 0)][ScriptBlock] $Content, [string] $Text, [int] $Level = 0, [ListItemType] $Type = [ListItemType]::Numbered, [HeadingType] $Heading = [HeadingType]::Heading1) [PSCustomObject] @{ObjectType = 'TocItem' Text = $Text Content = & $Content Level = $Level Type = $Type Heading = $Heading } } function DocPageBreak { [CmdletBinding()] [alias('DocumentimoPageBreak', 'New-DocumentimoPageBreak')] param([int] $Count = 1) [PSCustomObject] @{ObjectType = 'PageBreak' Count = $Count } } function DocTable { [CmdletBinding()] [alias('DocumentimoTable', 'New-DocumentimoTable')] param([Array] $DataTable, [AutoFit] $AutoFit = [AutoFit]::Window, [TableDesign] $Design, [Direction] $Direction = [Direction]::LeftToRight, [int] $MaximumColumns = 5, [string] $OverwriteTitle, [Alignment] $OverwriteTitleAlignment = [Alignment]::center, [System.Drawing.Color] $OverwriteTitleColor = [System.Drawing.Color]::Black) [PSCustomObject] @{ObjectType = 'Table' DataTable = $DataTable AutoFit = $AutoFit Design = $Design Direction = $Direction MaximumColumns = $MaximumColumns OverwriteTitle = $OverwriteTitle OverwriteTitleAlignment = $OverwriteTitleAlignment OverwriteTitleColor = $OverwriteTitleColor } } function DocText { [CmdletBinding()] [alias('DocumentimoText', 'New-DocumentimoText')] param([Parameter(Mandatory = $false, Position = 0)][ScriptBlock] $TextBlock, [String[]]$Text, [System.Drawing.Color[]]$Color = @(), [switch] $LineBreak) if ($TextBlock) { $Text = (Invoke-Command -ScriptBlock $TextBlock) } [PSCustomObject] @{ObjectType = 'Text' Text = $Text Color = $Color LineBreak = $LineBreak } } function DocToc { [CmdletBinding()] [alias('DocumentimoTOC', 'New-DocumentimoTOC')] param([string] $Title, [int] $RightTabPos, [TableOfContentsSwitches] $Switches) [PSCustomObject] @{ObjectType = 'TOC' Title = $Title RightTabPos = $RightTabPos Switches = $Switches } } function New-DocWordChart { [CmdletBinding()] param([Container] $WordDocument, [PSCustomObject] $Parameters) [Array] $DataTable = $Parameters.DataTable [string] $Key = $Parameters.Key [string] $Value = $Parameters.Value [ChartLegendPosition] $LegendPosition = $Parameters.LegendPosition [bool] $LegendOverlay = $Parameters.LegendOverlay [string] $Title = $Parameters.Title if ($DataTable.Count -gt 0) { if ($DataTable[0] -is [System.Collections.IDictionary]) { $TemporaryNames = $DataTable.Keys $Names = foreach ($Name in $TemporaryNames) { "$Name ($($DataTable.$Name))" } $Values = $DataTable.Values } else { if (($Key -ne '') -and ($Value -ne '')) { $Names = for ($i = 0; $i -lt $DataTable.Count; $i++) { "$($DataTable[$i].$Key) ($($DataTable[$i].$Value))" } $Values = $DataTable.$Value } else { return } } if (($Values.Count -eq $Names.Count) -and ($Values.Count -gt 0)) { Add-WordParagraph -WordDocument $WordDocument -Supress $True Add-WordPieChart -WordDocument $WordDocument -ChartName $Title -Names $Names -Values $Values -ChartLegendPosition $LegendPosition -ChartLegendOverlay $LegendOverlay } else { Write-Warning "DocumentimoChart - Names and Values count doesn't match or equals 0" } } } function New-DocWordList { [CmdletBinding()] param([Container] $WordDocument, [PSCustomObject] $Parameters) $List = $null foreach ($Item in $Parameters.ListItems) { if ($null -eq $List) { $List = $WordDocument.AddList($Item.Text, $Item.Level, $Parameters.Type, $Item.StartNumber, $Item.TrackChanges, $Item.ContinueNumbering) } else { $List = $WordDocument.AddListItem($List, $Item.Text, $Item.Level, $Parameters.Type, $Item.StartNumber, $Item.TrackChanges, $Item.ContinueNumbering) } } $null = Add-WordListItem -WordDocument $WordDocument -List $List } function New-DocWordNumbering { [CmdletBinding()] param([Container] $WordDocument, [PSCustomObject] $Parameters) $TOC = Add-WordTocItem -WordDocument $WordDocument -Text $Parameters.Text -ListLevel $Parameters.Level -ListItemType $Parameters.Type -HeadingType $Parameters.Heading if ($Parameters.Content) { New-WordProcessing -Content $Parameters.Content -WordDocument $WordDocument } } function New-DocWordPageBreak { [CmdletBinding()] param([Container] $WordDocument, [PSCustomObject] $Parameters) $i = 0 While ($i -lt $Parameters.Count) { Write-Verbose "New-WordBlockPageBreak - PageBreak $i" Add-WordPageBreak -Supress $True -WordDocument $WordDocument $i++ } } function New-DocWordTable { [CmdletBinding()] param([Container] $WordDocument, [PSCustomObject] $Parameters) if ($Parameters.OverWriteTitle) { [Alignment] $TitleAlignment = $Parameters.OverwriteTitleAlignment [System.Drawing.Color] $TitleColor = $Parameters.OverwriteTitleColor $Table = Add-WordTable -WordDocument $WordDocument -Supress $false -DataTable $Parameters.DataTable -Design $Parameters.Design -AutoFit $Parameters.AutoFit $Table = Set-WordTableRowMergeCells -Table $Table -RowNr 0 -MergeAll $TableParagraph = Get-WordTableRow -Table $Table -RowNr 0 -ColumnNr 0 $TableParagraph = Set-WordText -Paragraph $TableParagraph -Text $Parameters.OverwriteTitle -Alignment $TitleAlignment -Color $TitleColor } else { $Table = Add-WordTable -WordDocument $WordDocument -Supress $true -DataTable $Parameters.DataTable -Design $Parameters.Design -AutoFit $Parameters.AutoFit } } function New-DocWordText { [CmdletBinding()] param([Container] $WordDocument, [PSCustomObject] $Parameters) if ($Parameters.Text) { Add-WordText -WordDocument $WordDocument -Text $Parameters.Text -Color $Parameters.Color -Supress $true } if ($Parameters.LineBreak) { Add-WordParagraph -WordDocument $WordDocument -Supress $True } } function New-DocWordTOC { [CmdletBinding()] param([Container] $WordDocument, [PSCustomObject] $Parameters) Add-WordTOC -WordDocument $WordDocument -Title $Parameters.Title -Switches $Parameters.Switches -RightTabPos $Parameters.RightTabPos -Supress $True } function New-WordProcessing { [CmdletBinding()] param([Array] $Content, [Container] $WordDocument) if ($Content.Count -gt 0) { foreach ($Parameters in $Content) { if ($Parameters.ObjectType -eq 'List') { New-DocWordList -WordDocument $WordDocument -Parameters $Parameters } elseif ($Parameters.ObjectType -eq 'Table') { New-DocWordTable -WordDocument $WordDocument -Parameters $Parameters } elseif ($Parameters.ObjectType -eq 'TOC') { New-DocWordTOC -WordDocument $WordDocument -Parameters $Parameters } elseif ($Parameters.ObjectType -eq 'Text') { New-DocWordText -WordDocument $WordDocument -Parameters $Parameters } elseif ($Parameters.ObjectType -eq 'TocItem') { New-DocWordNumbering -WordDocument $WordDocument -Parameters $Parameters } elseif ($Parameters.ObjectType -eq 'PageBreak') { New-DocWordPageBreak -WordDocument $WordDocument -Parameters $Parameters } elseif ($Parameters.ObjectType -eq 'ChartPie') { New-DocWordChart -WordDocument $WordDocument -Parameters $Parameters } } } } |