使用lotusscript从Lotus Notes中检索内联图像

时间:2010-10-24 12:42:09

标签: lotus-notes lotusscript

我有一些NotesDocument,其中一些RichText字段同时包含文本和内嵌图像。我可以获得该项目的文本部分,但无法使用lotusscript检索内联图像。任何人都可以建议我从该文件中检索内嵌图像。 LotusScript代码:

Sub Click(Source As Button)
    Dim session As New NotesSession   
    Dim db As NotesDatabase   
    Dim mainDoc As NotesDocument
    Dim v As NotesView   
    Set db = session.CurrentDatabase   

    Dim fileName As String
    Dim fileNum As Integer
    fileNum% = Freefile()
    fileName$ = "D:\data.txt"
    Open FileName$ For Append As fileNum%

    Set v = db.GetView("MyView")
    Set mainDoc = v.GetFirstDocument       

    While Not ( mainDoc Is Nothing )              
        Forall i In mainDoc.Items
            If i.Type = RICHTEXT Then
                 Write #fileNum% ,    i.Name & ":" & i.text  'how the images??
            End If
        End Forall              
        Set mainDoc = v.GetNextDocument( mainDoc )  
    Wend
End Sub

感谢。

4 个答案:

答案 0 :(得分:2)

麦达斯是最简单的方法,但它不是免费的。 (总的来说,节省的钱总是值得的,但是如果你的组织就像我工作过的那样,那么该工具的全部成本将在拥有当前项目的计费单元上被强制执行,而不是而不是在整个组织上摊销,他们可能会在同意费用之前改变他们的要求。)还有另一种方法,那就是使用导出选项ConvertNotesBitmapToGIF将数据库导出到DXL(Domino XML)。图像将作为<picture>元素在XML中以Base64编码的数据出现。如果您完全在Notes环境中运行,则需要创建一个临时文档,其中富文本字段用作NotesMIMEEntity,以便在将编码图片流式传输到文件之前将其转换为二进制文件(使用NotesStream)。所有这些都假定您使用的是版本6或更高版本;如果你在R5或更早,Midas或使用C API直接访问CD记录是唯一的飞行方式。

答案 1 :(得分:0)

我建议您查看Genii Software MidasLSX产品。它们提供了一个LotusScript扩展包,可以更轻松地处理Lotus Notes富文本项的复杂性。

http://www.geniisoft.com/showcase.nsf/MidasHelp

否则,您可以尝试使用NotesRichTextNavigator类来访问富文本项中的图像(理论上)。关于这类事情的文档很少。我无法确定图像在使用该类时会出现什么,但假设您浏览富文本项并且能够将图像作为NotesEmbeddedObject处理,我知道有一种方法可以将对象保存到磁盘那个班。

另一个(疯狂的)想法是通过电子邮件发送文档,并通过另一个可以更轻松地处理电子邮件正文的程序接收文档。 Notes只是对处理自己的富文本字段没有帮助。

答案 2 :(得分:0)

七年后,我一直把头发拉过来。 Rod H的答案是关于附件,但嵌入式图像完全是另一回事。

我最好的运气来自位于此处的@ andre-guirard的LotusScript Gold Collection代码:https://www.openntf.org/main.nsf/project.xsp?r=project/LotusScript%20Gold%20Collection但是,这并不能解决所有问题,因为它无法处理嵌入式文档图像以旧方式嵌入。 (注释改变了存储嵌入图像的方式。)

我非常努力地将其与此处提供的AGECOM信息结合起来:https://www.agecom.com.au/support/agecomkb.nsf/0/58cbf10f0ab723c9ca25803e006c7de8?OpenDocument通过改变安德烈的EmbeddedImage对象来无缝地处理这两种格式,看看是否有丰富的嵌入式图像文本字段实际上只是一个指向$ FILE字段的指针然后,如果是这样,获取一个FileItem对象,但最终我用尽了我的理解和选项,以至于我无法证明花费我雇主的资源是合理的。 (我的时间)就可以了。

因此,如果您嵌入了以新方式包含的图像,我认为安德烈的代码可以不受干扰地运行。否则,我尽我所能,但我没有答案 ......我有什么(对我而言)是一个死胡同,希望你或其他人偶然发现它可以解释我做错了什么让我难堪!

基本上,我从安德烈的代码开始,并以下列方式改变它......

在DOMUtils中,添加以下方法:

