0

I'm trying to recode this to PowerShell v2 but when I try to insert a TIFF creating a iTextSharp.text.pdf.PdfReader using a memorystream as argument I get overload error:

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

I'm using itextsharp 5.5.9

Here is my code:

[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();

I don't know why I'm getting this error because PdfReader constructor supports it (also it's used in the original code)

Also tried using PoSh v2 and v3, x86 & x64...

Thanks!

Community
  • 1
  • 1
SalvaG
  • 3
  • 3
  • The error tells you that you are providing 18270 parameters while constructing the `PdfReader` instance. This is not an iText problem. It's a PowerShell problem. Use `PdfReader` with a single parameter. – Bruno Lowagie May 08 '16 at 06:24
  • Hi Bruno, as you can see at https://msdn.microsoft.com/en-us/library/system.io.memorystream.toarray(v=vs.110).aspx, 'toArray' method returns a byte array, but the constructor is not getting it as an array, as you said. All samples of pdf reader using powershell use it using file as source, any of them using byte array. Perhaps it's a posh incompatibility... – SalvaG May 08 '16 at 06:41
  • As I said: I don't know PowerShell, but I find it hard to believe that someone would create a language where you sometimes would use `New-Object iTextSharp.text.pdf.PdfReader $file` (no parentheses; no semi-colon) and sometimes `New-Object iTextSharp.text.pdf.PdfReader($m.ToArray());` (parentheses and semi-colon). That looks like very bad coding discipline. – Bruno Lowagie May 08 '16 at 07:25
  • Hi again. This code is only a sample;it's true that is not a well-done code, but powershell supports it. You can use parentheses (or not) and semicolon (or not). Anyway, I have tried what you told before and it didn't work. Also, I've tried now to create a new byte array {$byte = [Byte[]] (,0xFF * 100)} and I get the same error :{"New-Object : Cannot find an overload for "PdfReader" and the argument count: "100"."}. – SalvaG May 08 '16 at 08:11
  • You are confirming what I said earlier: you are not passing the `byte[]` as a single parameter, you are passing it as 100 (or 18270) separate parameters. **It should be evident that this can never work!** You need to pass the complete `byte[]` as a single parameter. – Bruno Lowagie May 08 '16 at 08:25
  • I have updated my answer to prove that I am right. – Bruno Lowagie May 08 '16 at 08:28
  • I agree with you that pdfreader is not receiving the byte array but I'm casting it to byte[] and also received the same message. Added to code { $byte = [Byte[]]($m.ToArray()); $byte.GetType() } Response to 'gettype' :{ Name: Byte[] BaseType: System.Array } – SalvaG May 08 '16 at 08:34
  • You should read the answer: http://stackoverflow.com/questions/12870109/how-do-i-call-new-object-for-a-constructor-which-takes-a-single-array-parameter I have closed the question because I am 100% sure that your question is a duplicate! The solution to your problem is also explained here: http://www.justaprogrammer.net/2011/03/30/an-array-of-one-item-in-powershell – Bruno Lowagie May 08 '16 at 08:37
  • I didn't see your answer edition and your comment about this edition, sorry :( You are right: it's a Posh trick! Thanks – SalvaG May 08 '16 at 08:52

1 Answers1

0

The error tells you that you are providing 18270 parameters while constructing the PdfReader instance. This is not an iText problem. It's a PowerShell problem. Use PdfReader with a single parameter.

I don’t know PowerShell, but I think that this is wrong:

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

This is the only place in your code snippet where you might pass 18270 parameters to PdfReader (18270 separate bytes).

Create a byte[] object from $m first and pass that object as the single parameter when constructing the PdfReader instance.

Read the answer to How do I call New-Object for a constructor which takes a single array parameter? to find out how this is done.

I repeat: I don't know PowerShell, but the error message you mention is very clear and the answer is obvious. See also: Using New-Object -ArgumentList when the constructor takes one parameter that is an array

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165