任何人都可以提供将JPEG转换为PDF的最简单方法的样本?
您要做的就是打印到PDF虚拟打印机。有许多这样的打印机。
您可以在Windows上执行此操作,而无需任何第三方可执行文件或库。
function ConvertToPdf($files, $outFile) {
Add-Type -AssemblyName System.Drawing
$files = @($files)
if (!$outFile) {
$firstFile = $files[0]
if ($firstFile.FullName) { $firstFile = $firstFile.FullName }
$outFile = $firstFile.Substring(0, $firstFile.LastIndexOf(".")) + ".pdf"
} else {
if (![System.IO.Path]::IsPathRooted($outFile)) {
$outFile = [System.IO.Path]::Combine((Get-Location).Path, $outFile)
}
}
try {
$doc = [System.Drawing.Printing.PrintDocument]::new()
$opt = $doc.PrinterSettings = [System.Drawing.Printing.PrinterSettings]::new()
$opt.PrinterName = "Microsoft Print to PDF"
$opt.PrintToFile = $true
$opt.PrintFileName = $outFile
$script:_pageIndex = 0
$doc.add_PrintPage({
param($sender, [System.Drawing.Printing.PrintPageEventArgs] $a)
$file = $files[$script:_pageIndex]
if ($file.FullName) {
$file = $file.FullName
}
$script:_pageIndex = $script:_pageIndex + 1
try {
$image = [System.Drawing.Image]::FromFile($file)
$a.Graphics.DrawImage($image, $a.PageBounds)
$a.HasMorePages = $script:_pageIndex -lt $files.Count
}
finally {
$image.Dispose()
}
})
$doc.PrintController = [System.Drawing.Printing.StandardPrintController]::new()
$doc.Print()
return $outFile
}
finally {
if ($doc) { $doc.Dispose() }
}
}
示例用法:
ConvertToPdf (gi *.jpeg| sort Name) out.pdf
q
任何人都可以提供将JPEG转换为PDF的最简单方法的样本?
a
第一个正确的答案是从https://stackoverflow.com/a/a/41068017/10802527上方的User3344003简要介绍,但是并未解释Windows通过" Microsoft"具有" Microsoft"的内置功能,可以" PDF"。
这可以在没有命令行的无powershell的情况下使用,但默认情况下,需要手动响应来指定文件名。可以通过多种方式将printing file.txt to file.pdf轻松配置在打印UI中。(但是在某些情况下,目标文件可能尚不存在。)
对PDF的图像并不那么简单,因此没有PowerShell,我们可以通过
进行打印mspaint /pt image.jpg "Microsoft Print to PDF"
,但再次需要解决方案:问题请参阅https://stackoverflow.com/a/71225924/10802527
因此,有或没有PowerShell,我们只需要运行具有默认表单大小的给定端口名称,在这里我将其设置为A4landscape的景观输入
mspaint /pt Landscape.jpg A4LPdf
File: C:UsersMeDocumentsprintout.pdf
Title: Untitled.jpg
Author: Me
Created: 2022-06-26 02:52:33
Modified: 2022-06-26 02:52:33
PDF Producer: Microsoft: Print To PDF
PDF Version: 1.7
File Size: 7.45 KB (7,632 Bytes)
Number of Pages: 1
Page Size: 21.0 x 29.7 cm (A4)
Eagle Eyed会发现,尽管我指定了景观,但输出是肖像,这是盲目打印到PDF的核心问题,但没有用户选择,就没有细节或方向控制。
应将每个任务视为结束游戏。因此,如果想要一个好的PDF,最好将图像导入PDF应用程序以按数字进行转换。
因此,我建议这个问题,因此答案应沿着
的线q我可以使用mutool.exe转换为pdf?
以受控的方式保存图像吗?
是。