将方法添加到itextsharp文档对象

时间:2018-03-07 09:24:33

标签: powershell itext extension-methods

上下文是我正在处理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认为我试图在布尔值中调用方法?它不识别/支持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;}                                                                                                            

1 个答案:

答案 0 :(得分:1)

您的函数返回两个对象,首先是boolean和第二个iTextSharp.text.Document对象。 Add-Member除非设置了-PassThru参数,否则不会返回值,即使在这种情况下,该值也是对象,而不是boolean

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

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