Why am I getting this format-default error in Powershell code using PdfSharp?

57 views Asked by At

I'm writing a simple program using PdfSharp and PowerShell. I have no experience with either PowerShell or PdfSharp so forgive me if the solution seems obvious.

I am essentially trying to copy pages from one pdf file into a new pdf file. However, I can't seem to get rid of this error:

format-default: Unable to cast object of type 'PdfSharp.Pdf.PdfInteger' to type 'System.IFormattable'.

Here is the part of the code that's causing the issue, I've added a comment next to the line that causes the error. It has something to do with the way I'm adding pages to the new pdf file.

Get-ChildItem $path | ForEach-Object {
    if ($_.PSIsContainer -eq $false) {
        
        $pdf = [PdfSharp.Pdf.IO.PdfReader]::Open($_.FullName, [PdfSharp.Pdf.IO.PdfDocumentOpenMode]::Import)

        for ($i = 0; $i -lt $pdf.PageCount; $i++)
        {
            $page = $pdf.Pages[$i]

            $metadata = Get-PieceInfo -page $page
            $documentName = [string]$metadata["/DocumentName"].ToString()
            $currentPage = [int]$metadata["/CurrentPage"].Value
            $archival = [bool]$metadata["/Archival"]
            $totalPages = [int]$metadata["/TotalPages"].Value
            
            Write-Host "Found document $documentName, $totalPages $currentPage"


            if ($archival) {
                if (-not $documents.ContainsKey($documentName)) {
                    $documents[$documentName] = New-Object PdfSharp.Pdf.PdfDocument
                }
                $documents[$documentName].AddPage($page)  # This line causes the error!              
            }
        }
    }
}  

If you have any idea of what this error is trying to tell me please let me know. I can't seem to get rid of it!

0

There are 0 answers