%REM
    Function DU_GetMeOrNextSiblingWithAttr
    Description: Starting with a particular node, return that node or the next sibling with an attribute that has a particular value.
        Does not recurse into the tree; looks only at the node passed and later siblings. 
    Parameters:
        nodeStart: node to start your search with.
        targetElement: element name of desired node.
        attrName: attribute name you want to check.
        attrValue: attribute value of element you're looking for.
        flags: string-matching flags to compare attribute, e.g. 1 for case insensitive.
%END REM
Function DU_GetMeOrNextSiblingWithAttr(nodeStart As NotesDOMNode, ByVal targetElement$, ByVal attrName$, ByVal attrValue$, ByVal flags%) As NotesDOMElementNode
    Dim node As NotesDOMNode, elTmp As NotesDOMElementNode
    Set node = nodeStart
    Do Until node.Isnull
        If node.Nodetype = DOMNODETYPE_ELEMENT_NODE Then
            If node.Nodename = targetElement Then
                Set elTmp = node
                If StrComp(elTmp.Getattribute(attrName), attrValue, flags) = 0 Then
                    Set DU_GetMeOrNextSiblingWithAttr = elTmp
                    Exit Function
                End If
            End If
        End If
        Set node = node.Nextsibling
    Loop
End Function

使用以下代码替换FileItem.New:

    %REM
    Sub New
    Description: Arguments are the parsed DOM node of the element representing a
        design element, and the name of the composite item you would like to read,
        modify or create.
%END REM
Sub New(parent As FileItemParent, elNote As NotesDOMElementNode, itemName$, fileName$)
    Set m_elNote = elNote

    SetItem elNote, itemName$, fileName$


    Dim node As NotesDOMNode
    Set node = m_elNote.Parentnode
    While node.Nodetype <> DOMNODETYPE_DOCUMENT_NODE
        Set node = node.Parentnode
    Wend
    Set m_domd = node
    parent.RegisterFileItem Me ' make sure the design element knows about us.
        ' (in case someone gets smart and invokes the constructor directly
        ' instead of using the nice methods we've provided).
End Sub

%REM
    Sub SetItem
<!-- Created Dec 6, 2017 by JSmart523 -->
    If fileName$ is blank, returns the XPath equivalent of elNote/ancestor::document/item[@name=itemName$][position()=1]
    If fileName$ is not blank, returns the XPath equivalent of elNote/ancestor::document/item[@name=itemName$][object/file/@name=fileName$][position()=1]

    Case insensitive. Changes itemName$ and fileName$ to the correct case if found.

    Also sets Me.m_elItem to the returned NotesDOMElementNode
    Also sets Me.m_elRawData to the file contents
%END REM
Sub SetItem(elNote As NotesDOMElementNode, itemName$, fileName$)
    Dim elFile As NotesDOMElementNode
    Dim node As NotesDOMNode

    'set node to ancestor::document
    Set node = elNote
    Do Until node.NodeName = "document"
        Set node = node.ParentNode
    Loop

    'If fileName$ = "", get the first ancestor::document/item[@name=itemName$]
    'Otherwise,         get the first ancestor::document/item[@name=itemName$][/object/file/@name=fileName$]
    Set m_elItem = DU_GetChildOfType(node, DOMNODETYPE_ELEMENT_NODE)
    QualifyingItem m_elItem, itemName$, m_elRawData, fileName$
    m_itemName = itemName$
    m_fileName = fileName$
End Sub

%REM
    Sub QualifyingItem
<!-- Created Dec 8, 2017 by JSmart523 -->
    Starting with incoming elItem node, ensures it's an item we want or changes elItem to the first sibling that qualifies.
%END REM
Sub QualifyingItem(elItem As NotesDOMElementNode, itemName$, elRawData As NotesDOMElementNode, fileName$)
    Dim elFile As NotesDOMElementNode
    Dim node As NotesDOMNode
    Dim elObject As NotesDOMElementNode

    If Not elItem Is Nothing Then
        'Initially, elItem is just a starting point, not necessarily the item we want.
        'If it's an item with the right name, great, otherwise change elItem to the next sibling item with the right name. 
        Set elItem = DU_GetMeOrNextSiblingWithAttr(elItem, "item", "name", itemName$, 1)

        If Not elItem Is Nothing Then
            If fileName$ = "" Then
                'we have the right item, and aren't looking for a file node, which means we want the rawitemdata node
                Set elRawData = DU_getChildNamed("rawitemdata", elItem)
            Else
                'We are looking for a $FILE item that contains a file.
                'There are possibly several $FILE items within a document, one for each file. We've got the right one if ./object/file/@name = fileName$
                Do
                    Set elObject = DU_GetChildNamed("object", elItem)
                    If Not elObject Is Nothing Then
                        Set elFile = DU_GetChildWithAttr(elObject, "file", "name", fileName$, 1)
                        If Not elFile Is Nothing Then
                            'Yay! We have the right elItem node!
                            Set elRawData = DU_GetChildNamed("filedata", elFile)
                            fileName$ = elFile.GetAttribute("name")
                            Exit Do
                        End If
                    End If
                    Set elItem = DU_GetMeOrNextSiblingWithAttr(elItem.NextSibling, "item", "name", itemName$, 1)
                Loop Until elItem Is Nothing
                'At this point, either we jumped out of the loop with a valid elItem and elRawData, or elItem is Nothing
            End If
        End If
    End If

    If elItem Is Nothing Then
        'we didn't find the correct item
        'make sure elRawData is changed to Nothing, too.
        Set elRawData = Nothing
    Else
        itemName$ = elItem.GetAttribute("name")
    End If
End Sub

同样在FileItem脚本库中,添加一个新类FileItemParent

    %REM
    Class FileItemParent
<!-- Created Dec 5, 2017 by JSmart523 -->
    This is a base class for objects that use FileItem objects
%END REM
Class FileItemParent
    m_elElRoot As NotesDOMElementNode
    m_elFD As NotesDOMElementNode
    Public m_fileItem As FileItem
    m_fItems List As FileItem ' list of FileItems we've created and returned to caller.
    m_iMode As Integer

    %REM
        Property Get DOMElement
        Description: Return the element node representing the design element.
    %END REM
    Public Property Get DOMElement As NotesDOMElementNode
        Set DOMElement = m_elElRoot
    End Property

    %REM
        Sub New
        Arguments:
            db: the database containing the design element.
            elElement: the DOM element corresponding to the design note (e.g. the <note>
                element).
            domp: The DOM parser object containing elElement.
    %END REM
    Sub New(elElement As NotesDOMElementNode)
        Set m_elElRoot = elElement
    End Sub

    Sub Delete
        On Error Resume Next
        ForAll thing In m_fItems
            Delete thing
        End ForAll
    End Sub

    %REM
        Function HasItem
        Description: Determine whether there's an item element in the note DXL with a
            given item name.
            Note that the presence of an item doesn't guarantee it's formatted as a file
            CD record.
    %END REM
    Function HasItem(ByVal itemName$) As Boolean
        HasItem = Not (DU_GetChildWithAttr(m_elElRoot, "item", "name", itemName, 1) Is Nothing)
    End Function


    %REM
        Function RegisterFileItem
        Description: For internal use -- lets the FileItem class notify us that it's
            referencing our DOM tree so that we can delete the object if we erase the
            corresponding item element.
    %END REM
    Sub RegisterFileItem(x As FileItem)
        Set m_fItems(LCase(x.itemName)) = x
        If m_FileItem Is Nothing Then
            Set m_FileItem = x
        End If
    End Sub

    %REM
        Function GetFileItem
        Description: Retrieve the FileItem object associated with a CD-record item.
            An object will be returned even if the item doesn't exist, which you can
            use to create the item via UpdateFile method.
    %END REM
    Function GetFileItem(itemName$, fileName$) As FileItem
        Set GetFileItem = New FileItem(Me, m_elElRoot, itemName, fileName)
    End Function

End Class

FileItemParent类主要是从Andre的FileResource类中获取的代码,因此FileResource和EmbeddedImage都可以使用它。更改FileResource以扩展FileItemParent,删除任何重复的代码。

现在我们要更改EmbeddedImage,以便即使嵌入的图像节点包含指向$ FILE项而不是实际内容的链接,也会返回实际内容。

因此,更改EmbeddedImage以扩展FileItemParent

将以下方法添加/替换为EmbededImage

%REM
    Sub InitFileItem
<!-- Created Dec 6, 2017 by JSmart523 -->
    Called by New
