Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [CmdLetBinding()]
- Param
- (
- [Parameter(Mandatory=$true)]
- [string]$InputFile
- , [string]$OutPutPath = "$((Get-Item -path $inputfile).DirectoryName)\Print_pptxToPdf"
- )
- $PFile = get-item -Path $InputFile
- if ($PFile.Extension -ne '.pptx')
- {
- Write-Host -ForegroundColor red "Not a Valid PowerPoint file"
- break;
- }
- if (!(Test-Path $OutPutPath))
- {
- New-Item -Path $OutPutPath -ItemType Directory
- }
- Get-Process | where {$_.name -match 'powerpnt'} | kill
- $PowerPoint = New-Object -ComObject PowerPoint.application
- #$PowerPoint.Visible = msoFalse | Out-Null
- $doc = $PowerPoint.Presentations.Open($PFile.FullName)
- #Creating Sections
- $SectionCount = $doc.SectionProperties.Count
- $sectionlist = New-Object System.Collections.Generic.list[PsObject]
- for ($i = 1; $i -le $SectionCount; $i++)
- {
- $hash = @{
- 'Section' = $i
- 'Name' = $null
- 'Pages' = 0
- }
- $obj = New-Object -TypeName PsObject -Property $hash
- $sectionlist.Add($obj)
- }
- #Add PageNumbers and Names to sections
- for ($i = 1; $i -le $SectionCount;$i++)
- {
- $sectionlist[$i - 1].Pages = $doc.SectionProperties.SlidesCount($i)
- $sectionlist[$i - 1].Name = $doc.SectionProperties.Name($i)
- }
- #printer settings
- $doc.PrintOptions.ActivePrinter = (Get-Printer | where {$_.name -match 'print to pdf'}).Name
- #Print
- #Append numbers if needed
- $Che = Get-ChildItem -Path $OutPutPath
- if($che -ne $null)
- {
- [int]$fn = $che[-1].Name.split("-")[0]
- $fn = $fn
- } else {$fn = 0}
- $i = 0
- foreach ($section in $sectionlist)
- {
- #Remove Illegal Charecters, List so far ","
- $newname = "$(($section.Section + $fn).ToString("00")) - $($PFile.BaseName) - $($sectionlist[$i].name).pdf"
- $newname = [regex]::Replace($newname,",","")
- #Start Print to PDF
- $startpage = $doc.SectionProperties.FirstSlide($section.Section)
- $Endpage = $startpage + $doc.SectionProperties.SlidesCount($section.Section)
- $doc.PrintOut($startpage, $Endpage,"$OutPutPath\$newname",1)
- $i++
- }
- $c = 0
- do
- {
- $c++
- $check = Get-ChildItem -LiteralPath $OutPutPath -File | where {$_.name -match $PFile.BaseName}
- #clear
- Write-Host -f Yellow "Printing $c"
- Start-Sleep 1
- } until ($check.count -eq $SectionCount)
- Write-Host -f green "$($Pfile.name) Printed to $OutPutPath - $SectionCount PDF files created"
- $doc.Close() | Out-Null
- $PowerPoint.Quit()
- Get-Process | where {$_.name -match 'powerpnt'} | kill
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement