本帖最后由 buyiyang 于 2024-8-6 20:56 编辑
遍历目录合并jpg为pdf,保存为ansi编码的bat- <# :
- @echo off
- cd /d "%~dp0"
- for /f "delims=" %%i in ('dir /b /ad /s') do (
- pushd "%%i"&&(
- if exist *.jpg (
- echo,%%i
- powershell -c "gc '%~0' | Out-String | iex"
- )
- popd
- )||set /p=%%i<nul
- )
- pause&exit
- #>
-
- [void][Reflection.Assembly]::LoadWithPartialName('System.Drawing')
-
- function Img2Pdf($paramImgGroup) {
- $listImg = New-Object 'System.Collections.ArrayList'
- ForEach ($file In $groups[$paramImgGroup]) {
- [void]$listImg.Add([Drawing.Image]::FromFile($file))
- }
-
- $docPrint = New-Object 'Drawing.Printing.PrintDocument'
- $settingsPrint = New-Object 'Drawing.Printing.PrinterSettings'
- $settingsPrint.PrinterName = 'Microsoft Print to PDF'
- $settingsPrint.PrintToFile = $true
- $settingsPrint.PrintFileName = $paramImgGroup + '.pdf'
- $docPrint.PrinterSettings = $settingsPrint
- $docPrint.DefaultPageSettings.Landscape = $true
-
- $docPrint.add_PrintPage({
- $imgCurrent = $listImg[0]
- $ratioWidth = $_.MarginBounds.Width / $imgCurrent.Width
- $ratioHeight = $_.MarginBounds.Height / $imgCurrent.Height
- $ratioSelected = [Math]::Min($ratioWidth, $ratioHeight)
- $widthNew = [int]($imgCurrent.Width * $ratioSelected)
- $heightNew = [int]($imgCurrent.Height * $ratioSelected)
- $_.Graphics.DrawImage($imgCurrent, ($_.MarginBounds.Left + ($_.MarginBounds.Width - $widthNew) / 2), ($_.MarginBounds.Top + ($_.MarginBounds.Height - $heightNew) / 2), $widthNew, $heightNew)
- $imgCurrent.Dispose()
- $listImg.RemoveAt(0)
- If ($listImg.Count -gt 0) { $_.HasMorePages = $true } Else { $_.HasMorePages = $false }
- })
-
- $docPrint.Print()
- $docPrint.Dispose()
- }
-
- $files = Get-ChildItem -Filter "*.jpg"
- $groups = @{}
-
- ForEach ($file In $files) {
- $partChinese = [regex]::Match($file.Name, '^[\u4e00-\u9fff]+').Value
- If ($partChinese) {
- If (-not $groups.ContainsKey($partChinese)) {
- $groups[$partChinese] = @()
- }
- $groups[$partChinese] += $file
- }
- }
-
- ForEach ($group In $groups.Keys) {
- Write-Output "分组:$group"
- Img2Pdf -paramImgGroup $group
- }
复制代码
|