%END REM
Sub InitFileItem()
    Dim buffer As Variant 'byte array
    Dim iFileNameLen As Integer
    Dim sFileName As String
    Dim sItemName As String
    Dim stream As NotesStream

    If Len(m_b64) < 30000 Then
        'If content is short then maybe it's a link to a $FILE item instead of the actual content?
        Dim session As New NotesSession
        Set stream = session.CreateStream()
        Base64ToBinary m_b64, stream
        stream.Position = 0
        buffer = stream.Read(1)
        If buffer(0) = 196 Then
            'this is a link to a $FILE, not the actual image contents!

            stream.Position = 10
            buffer = stream.Read(2)
            iFileNameLen = ConvertWordByteArray(buffer)

            stream.Position = 24
            buffer = stream.Read(iFileNameLen)
            sFileName = BytesToString(buffer)
            sItemName = "$FILE"

            GetFileItem sItemName, sFileName 'sets m_fileItem to a FileItem object
        End If
    End If
End Sub

%REM
    Property Get SuggestedFileName
%END REM
Public Property Get SuggestedFileName As String
    If m_fileItem Is Nothing Then
        SuggestedFileName = "Embedded-" + ItemName + "." + SuggestedFileType
    Else
        SuggestedFileName = m_fileItem.FileName
        If InStr(SuggestedFileName, ".") = 0 Then
            SuggestedFileName = SuggestedFileName + "." + SuggestedFileType
        End If
    End If
End Property

%REM
    Property Get SuggestedFileType
%END REM
Public Property Get SuggestedFileType As String
    If ImageType = "notesbitmap" Then
        SuggestedFileType = "bmp"
    Else
        SuggestedFileType = ImageType
    End If
End Property

%REM
    Sub ReadFileToStream
%END REM
Sub ReadFileToStream(streamOut As NotesStream)
    If m_FileItem Is Nothing Then
        ReadToStream streamOut
    Else
        Set m_FileItem.Stream = streamOut
        m_FileItem.Load
    End If
End Sub

然后将EmbeddedItem.New更改为最后调用InitFileItem,这样如果它是一个链接,那么获取内容将返回内容而不是链接。

好吧,到目前为止,据我所知这么好,但问题是存储在$ FILE项目中的嵌入式图像的CD记录(即富文本字段的嵌入式图像节点只包含一个链接而不是尽管有AGECOM的代码和解释,对于我来说,实际的图像记录的方式对我来说是难以理解的。我可以使用上面的代码和Andre的EmbeddedImageList对象来抓取每一个嵌入的图像,但我根本无法得到一个&#34; ConvertOldCDToNew&#34;工作方法,所以我无法将旧的CD记录格式转换为可靠,无损的文件!我不知道我是否剥离了太多字节,没有剥离正确的字节,或者我只是forgot to carry the two

答案 3 :(得分:-1)

这是我用来从文档上的richtext字段中分离文件的代理。

Option Public
Dim uidoc As notesuidocument
Dim doc As NotesDocument
Dim db As NotesDatabase
Dim obj As NotesEmbeddedObject
Dim collection As NotesDocumentCollection
Dim rt As Variant
Dim attachNames As Variant
Dim i As Integer, x As Integer
Dim j As Integer
' Agent  - Detach Attachments to C drive for later reattachment
' Copies all attachment in richtext field to personal directory.

Sub Initialize
    Dim ws As New notesuiworkspace
    Dim ses As New NotesSession
    Set db = ses.CurrentDatabase
    Set collection = db.UnprocessedDocuments
    '  get first doc in collection
    For j = 1 To collection.Count
        Set doc = collection.GetNthDocument( j )    
' ---  create array of filenames of all the attachments in the document
        i = 0
        Redim attachNames(i)
        Forall x In doc.items
            If x.name = "$FILE" Then
                attachNames(i) = x.values(0)
                i = i + 1
                Redim Preserve attachNames(i)
            End If
        End Forall

        If i > 0 Then
            Redim Preserve attachNames(i-1)
        End If

' ---  for all of the filenames in attachNames, if it exists in the rich text field, detatch them
        If doc.hasItem("richtextfieldname") Then
            Set rt = doc.GetFirstItem("richtextfieldname")
        End If
        If attachNames(0) <> "" Then
            Forall x In attachNames 
                Set obj = rt.GetEmbeddedObject( x )
                If Not( obj Is Nothing ) Then
                    Call obj.ExtractFile( "C:\path\" & Cstr(x) )
                End If
            End Forall
        End If
        Call doc.save(True, False)
    Next
End Sub