July 18, 2007

How to use iText in Lotus Notes

iText was introduced in iText and Lotus Notes, there are some important steps.
1. Download iText 1.3.6, if you use Notes 6.x.
2. Import the Jar to Script Library.
3. Write your PDF Java program for iText. I write Two page into One page effect that refer TwoOnOne.java . My TwoOnOne.java code.
4. Then, use LotusScript to create java object and run. LS2J Example DB is a good material.
My LotusScript:
Option Public
Option Explicit

Uselsx "*javacon"
Use "iText-Merge"

Sub Initialize
On Error Goto processError
Dim ws As New NotesUIWorkspace
Dim uidoc As NotesUIDocument
Dim doc As NotesDocument
Set uidoc = ws.CurrentDocument

Dim rtitem As NotesRichTextItem
Dim filename() As String
Dim index As Integer
index = 0
If uidoc.IsNewDoc Then
Call uidoc.Save
Else
Set doc = uidoc.Document
Call doc.Save(False, False)
End If
Set doc = uidoc.Document
Call uidoc.Close
Set uidoc = ws.EditDocument(True, doc)
Set rtitem = doc.GetFirstItem("Body")
If Isempty(rtitem.EmbeddedObjects) Then
Msgbox "未放入PDF附件,請放入後重新執行!", 16, "錯誤訊息"
Exit Sub
End If
Dim nameflag As Boolean
nameflag = True
If ( rtitem.Type = RICHTEXT ) Then
Forall o In rtitem.EmbeddedObjects
If ( o.Type = EMBED_ATTACHMENT ) Then
If Right(o.Source, 3) = "pdf" Then
Redim Preserve filename(index)
Call o.ExtractFile( "c:\temp\" & o.Source )
filename(index) = o.Source
index = index + 1
nameflag = False
End If
End If
End Forall
End If
If nameflag Then
Msgbox "未放入PDF附件,請放入後重新執行!", 16, "錯誤訊息"
Exit Sub
End If

Java code for iText

Dim jSession As New JavaSession
Dim PDFClass As JavaClass
Dim PDFClass1 As JAVACLASS
Dim PDFObj As JavaObject
Dim PDFobj1 As JavaOBject
Dim jError As JavaError

Set PDFClass = jSession.GetClass("Concatenate")

Dim v As String
Dim x As Integer
For x = 0 To Ubound(filename)
v = v & filename(x) & "|"
Next
v = v & "FinalPDF.pdf"

Set PDFObj = PDFClass.CreateObject("(Ljava/lang/String;)V", v)
If PDFObj.merge() Then
Set PDFClass1 = jSession.GetClass("TwoOnOne")
Set PDFObj1 = PDFClass1.CreateObject("(Ljava/lang/String;)V", "C:\\TEMP\\FinalPDF.pdf")
If PDFObj1.print() Then
Msgbox "PDF合併列印完成,請列印NOTESDONE.PDF檔案!", 64, "執行結果"
Else
Msgbox "PDF合併列印發生錯誤,請通知資訊處NOTES經辦!", 16, "錯誤訊息"
Exit Sub
End If
Else
Msgbox "PDF合併檔案發生錯誤,請通知資訊處NOTES經辦!", 16, "錯誤訊息"
Exit Sub
End If
Dim rtitem1 As NotesRichTextItem
Dim object As NotesEmbeddedObject
Set rtitem1 = New NotesRichTextItem( doc, "Body1" )
Set object = rtitem1.EmbedObject( EMBED_ATTACHMENT, "", "C:\Temp\notesdone.pdf")
Call doc.Save(False, False)
Call uidoc.Close
Set uidoc = ws.EditDocument(True, doc)
Exit Sub

processError:
'** report any errors we get and keep on going
Set jError = jSession.getLastJavaError()
If (jError.errorMsg = "") Then
Print "Notes Error at line " & Erl & ": " & Error$
Else
Print "Error at line " & Erl & ": " & jError.errorMsg
jSession.ClearJavaError
End If
End Sub

I suggest using Eclipse to check your Java code. Notes IDE is poor for Java coding.

LS2J iText LotusScript
Posted by philipz at July 18, 2007 12:39 AM
歷史上的今天
Comments
Post a comment













Remember personal info?