向 itextsharp 文档对象添加方法

Adding method to itextsharp document object

背景是我正在为 PowerShell 流程(解释器,如果你愿意的话)开发一组包装器,以增加简单性,并且 - 最重要的是 - 标准化我的同事所做的报告。

我用来试验的片段如下:

Add-Type -Path ".\itextsharp.dll"

function CreateReport([string]$Path = ".\Report.pdf")
{
    #Create Document, define basic properties
    $Document = New-Object iTextSharp.text.Document
    $Document.SetPageSize([iTextSharp.text.PageSize]::A4)
    $Document.SetMargins(5,5,5,5)
    $Document.AddAuthor($env:USERNAME)

    #Define methods
    Add-Member -in $Document scriptmethod AddParagraph {
        param([string]$Text = "Empty paragraph")
        $Paragraph = New-Object iTextSharp.text.Paragraph
        $Paragraph.Add($Text)
        $this.Add($Paragraph)
    }
    return $Document
}

(我很清楚参数$Path没有做任何事情,这是为了以后使用)

根据我的理解,这应该有效,但实际上无效。最糟糕的是,我无法弄清楚 为什么 。我测试如下:

$TestDoc = CreateReport
$TestDoc.AddParagraph("asdf")

它抛出以下异常:

Method invocation failed because [System.Boolean] does not contain a method named 'AddParagraph'.
At line:1 char:1
+ $TestDoc.AddParagraph("asdf")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

为什么 PowerShell 认为我正在尝试调用布尔值中的方法? recognize/support 不是 iTextSharp.text.Document 对象类型吗?


根据要求,这是 CreateReport 的成员列表,我的函数:

PS C:\temp\Robin> CreateReport | Get-Member


   TypeName: System.Boolean

