如何通过PowerShell脚本实现无边距打印?
无边距打印PowerShell脚本问题解决
问题描述
我使用修改版PowerShell打印脚本打印图片,但始终无法实现无边距打印。已尝试将边距设为0、自定义纸张尺寸、向打印机发送超出允许尺寸的图片,均未奏效,请问如何启用无边距打印标志?
原脚本如下:
$imageName = 'C:\image.png' $printer = "Canon SELPHY CP1500" $fitImageToPaper = $true trap { break; } # 加载System.Drawing程序集 [void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") $bitmap = $null $doc = new-object System.Drawing.Printing.PrintDocument $doc.DefaultPageSettings.Margins.Left = 0 $doc.DefaultPageSettings.Margins.Right = 0 $doc.DefaultPageSettings.Margins.Top = 0 $doc.DefaultPageSettings.Margins.Bottom = 0 Write-Host $doc.DefaultPageSettings.PaperSize if ($printer -ne "") { $doc.PrinterSettings.PrinterName = $printer } $doc.DocumentName = [System.IO.Path]::GetFileName($imageName) $doc.add_BeginPrint({ Write-Host "==================== $($doc.DocumentName) ====================" }) $doc.add_EndPrint({ if ($bitmap -ne $null) { $bitmap.Dispose() $bitmap = $null } Write-Host "xxxxxxxxxxxxxxxxxxxx $($doc.DocumentName) xxxxxxxxxxxxxxxxxxxx" }) $doc.add_PrintPage({ Write-Host "Printing $imageName..." $g = $_.Graphics $pageBounds = $_.PageBounds $img = new-object Drawing.Bitmap($imageName) $adjustedImageSize = $img.Size $ratio = [double] 1; if ($fitImageToPaper) { $fitWidth = [bool] ($img.Size.Width > $img.Size.Height) if (($img.Size.Width -le $_.MarginBounds.Width) -and ($img.Size.Height -le $_.MarginBounds.Height)) { $adjustedImageSize = new-object System.Drawing.SizeF($img.Size.Width, $img.Size.Height) } else { if ($fitWidth) { $ratio = [double] ($_.MarginBounds.Width / $img.Size.Width); } else { $ratio = [double] ($_.MarginBounds.Height / $img.Size.Height) } $adjustedImageSize = new-object System.Drawing.SizeF($img.Size.Width, $img.Size.Height) } } $recDest = new-object Drawing.RectangleF($pageBounds.Location, $adjustedImageSize) $recSrc = new-object Drawing.RectangleF(0, 0, $img.Width, $img.Height) $_.Graphics.DrawImage($img, $recDest, $recSrc, [Drawing.GraphicsUnit]"Pixel") $_.HasMorePages = $false; }) $doc.Print()
解决方案
单纯设置边距为0无法触发打印机的无边距模式,需要同时配置打印机驱动的特定参数,并调整绘图区域到纸张物理边界。
修改后的脚本
$imageName = 'C:\image.png' $printer = "Canon SELPHY CP1500" $fitImageToPaper = $true trap { break; } # 加载必要程序集 [void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") $doc = New-Object System.Drawing.Printing.PrintDocument $doc.PrinterSettings.PrinterName = $printer # 启用无边距打印核心设置 if ($doc.PrinterSettings.SupportsCustomPageSettings) { # 强制设置四边边距为0 $doc.DefaultPageSettings.Margins = New-Object System.Drawing.Printing.Margins(0,0,0,0) # 同步打印机默认页设置的边距 $doc.PrinterSettings.DefaultPageSettings.Margins = New-Object System.Drawing.Printing.Margins(0,0,0,0) # 绑定打印对话框配置,确保驱动识别无边距模式 $printDialog = New-Object System.Windows.Forms.PrintDialog $printDialog.PrinterSettings = $doc.PrinterSettings $printDialog.PrinterSettings.DefaultPageSettings.Margins = New-Object System.Drawing.Printing.Margins(0,0,0,0) # 匹配打印机当前使用的纸张尺寸(无边距仅对特定尺寸生效) $doc.DefaultPageSettings.PaperSize = $doc.PrinterSettings.DefaultPageSettings.PaperSize } $doc.DocumentName = [System.IO.Path]::GetFileName($imageName) $doc.add_BeginPrint({ Write-Host "==================== $($doc.DocumentName) ====================" }) $doc.add_EndPrint({ Write-Host "xxxxxxxxxxxxxxxxxxxx $($doc.DocumentName) xxxxxxxxxxxxxxxxxxxx" }) $doc.add_PrintPage({ Write-Host "Printing $imageName..." $img = New-Object Drawing.Bitmap($imageName) $g = $_.Graphics # 使用纸张物理边界而非边距区域进行绘制 $pageBounds = $_.PageBounds # 计算缩放比例,让图片完全覆盖纸张 $scaleWidth = [double]$pageBounds.Width / $img.Width $scaleHeight = [double]$pageBounds.Height / $img.Height $scale = [Math]::Max($scaleWidth, $scaleHeight) $newWidth = $img.Width * $scale $newHeight = $img.Height * $scale # 居中绘制,确保填满整个纸张 $x = ($pageBounds.Width - $newWidth) / 2 $y = ($pageBounds.Height - $newHeight) / 2 $recDest = New-Object Drawing.RectangleF($x, $y, $newWidth, $newHeight) $recSrc = New-Object Drawing.RectangleF(0, 0, $img.Width, $img.Height) $g.DrawImage($img, $recDest, $recSrc, [Drawing.GraphicsUnit]"Pixel") $img.Dispose() $_.HasMorePages = $false }) $doc.Print()
关键注意事项
- 硬件支持确认:先确认Canon SELPHY CP1500本身支持无边距打印,该型号仅对特定尺寸相纸(如4×6英寸)生效。
- 驱动预配置:先在打印机属性的图形界面中手动开启一次无边距打印,部分驱动需要先确认后才能通过脚本生效。
- 纸张匹配:确保打印机内装的纸张尺寸与脚本中设置的一致,无边距模式不支持所有纸张尺寸。
内容的提问来源于stack exchange,提问作者Manolis
相关产品推荐
相关产品推荐

