[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
本帖最后由 buyiyang 于 2024-8-6 20:56 编辑

遍历目录合并jpg为pdf,保存为ansi编码的bat
  1. <# :
  2. @echo off
  3. cd /d "%~dp0"
  4. for /f "delims=" %%i in ('dir /b /ad /s') do (
  5.     pushd "%%i"&&(
  6.     if exist *.jpg (
  7.         echo,%%i
  8.         powershell -c "gc '%~0' | Out-String | iex"
  9.         )
  10.     popd
  11.     )||set /p=%%i<nul
  12. )
  13. pause&exit
  14. #>
  15. [void][Reflection.Assembly]::LoadWithPartialName('System.Drawing')
  16. function Img2Pdf($paramImgGroup) {
  17.     $listImg = New-Object 'System.Collections.ArrayList'
  18.     ForEach ($file In $groups[$paramImgGroup]) {
  19.         [void]$listImg.Add([Drawing.Image]::FromFile($file))
  20.     }
  21.     $docPrint = New-Object 'Drawing.Printing.PrintDocument'
  22.     $settingsPrint = New-Object 'Drawing.Printing.PrinterSettings'
  23.     $settingsPrint.PrinterName = 'Microsoft Print to PDF'
  24.     $settingsPrint.PrintToFile = $true
  25.     $settingsPrint.PrintFileName = $paramImgGroup + '.pdf'
  26.     $docPrint.PrinterSettings = $settingsPrint
  27.     $docPrint.DefaultPageSettings.Landscape = $true
  28.     $docPrint.add_PrintPage({
  29.         $imgCurrent = $listImg[0]
  30.         $ratioWidth = $_.MarginBounds.Width / $imgCurrent.Width
  31.         $ratioHeight = $_.MarginBounds.Height / $imgCurrent.Height
  32.         $ratioSelected = [Math]::Min($ratioWidth, $ratioHeight)
  33.         $widthNew = [int]($imgCurrent.Width * $ratioSelected)
  34.         $heightNew = [int]($imgCurrent.Height * $ratioSelected)
  35.         $_.Graphics.DrawImage($imgCurrent, ($_.MarginBounds.Left + ($_.MarginBounds.Width - $widthNew) / 2), ($_.MarginBounds.Top + ($_.MarginBounds.Height - $heightNew) / 2), $widthNew, $heightNew)
  36.         $imgCurrent.Dispose()
  37.         $listImg.RemoveAt(0)
  38.         If ($listImg.Count -gt 0) { $_.HasMorePages = $true } Else { $_.HasMorePages = $false }
  39.     })
  40.     $docPrint.Print()
  41.     $docPrint.Dispose()
  42. }
  43. $files = Get-ChildItem -Filter "*.jpg"
  44. $groups = @{}
  45. ForEach ($file In $files) {
  46.     $partChinese = [regex]::Match($file.Name, '^[\u4e00-\u9fff]+').Value
  47.     If ($partChinese) {
  48.         If (-not $groups.ContainsKey($partChinese)) {
  49.             $groups[$partChinese] = @()
  50.         }
  51.         $groups[$partChinese] += $file
  52.     }
  53. }
  54. ForEach ($group In $groups.Keys) {
  55.     Write-Output "分组:$group"
  56.     Img2Pdf -paramImgGroup $group
  57. }
复制代码

TOP

返回列表