0

I'm trying to get the script below working. The problem I keep running into is the Object being returned is a SystemObject[] instead of a PdfSharp.Pdf.PdfDocument.

I wrote the below script with the idea of using this in a larger script by setting a $out = New-Object PdfSharp.Pdf.PdfDocument and capturing the return. Unfortunately, this did not work.

After googling for several hours, and finding nothing, I am open to suggestions.

Add-Type -Path C:\assemblies\GDI+\PdfSharp.dll

Function mergePdf {
    Param($file1, $file2)

    $output = New-Object PdfSharp.Pdf.PdfDocument
    $PdfReader = [PdfSharp.PDF.IO.PdfReader]
    $PdfDocumentOpenMode = [PdfSharp.Pdf.IO.PdfDocumentOpenMode]


    $input = New-Object PdfSharp.Pdf.PdfDocument
    $input = $PdfReader::Open($file1, $PdfDocumentOpenMode::Import)
    $input.Pages | %{$output.AddPage($_)}
    $input = $PdfReader::Open($file2, $PdfDocumentOpenMode::Import)
    $input.Pages | %{$output.AddPage($_)}

    return $output
}

Edit: I changed $output.pages to the previous $output for clarity, but the script still fails to pass a PdfSharp.Pdf.Pdfdocument object back from the function correctly.

$obj = mergePdf temp1.pdf temp2.pdf
$obj.Save("./merged/temp.pdf")
Method invocation failed because [System.Object[]] doesn't contain a method named 'Save'
.  At line:1 char:10
+ $obj.Save <<<< ("./merged/temp.pdf")
+ CategoryInfo          : InvalidOperation: (Save:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound

And yet I get:

$obj | gm
TypeName: PdfSharp.Pdf.PdfDocument

Name              MemberType Definition
----              ---------- ----------
AddPage           Method     PdfSharp.Pdf.PdfPage AddPage(), PdfSharp.Pdf.PdfPage Add...
...
Save              Method     System.Void Save(string path), System.Void Save(System.I...
5
  • 1
    If you want to return a PdfDocument, shouldn't you return $output instead of $output.Pages? Pages would appear to be an array which is why you get Object[]. Commented Dec 5, 2014 at 1:25
  • To clarify, I attempted returning several variables. the $output.pages was the last attempt where I tried wrapping. I'll edit for clarity. I had tried $output and it was still returning a system.object[] which did not contain any properties of the PdfDocument. Commented Dec 5, 2014 at 1:57
  • system.object[] on v2, seems I get a different error using v3 Commented Dec 5, 2014 at 2:24
  • Sorry, was transitioning between different computers with different powershell versions, the error is still there, you can disregard the previous comment. Anyways I'm at a loss for why its throwing that error. Not as knowledgeable as some people on this site regarding powershell or dotnet Commented Dec 5, 2014 at 2:37
  • Seems the error is related to a seperate issue. This will load the file correctly and return the object however you cannot pass the object back in and have it work. That was the error I was receiving. Apologies. I will start a new thread for the new issue. Commented Jan 8, 2015 at 21:41

1 Answer 1

1

I think you are outputting multiple different types via these lines.

$input.Pages | %{$output.AddPage($_)}
...
$input.Pages | %{$output.AddPage($_)}
...
return $output

Remember that any command/pipeline in a function that doesn't assign to a variable, output to a file or $null or cast to [void] will contribute to the output of the function. My guess is that $output.AddPage($_) is returning some object - perhaps the page that was added. Anyway, change those lines to:

$input.Pages | %{$output.AddPage($_)} > $null
...
$input.Pages | %{$output.AddPage($_)} > $null
...
$output

Also note that the return keyword is not required in this case. You only need to use the return keyword to bail out of a function early.

Sign up to request clarification or add additional context in comments.

3 Comments

That solved the problem. It works perfectly now. I'll have to use > $null from now on for objects that don't return variable. Thank you.
@Lorek use > $null for commands who's output you don't want.
I know its a little after the fact, that trick worked perfectly in version 3, however I am getting an error in version 2 of powershell which is primarily where it will be used. I have included it as an edit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.