Powershell ISE to Include Sub Class from DLL

I am trying to create PDFs with header and footer using Powershell. I am using PowerShell v5.1. My PowerShell experience is pretty rudimentary as I usually do coding in C#/VB. Unfortunately, one of the requirements is to do this in PowerShell. The library I am using is iTextSharp library.

I am including iTextSharp.dll in my script

Add-Type -Path "$ScriptLocationIncludesitextsharp.dll" -ErrorAction Stop

I need to inherit the iTextSharp.text.pdf.PdfPageEventHelper class to override the OnEndPage function. Everytime I load the script into PowerShell ISE environment, I get this error: Unable to find the type [iTextSharp.text.pdf.PdfPageEventHelper]. However, the ‘missing’ type is part of the itextsharp.dll.

I am able to get the environment to recognize the type after the following steps:

  1. Comment out the inherited class and the command
  2. Run the script
  3. Uncomment the inherit class and function

In VB.NET, it has the imports statement, sometime like this:

Imports iTextSharp.text

Is there something similar in Powershell? How can I force the PowerShell environment to recongnize the sub class (iTextSharp.text) without going through those steps I outlinged above?

My code is as follows:

Add-Type -Path "$ScriptLocationIncludesitextsharp.dll" -ErrorAction Stop

    class PdfPageEvents : iTextSharp.text.pdf.PdfPageEventHelper {
        [string]$headerText
    
        PdfPageEvents([string]$headerText) {
            $this.headerText = $headerText
        }
    
        
        # Override OnEndPage to add header and footer
        [void] OnEndPage ([iTextSharp.text.pdf.PdfWriter]$writer, [iTextSharp.text.Document]$document) {
            $cb = $writer.DirectContent
            $pageSize = $document.PageSize
    
            # Add header
            $header = New-Object iTextSharp.text.Paragraph($this.headerText, New-Object iTextSharp.text.Font([iTextSharp.text.Font]::ARIAL, 12))
            $header.Alignment = [iTextSharp.text.Element]::ALIGN_CENTER
            $headerTable = New-Object iTextSharp.text.pdf.PdfPTable(1)
            $headerTable.TotalWidth = $pageSize.Width - $document.LeftMargin - $document.RightMargin
            $headerTable.AddCell($header)
            $headerTable.WriteSelectedRows(0, -1, $document.LeftMargin, $pageSize.Height - $document.TopMargin + 15, $cb)
   
        }
    }
    
    $pdfDocument = New-Object iTextSharp.text.Document([iTextSharp.text.Rectangle]::new([iTextSharp.text.PageSize]::A4.Height, [iTextSharp.text.PageSize]::A4.Width))
                
    $fileStream = New-Object -TypeName IO.FileStream -ArgumentList ($FileOutputName, [System.IO.FileMode]::Create)
    $pdfWriter = [iTextSharp.text.pdf.PdfWriter]::GetInstance($pdfDocument, $filestream)
    [iTextSharp.text.FontFactory]::RegisterDirectories()
    $pdfDocument.Open()
    $paragraph = New-Object -TypeName iTextSharp.text.Paragraph
    
    $headerString = "Header Text"
    $pdfWriter.PageEvent = [PdfPageEvents]::new("$headerString")
    $paragraph.add("blah blah blah")
    $pdfDocument.Add($paragraph)
    $pdfDocument.Close()

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật