【问题标题】:Powershell and iTextsharp add multiple images to PDFPowershell 和 iTextsharp 将多个图像添加到 PDF
【发布时间】:2014-06-27 21:49:36
【问题描述】:

我正在尝试在 Powershell 中使用 itextsharp(如果这是更好的选择,可以更改为 pdfsharp)从图像中制作 PDF。我已经设法用一张图片创建了一个 PDF 文件,但我不知道如何从文件夹中的所有图片中创建一个。

由于这些图像的比例非常适合 PDF,因此我还想设置比例以使其 100% 填满页面。那可能吗?

我不是经验丰富的 Powershell 用户,但这是我目前所得到的:

[System.Reflection.Assembly]::LoadFrom("C:\temp\itextsharp.dll")

$doc = New-Object iTextSharp.text.Document
$fileStream = New-Object IO.FileStream("C:\temp\output5.pdf", [System.IO.FileMode]::Create)
[iTextSharp.text.pdf.PdfWriter]::GetInstance($doc, $filestream)

$jpg = [iTextSharp.text.Image]::GetInstance( "c:\temp\horse.jpg" )
$doc.open()
$Doc.add($jpg);
$doc.close()

如果有人知道,请告诉我,谢谢。

【问题讨论】:

    标签: powershell pdf itextsharp


    【解决方案1】:

    您需要使用Get-ChildItem 来获取给定文件夹中的所有图像。然后,您需要在调用$doc.Add() 的图像上使用ForEach-Object(有时缩写为foreach),但在调用$doc.NewPage() 之前。

    下面的代码显示了这一切。一个常见的要求是每个页面的大小也要适合图像,所以我也添加了这一点。我们为每个图像实例化一个System.Drawing.Bitmap 以获取尺寸,使用这些尺寸创建一个iTextSharp Rectangle,然后使用它通过$doc.SetPageSize() 设置页面大小。

    我将大部分变量移到顶部只是为了让事情变得更容易,您需要更新它们以满足您的需求。 cmets 应该有望帮助您完成剩下的工作。

    ## Set various paths
    $iTextSharpFilePath = "D:\DLLs\itextsharp.dll"
    $imageFolderPath    = "D:\images"
    $pdfFilePath        = "D:\temp.pdf"
    
    ## Load iTextSharp and System.Drawing
    [System.Reflection.Assembly]::LoadFrom($iTextSharpFilePath)
    [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
    
    ## Get all of the images in the folder
    ## Change the filter if needed
    $images = Get-ChildItem $imageFolderPath -Filter *.png
    
    ## Create our stream, document and bind a writer
    $fileStream = New-Object System.IO.FileStream($pdfFilePath, [System.IO.FileMode]::Create)
    $doc = New-Object iTextSharp.text.Document
    $writer = [iTextSharp.text.pdf.PdfWriter]::GetInstance($doc, $filestream)
    
    ## Open the document for writing
    $doc.Open()
    
    ## Remove all document margins
    $doc.SetMargins(0, 0, 0, 0)
    
    ## Loop through each image in the folder
    foreach($image in $images)
    {
        ## Create a .Net image so that we can get the image dimensions
        $bmp = New-Object System.Drawing.Bitmap($image.FullName)
    
        ## Create an iTextSharp rectangle that corresponds to those dimensions
        $rect = New-Object iTextSharp.text.Rectangle($bmp.Width, $bmp.Height)
    
        ## Set the next page size to those dimensions and add a new page
        $doc.SetPageSize( $rect )
        $doc.NewPage()
    
        ## Add our image to the page
        $doc.Add([iTextSharp.text.Image]::GetInstance( $image.FullName ));
    
        ## Cleanup
        $bmp.Dispose()
    }
    
    ## Cleanup
    $doc.Close()
    $doc.Dispose()
    $writer.Dispose()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-10
      • 2016-02-12
      • 1970-01-01
      • 2011-05-18
      • 2015-04-02
      • 1970-01-01
      • 2011-08-27
      相关资源
      最近更新 更多