【问题标题】:itextsharp PdfReader overload error [duplicate]itextsharp PdfReader 重载错误 [重复]
【发布时间】:2016-05-06 12:38:56
【问题描述】:

我正在尝试将 this 重新编码为 PowerShell v2,但是当我尝试插入 TIFF 并使用内存流作为参数创建 iTextSharp.text.pdf.PdfReader 时,出现过载错误:

"New-Object : Cannot find an overload for "PdfReader" and the argument count: "18270"."

我正在使用 itextsharp 5.5.9

这是我的代码:

[System.Reflection.Assembly]::LoadFrom(c:\temp\itextsharp.dll) | Out-Null
$List = gc C:\temp\filelist.txt
$Dest = "C:\destPDF.pdf"


$document = New-Object iTextSharp.text.Document([iTextSharp.text.PageSize]::A4, 0, 0, 0, 0)

$copy = New-Object iTextSharp.text.pdf.PdfCopy($document, (New-Object System.IO.FileStream $RutaDestino, 'Create'))

$document.Open();

foreach ($file in $List)
{
    $extension = (Get-Item $file).extension.toupper()
    switch ($extension)
    {

        ".PDF" {

            [iTextSharp.text.pdf.PdfReader] $reader = New-Object  iTextSharp.text.pdf.PdfReader $file

            $reader.ConsolidateNamedDestinations()
            for ($i = 1; $i -le $reader.NumberOfPages; $i++)
            {
                [iTextSharp.text.pdf.PdfImportedPage] $page = $copy.GetImportedPage($reader, $i)
                $copy.addpage($page)
            }

            $reader.Close()
        }

        ".TIF" {

            [iTextSharp.text.Rectangle] $pageSize = $null;
            [System.Drawing.Bitmap] $bm = New-Object System.Drawing.Bitmap($file)
            $pageSize = New-Object iTextSharp.text.Rectangle(0, 0, $bm.Width, $bm.Height);
            $m = New-Object System.IO.MemoryStream
            $d = New-Object iTextSharp.text.Document($pageSize, 0, 0, 0, 0)
            $w = [iTextSharp.text.pdf.PdfWriter]::GetInstance($d, $m)
            $d.Open();
            $d.Add([iTextSharp.text.Image]::GetInstance($file));
            $d.Close();

            $r = New-Object iTextSharp.text.pdf.PdfReader($m.ToArray());
            $copy.AddDocument($r);
        }       
    }
}

$document.Close();

我不知道为什么会出现这个错误,因为PdfReader constructor 支持它(在原始代码中也使用它)

还尝试使用 PoSh v2 和 v3、x86 和 x64...

谢谢!

【问题讨论】:

  • 错误告诉你在构造PdfReader实例时提供了18270个参数。这不是 iText 问题。这是一个 PowerShell 问题。将PdfReader 与单个参数一起使用。
  • 嗨 Bruno,正如您在 msdn.microsoft.com/en-us/library/… 看到的那样,'toArray' 方法返回一个字节数组,但正如您所说,构造函数没有将它作为数组获取。所有使用 powershell 的 pdf 阅读器示例都使用文件作为源,其中任何一个都使用字节数组。也许这是一个豪华的不兼容......
  • 正如我所说:我不了解 PowerShell,但我很难相信有人会创建一种您有时会使用 New-Object iTextSharp.text.pdf.PdfReader $file 的语言(没有括号;没有分号)和有时New-Object iTextSharp.text.pdf.PdfReader($m.ToArray());(括号和分号)。这看起来像是非常糟糕的编码纪律。
  • 你好。这段代码只是一个示例;确实不是一个完善的代码,但 powershell 支持它。您可以使用括号(或不使用)和分号(或不使用)。无论如何,我已经尝试过你之前所说的,它没有奏效。另外,我现在尝试创建一个新的字节数组 {$byte = [Byte[]] (,0xFF * 100)} 并且我得到了同样的错误:{“New-Object : Cannot find an overload for "PdfReader"和参数计数:"100"."}.
  • 您正在确认我之前所说的:您没有将 byte[] 作为单个参数传递,而是将其作为 100(或 18270)个单独的参数传递。 很明显,这永远行不通!您需要将完整的byte[] 作为单个参数传递。

标签: powershell pdf itextsharp powershell-2.0


【解决方案1】:

错误告诉您在构造 PdfReader 实例时提供了 18270 个参数。这不是 iText 问题。这是一个 PowerShell 问题。使用带有单个参数的 PdfReader。

我不知道 PowerShell,但我认为这是错误的:

$r = New-Object iTextSharp.text.pdf.PdfReader($m.ToArray());

这是您的代码 sn-p 中唯一可以将 18270 个参数传递给 PdfReader(18270 个单独字节)的地方。

首先从$m 创建一个byte[] 对象,并在构造PdfReader 实例时将该对象作为单个参数传递。

阅读How do I call New-Object for a constructor which takes a single array parameter? 的答案,了解如何做到这一点。

我再说一遍:我不懂PowerShell,但是你提到的错误信息很清楚,答案也很明显。另见:Using New-Object -ArgumentList when the constructor takes one parameter that is an array

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-23
    • 2015-01-02
    相关资源
    最近更新 更多