2016-05-06 40 views
0

我试图重新编码this到的PowerShell v2的,但是当我尝试插入TIFF使用一个MemoryStream作为参数创建iTextSharp.text.pdf.PdfReader我得到过载错误:iTextSharp的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支持的话,为什么我得到这个错误(也很在原来的代码中使用)

使用豪华v2和v3也试过,86 & 64 ...

谢谢!

+0

错误提示您在构建“PdfReader”实例时提供了18270个参数。这不是iText问题。这是一个PowerShell问题。使用带有单个参数的'PdfReader'。 –

+0

嗨布鲁诺,你可以在https://msdn.microsoft.com/en-us/library/system.io.memorystream.toarray(v=vs.110).aspx看到,'toArray'方法返回一个字节数组,但是正如你所说,构造函数并不像数组那样获得它。使用PowerShell的PDF阅读器的所有示例都使用文件作为源,它们中的任何一个都使用字节数组。也许这是一种豪华的不兼容...... – SalvaG

+0

正如我所说:我不知道PowerShell,但我觉得很难相信有人会创建一种语言,你有时会使用'New-Object iTextSharp.text.pdf.PdfReader $ (无括号;无分号),有时还有New-Object iTextSharp.text.pdf.PdfReader($ m.ToArray());(括号和分号)。这看起来非常糟糕的编码规范。 –

回答

0

错误提示您在构建PdfReader实例时提供18270个参数。这不是iText问题。这是一个PowerShell问题。用一个参数使用PdfReader。

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

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

这是您的代码段的唯一的地方,你可以通过18270个参数PdfReader(18270个单独字节)。

首先创建byte[]对象$m,并在构造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