Previous Topic

Next Topic

Book Contents

Book Index

Create a preview of PDF attachments

Envision a database containing documents with Acrobat PDF attachments. By using II2LN together with Ghostscript, you can extract the PDF attachment and make an image of the first page automatically. The agent below will do that. Please note that the agent look for the PDF attachments in the Body field. For each PDF attachment found, the agent will create two new rich text fields, rtDisplay_n and rtAttachment_n, where "n" represent the number of the attachment. If you want to display the image, please add the necessary rtDisplay_n fields to your Notes form.

'Process PDF Attachments with II2LN:

Option Public

Option Declare

' Functions from II2LN

Declare Function ImportImage Lib "VCII2LN.DLL" (_

Byval pstrNotesServer As String, _

Byval pstrNotesDatabase As String, _

Byval pstrNotesUNID As String, _

Byval pstrNotesField As String, _

Byval pstrFilename As String, _

Byval pstrImportOptions As String) As Long

' General Windows functions

Declare Function GetTempPath Lib "kernel32.dll" Alias "GetTempPathA" (Byval nBufferLength As Long, Byval lpBuffer As String) As Long

Sub Initialize

Dim session As New NotesSession

Dim db As NotesDatabase

Dim col As NotesDocumentCollection

Dim doc As NotesDocument

Dim docNext As NotesDocument

Dim strUNID As String

Dim lRc As Long

Dim strTempPath As String

Dim strFilePath As String

Dim body As NotesRichTextItem

Dim listAttachmentsInDoc List As String

Dim strImportOptions As String

Dim listImportOptions List As String

Dim strIO As String

Dim strIOParam As String

Dim strTmp As String

Dim iCounter As Integer

' Get the temporary path for the user. This is the directory where the temp files will be stored

strTempPath = GetTemporaryPath()

On Error Resume Next

' Prepare to loop through all the selected documents

Set db = session.CurrentDatabase

Set col = db.UnprocessedDocuments

Set doc = col.GetFirstDocument

' For each selected document ....

While Not doc Is Nothing

' Remember the UNID of the currently selected document

strUNID = doc.UniversalID

' Get the next document now - because when we use II2LN, we detach the doc variable!

Set docNext = col.GetNextDocument(doc)

' Find and extract the attachment. If found, then process the current document with II2LN - but

' only if the document has attachments at all

If doc.HasEmbedded Then

' Ensure list of filenames is empty

Erase listAttachmentsInDoc

' Get the handle to the richtext item "Body"

Set body = doc.GetFirstItem("Body")

' Loop through all the attachments in the document

iCounter = 0

Forall attachment In body.EmbeddedObjects

If attachment.Type = EMBED_ATTACHMENT Then

' Construct temporary file path for attachment

strFilePath = strTempPath & attachment.Source

' Extract attachment

Call attachment.ExtractFile(strFilePath)

' If our list of filenames for this document doesn't contain the filename, then add it

If Not Iselement(listAttachmentsInDoc(strFilePath)) Then

listAttachmentsInDoc(Cstr(iCounter)) =

strFilePath

iCounter = iCounter + 1

End If

Call attachment.Remove

End If

End Forall

' Save the document - this will essentially save the document *without* the attachment

Call doc.Save(True,False)

' Ok, we don't need to reference the doc-variable anymore before II2LN processing,

' so we get rid of it

Set doc = Nothing

' Loop through all attachments

iCounter = 1

Forall strFileName In listAttachmentsInDoc

' Clear list of II2LN options

Erase listImportOptions

' Ok, now we have a list of attachment filenames, and they are all extracted to temp directory

' Time to work with II2LN!

' Using the list structure below makes it easy to add new import options!

listImportOptions("SetResizeMethod") = "7"

listImportOptions("SetLogLevel") = "5"

' listImportOptions("Sharpen") = "50"

' The SetGhostscriptProcessor is not necessary if you find GSWIN32C.EXE in one of your PATH directories

listImportOptions("SetGhostscriptProcessor") = "E:\Programs\gswin32c.exe" ' The exact path and filename to GS

listImportOptions("SetGhostscriptParameters") = "-r300 -dTextAlphaBits=4 -dGraphicsAlphaBits=4" ' Enhance PDF conversion!!

listImportOptions("Resize") = "1024,0"

listImportOptions("AttachOriginalFile") = "rtAttachment_" & Cstr(iCounter)

' Straighten out the listImportOptions list to a string, which I can use with II2LN

Forall impopt In listImportOptions

strIO = Listtag(impopt)

strIOParam = impopt

strTmp = strIO & ":" & strIOParam

If strImportOptions = "" Then

strImportOptions = strTmp

Else

strImportOptions = strImportOptions & ";" & strTmp

End If

End Forall

' Process the image with II2LN

lRc = ImportImage(_

db.Server,_ ' The server

db.FilePath,_ ' The database

strUNID, _ ' The UNID

"rtDisplay_" & Cstr(iCounter),_ ' The field name

strFileName,_ ' The image to import

strImportOptions) ' The import options

' If OK, delete the temp file

If lRc = 0 Then

Kill strFileName

End If

iCounter = iCounter + 1

End Forall

End If ' end if doc.HasEmbedded

' Advance to the next selected document in the view

Set doc = docNext

Wend

Exit Sub

End Sub

Function GetTemporaryPath() As String

Dim strTempPath As String*255

Dim lStringLength As Long

strTempPath = String(255, 0) ' Initialize string

lStringLength = GetTempPath(255, strTempPath)

GetTemporaryPath = Left$(strTempPath, lStringLength)

End Function

See Also

Examples

Import an image from view context to the current document

Create an e-mail containing an image