Name        MemberType Definition                                                                                                                                        
----        ---------- ----------                                                                                                                                        
CompareTo   Method     int CompareTo(System.Object obj), int CompareTo(bool value), int IComparable.CompareTo(System.Object obj), int IComparable[bool].CompareTo(bool...
Equals      Method     bool Equals(System.Object obj), bool Equals(bool obj), bool IEquatable[bool].Equals(bool other)                                                   
GetHashCode Method     int GetHashCode()                                                                                                                                 
GetType     Method     type GetType()                                                                                                                                    
GetTypeCode Method     System.TypeCode GetTypeCode(), System.TypeCode IConvertible.GetTypeCode()                                                                         
ToBoolean   Method     bool IConvertible.ToBoolean(System.IFormatProvider provider)                                                                                      
ToByte      Method     byte IConvertible.ToByte(System.IFormatProvider provider)                                                                                         
ToChar      Method     char IConvertible.ToChar(System.IFormatProvider provider)                                                                                         
ToDateTime  Method     datetime IConvertible.ToDateTime(System.IFormatProvider provider)                                                                                 
ToDecimal   Method     decimal IConvertible.ToDecimal(System.IFormatProvider provider)                                                                                   
ToDouble    Method     double IConvertible.ToDouble(System.IFormatProvider provider)                                                                                     
ToInt16     Method     int16 IConvertible.ToInt16(System.IFormatProvider provider)                                                                                       
ToInt32     Method     int IConvertible.ToInt32(System.IFormatProvider provider)                                                                                         
ToInt64     Method     long IConvertible.ToInt64(System.IFormatProvider provider)                                                                                        
ToSByte     Method     sbyte IConvertible.ToSByte(System.IFormatProvider provider)                                                                                       
ToSingle    Method     float IConvertible.ToSingle(System.IFormatProvider provider)                                                                                      
ToString    Method     string ToString(), string ToString(System.IFormatProvider provider), string IConvertible.ToString(System.IFormatProvider provider)                
ToType      Method     System.Object IConvertible.ToType(type conversionType, System.IFormatProvider provider)                                                           
ToUInt16    Method     uint16 IConvertible.ToUInt16(System.IFormatProvider provider)                                                                                     
ToUInt32    Method     uint32 IConvertible.ToUInt32(System.IFormatProvider provider)                                                                                     
ToUInt64    Method     uint64 IConvertible.ToUInt64(System.IFormatProvider provider)                                                                                     


   TypeName: iTextSharp.text.Document

Name                        MemberType   Definition                                                                                                                      
----                        ----------   ----------                                                                                                                      
Add                         Method       bool Add(iTextSharp.text.IElement element), bool IElementListener.Add(iTextSharp.text.IElement element)                         
AddAuthor                   Method       bool AddAuthor(string author)                                                                                                   
AddCreationDate             Method       bool AddCreationDate()                                                                                                          
AddCreator                  Method       bool AddCreator(string creator)                                                                                                 
AddDocListener              Method       void AddDocListener(iTextSharp.text.IDocListener listener)                                                                      
AddHeader                   Method       bool AddHeader(string name, string content)                                                                                     
AddKeywords                 Method       bool AddKeywords(string keywords)                                                                                               
AddLanguage                 Method       bool AddLanguage(string language)                                                                                               
AddProducer                 Method       bool AddProducer()                                                                                                              
AddSubject                  Method       bool AddSubject(string subject)                                                                                                 
AddTitle                    Method       bool AddTitle(string title)                                                                                                     
Close                       Method       void Close(), void IDocListener.Close()                                                                                         
CloseDocument               Method       void CloseDocument()                                                                                                            
Dispose                     Method       void Dispose(), void IDisposable.Dispose()                                                                                      
Equals                      Method       bool Equals(System.Object obj)                                                                                                  
GetAccessibleAttribute      Method       iTextSharp.text.pdf.PdfObject GetAccessibleAttribute(iTextSharp.text.pdf.PdfName key), iTextSharp.text.pdf.PdfObject IAccessi...
GetAccessibleAttributes     Method       System.Collections.Generic.Dictionary[iTextSharp.text.pdf.PdfName,iTextSharp.text.pdf.PdfObject] GetAccessibleAttributes(), S...
GetBottom                   Method       float GetBottom(float margin)                                                                                                   
GetHashCode                 Method       int GetHashCode()                                                                                                               
GetLeft                     Method       float GetLeft(float margin)                                                                                                     
GetRight                    Method       float GetRight(float margin)                                                                                                    
GetTop                      Method       float GetTop(float margin)                                                                                                      
GetType                     Method       type GetType()                                                                                                                  
IsMarginMirroring           Method       bool IsMarginMirroring()                                                                                                        
IsOpen                      Method       bool IsOpen()                                                                                                                   
NewPage                     Method       bool NewPage(), bool IDocListener.NewPage()                                                                                     
Open                        Method       void Open(), void IDocListener.Open()                                                                                           
OpenDocument                Method       void OpenDocument()                                                                                                             
RemoveIDocListener          Method       void RemoveIDocListener(iTextSharp.text.IDocListener listener)                                                                  
ResetPageCount              Method       void ResetPageCount(), void IDocListener.ResetPageCount()                                                                       
SetAccessibleAttribute      Method       void SetAccessibleAttribute(iTextSharp.text.pdf.PdfName key, iTextSharp.text.pdf.PdfObject value), void IAccessibleElement.Se...
SetMarginMirroring          Method       bool SetMarginMirroring(bool marginMirroring), bool IDocListener.SetMarginMirroring(bool marginMirroring)                       
SetMarginMirroringTopBottom Method       bool SetMarginMirroringTopBottom(bool marginMirroringTopBottom), bool IDocListener.SetMarginMirroringTopBottom(bool marginMir...
SetMargins                  Method       bool SetMargins(float marginLeft, float marginRight, float marginTop, float marginBottom), bool IDocListener.SetMargins(float...
SetPageSize                 Method       bool SetPageSize(iTextSharp.text.Rectangle pageSize), bool IDocListener.SetPageSize(iTextSharp.text.Rectangle pageSize)         
ToString                    Method       string ToString()                                                                                                               
Bottom                      Property     float Bottom {get;}                                                                                                             
BottomMargin                Property     float BottomMargin {get;}                                                                                                       
HtmlStyleClass              Property     string HtmlStyleClass {get;set;}                                                                                                
ID                          Property     iTextSharp.text.AccessibleElementId ID {get;set;}                                                                               
IsInline                    Property     bool IsInline {get;}                                                                                                            
JavaScript_onLoad           Property     string JavaScript_onLoad {get;set;}                                                                                             
JavaScript_onUnLoad         Property     string JavaScript_onUnLoad {get;set;}                                                                                           
Left                        Property     float Left {get;}                                                                                                               
LeftMargin                  Property     float LeftMargin {get;}                                                                                                         
PageCount                   Property     int PageCount {set;}                                                                                                            
PageNumber                  Property     int PageNumber {get;}                                                                                                           
PageSize                    Property     iTextSharp.text.Rectangle PageSize {get;}                                                                                       
Right                       Property     float Right {get;}                                                                                                              
RightMargin                 Property     float RightMargin {get;}                                                                                                        
Role                        Property     iTextSharp.text.pdf.PdfName Role {get;set;}                                                                                     
Top                         Property     float Top {get;}                                                                                                                
TopMargin                   Property     float TopMargin {get;}                                                                                                          
AddParagraph                ScriptMethod System.Object AddParagraph();                                                                                                   

下面是 $Document 的成员:

   TypeName: iTextSharp.text.Document

Name                        MemberType Definition                                                                                                                        
----                        ---------- ----------                                                                                                                        
Add                         Method     bool Add(iTextSharp.text.IElement element), bool IElementListener.Add(iTextSharp.text.IElement element)                           
AddAuthor                   Method     bool AddAuthor(string author)                                                                                                     
AddCreationDate             Method     bool AddCreationDate()                                                                                                            
AddCreator                  Method     bool AddCreator(string creator)                                                                                                   
AddDocListener              Method     void AddDocListener(iTextSharp.text.IDocListener listener)                                                                        
AddHeader                   Method     bool AddHeader(string name, string content)                                                                                       
AddKeywords                 Method     bool AddKeywords(string keywords)                                                                                                 
AddLanguage                 Method     bool AddLanguage(string language)                                                                                                 
AddProducer                 Method     bool AddProducer()                                                                                                                
AddSubject                  Method     bool AddSubject(string subject)                                                                                                   
AddTitle                    Method     bool AddTitle(string title)                                                                                                       
Close                       Method     void Close(), void IDocListener.Close()                                                                                           
CloseDocument               Method     void CloseDocument()                                                                                                              
Dispose                     Method     void Dispose(), void IDisposable.Dispose()                                                                                        
Equals                      Method     bool Equals(System.Object obj)                                                                                                    
GetAccessibleAttribute      Method     iTextSharp.text.pdf.PdfObject GetAccessibleAttribute(iTextSharp.text.pdf.PdfName key), iTextSharp.text.pdf.PdfObject IAccessibl...
GetAccessibleAttributes     Method     System.Collections.Generic.Dictionary[iTextSharp.text.pdf.PdfName,iTextSharp.text.pdf.PdfObject] GetAccessibleAttributes(), Sys...
GetBottom                   Method     float GetBottom(float margin)                                                                                                     
GetHashCode                 Method     int GetHashCode()                                                                                                                 
GetLeft                     Method     float GetLeft(float margin)                                                                                                       
GetRight                    Method     float GetRight(float margin)                                                                                                      
GetTop                      Method     float GetTop(float margin)                                                                                                        
GetType                     Method     type GetType()                                                                                                                    
IsMarginMirroring           Method     bool IsMarginMirroring()                                                                                                          
IsOpen                      Method     bool IsOpen()                                                                                                                     
NewPage                     Method     bool NewPage(), bool IDocListener.NewPage()                                                                                       
Open                        Method     void Open(), void IDocListener.Open()                                                                                             
OpenDocument                Method     void OpenDocument()                                                                                                               
RemoveIDocListener          Method     void RemoveIDocListener(iTextSharp.text.IDocListener listener)                                                                    
ResetPageCount              Method     void ResetPageCount(), void IDocListener.ResetPageCount()                                                                         
SetAccessibleAttribute      Method     void SetAccessibleAttribute(iTextSharp.text.pdf.PdfName key, iTextSharp.text.pdf.PdfObject value), void IAccessibleElement.SetA...
SetMarginMirroring          Method     bool SetMarginMirroring(bool marginMirroring), bool IDocListener.SetMarginMirroring(bool marginMirroring)                         
SetMarginMirroringTopBottom Method     bool SetMarginMirroringTopBottom(bool marginMirroringTopBottom), bool IDocListener.SetMarginMirroringTopBottom(bool marginMirro...
SetMargins                  Method     bool SetMargins(float marginLeft, float marginRight, float marginTop, float marginBottom), bool IDocListener.SetMargins(float m...
SetPageSize                 Method     bool SetPageSize(iTextSharp.text.Rectangle pageSize), bool IDocListener.SetPageSize(iTextSharp.text.Rectangle pageSize)           
ToString                    Method     string ToString()                                                                                                                 
Bottom                      Property   float Bottom {get;}                                                                                                               
BottomMargin                Property   float BottomMargin {get;}                                                                                                         
HtmlStyleClass              Property   string HtmlStyleClass {get;set;}                                                                                                  
ID                          Property   iTextSharp.text.AccessibleElementId ID {get;set;}                                                                                 
IsInline                    Property   bool IsInline {get;}                                                                                                              
JavaScript_onLoad           Property   string JavaScript_onLoad {get;set;}                                                                                               
JavaScript_onUnLoad         Property   string JavaScript_onUnLoad {get;set;}                                                                                             
Left                        Property   float Left {get;}                                                                                                                 
LeftMargin                  Property   float LeftMargin {get;}                                                                                                           
PageCount                   Property   int PageCount {set;}                                                                                                              
PageNumber                  Property   int PageNumber {get;}                                                                                                             
PageSize                    Property   iTextSharp.text.Rectangle PageSize {get;}                                                                                         
Right                       Property   float Right {get;}                                                                                                                
RightMargin                 Property   float RightMargin {get;}                                                                                                          
Role                        Property   iTextSharp.text.pdf.PdfName Role {get;set;}                                                                                       
Top                         Property   float Top {get;}                                                                                                                  
TopMargin                   Property   float TopMargin {get;}                                                                                                            

您的函数 return 有两个对象,第一个是 boolean,第二个是 iTextSharp.text.Document 对象。除非设置了 -PassThru 参数,否则 Add-Member 不会 return 值,即使在那种情况下,该值也将是对象,而不是 boolean.

因此,您调用的 iTextSharp.text.Document 方法之一是 returning $true$false。您可以将 return 值重定向到 $null 以解决问题:

$Document.SetPageSize([iTextSharp.text.PageSize]::A4) | Out-Null
$Document.SetMargins(5,5,5,5) | Out-Null
$Document.AddAuthor($env:USERNAME) | Out-Null