Advertisement
upz

PrintPowerPointSections To PDF

upz
May 1st, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [CmdLetBinding()]
  2. Param
  3. (
  4.         [Parameter(Mandatory=$true)]
  5.         [string]$InputFile
  6.     ,   [string]$OutPutPath = "$((Get-Item -path $inputfile).DirectoryName)\Print_pptxToPdf"
  7. )
  8.  
  9. $PFile = get-item -Path $InputFile
  10.  
  11. if ($PFile.Extension -ne '.pptx')
  12. {
  13.     Write-Host -ForegroundColor red "Not a Valid PowerPoint file"
  14.     break;
  15. }
  16.  
  17. if (!(Test-Path $OutPutPath))
  18. {
  19.     New-Item -Path $OutPutPath -ItemType Directory
  20. }
  21.  
  22. Get-Process | where {$_.name -match 'powerpnt'} | kill
  23. $PowerPoint = New-Object -ComObject PowerPoint.application
  24. #$PowerPoint.Visible = msoFalse | Out-Null
  25. $doc = $PowerPoint.Presentations.Open($PFile.FullName)
  26.  
  27. #Creating Sections
  28. $SectionCount = $doc.SectionProperties.Count
  29. $sectionlist = New-Object System.Collections.Generic.list[PsObject]
  30.  
  31. for ($i = 1; $i -le $SectionCount; $i++)
  32. {
  33.     $hash = @{
  34.     'Section' = $i
  35.     'Name' = $null
  36.     'Pages' = 0
  37.     }
  38.     $obj = New-Object -TypeName PsObject -Property $hash
  39.     $sectionlist.Add($obj)
  40. }
  41.  
  42. #Add PageNumbers and Names to sections
  43. for ($i = 1; $i -le $SectionCount;$i++)
  44. {
  45.     $sectionlist[$i - 1].Pages = $doc.SectionProperties.SlidesCount($i)
  46.     $sectionlist[$i - 1].Name = $doc.SectionProperties.Name($i)    
  47. }
  48.  
  49. #printer settings
  50. $doc.PrintOptions.ActivePrinter = (Get-Printer | where {$_.name -match 'print to pdf'}).Name
  51.  
  52. #Print
  53.  
  54. #Append numbers if needed
  55. $Che = Get-ChildItem -Path $OutPutPath
  56. if($che -ne $null)
  57. {
  58.     [int]$fn = $che[-1].Name.split("-")[0]
  59.     $fn = $fn
  60. } else {$fn = 0}
  61.  
  62. $i = 0
  63. foreach ($section in $sectionlist)
  64. {
  65.     #Remove Illegal Charecters, List so far ","
  66.     $newname = "$(($section.Section + $fn).ToString("00")) - $($PFile.BaseName) - $($sectionlist[$i].name).pdf"
  67.     $newname = [regex]::Replace($newname,",","")
  68.    
  69.     #Start Print to PDF
  70.     $startpage = $doc.SectionProperties.FirstSlide($section.Section)
  71.     $Endpage = $startpage + $doc.SectionProperties.SlidesCount($section.Section)
  72.     $doc.PrintOut($startpage, $Endpage,"$OutPutPath\$newname",1)
  73.     $i++
  74. }
  75.  
  76. $c = 0
  77. do
  78. {
  79.     $c++
  80.     $check = Get-ChildItem -LiteralPath $OutPutPath -File | where {$_.name -match $PFile.BaseName}
  81.     #clear
  82.     Write-Host -f Yellow "Printing $c"
  83.     Start-Sleep 1
  84. } until ($check.count -eq $SectionCount)
  85.  
  86. Write-Host -f green "$($Pfile.name) Printed to $OutPutPath - $SectionCount PDF files created"
  87.  
  88. $doc.Close() | Out-Null
  89. $PowerPoint.Quit()
  90. Get-Process | where {$_.name -match 'powerpnt'} | kill
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement