You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从Java代理/JAR向LotusScript返回PDF解析字符串值?

在LotusScript中获取Java代理返回的PDF字符串并上传API

注意:你当前的Java代码直接将PDF字节转换为字符串的方式是错误的。PDF是二进制格式,直接把字节强转成char会产生大量乱码,且无法被API正确解析。正确的做法是将PDF二进制数据编码为Base64字符串,这样既可以安全传输,也能被API还原为原始PDF文件;如果你的需求是提取PDF中的文本内容,则需要使用专门的PDF解析库(如iText)。

方案一:修改Java代理,将PDF转为Base64并传递给LotusScript

修正后的Java代理代码

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Base64;
import lotus.domino.*;

public class PdfByteReaderAgent extends AgentBase {

    @Override
    public void NotesMain() {
        Session session = null;
        Document doc = null;
        try {
            session = getSession();
            AgentContext agentContext = session.getAgentContext();
            doc = agentContext.getDocumentContext();
            
            // 从文档字段获取PDF文件路径(可根据实际场景调整为代理参数)
            String filePath = doc.getItemValueString("PDFFilePath");
            
            // 将PDF转为Base64字符串
            String pdfBase64 = readPdfAsBase64(filePath);
            
            // 将结果写入文档字段,供LotusScript读取
            doc.replaceItemValue("PDFBase64Content", pdfBase64);
            doc.save(true, false);
        } catch (Exception e) {
            e.printStackTrace();
            try {
                if (doc != null) {
                    doc.replaceItemValue("Error", "PDF处理失败:" + e.getMessage());
                    doc.save(true, false);
                }
            } catch (NotesException ne) {
                ne.printStackTrace();
            }
        } finally {
            // 回收Notes对象
            try {
                if (doc != null) doc.recycle();
            } catch (NotesException e) {
                e.printStackTrace();
            }
        }
    }

    private String readPdfAsBase64(String filePath) throws IOException {
        File pdfFile = new File(filePath);
        byte[] pdfBytes = new byte[(int) pdfFile.length()];
        
        try (FileInputStream fis = new FileInputStream(pdfFile)) {
            fis.read(pdfBytes);
        }
        
        return Base64.getEncoder().encodeToString(pdfBytes);
    }
}

LotusScript调用代理并获取结果

Sub Initialize
    Dim session As New NotesSession
    Dim db As NotesDatabase
    Dim agent As NotesAgent
    Dim doc As NotesDocument
    
    ' 创建临时文档存储文件路径(或使用现有业务文档)
    Set db = session.CurrentDatabase
    Set doc = db.CreateDocument()
    doc.Form = "PDFProcessForm"
    doc.PDFFilePath = "C:\your\pdf\file.pdf" ' 替换为实际PDF路径
    doc.Save True, False
    
    ' 执行Java代理,传递文档ID作为参数
    Set agent = db.GetAgent("PdfByteReaderAgent") ' 代理名称需与Java代理一致
    Call agent.Run(doc.NoteID)
    
    ' 读取处理结果
    Set doc = db.GetDocumentByID(doc.NoteID)
    If doc.HasItem("PDFBase64Content") Then
        Dim pdfBase64 As String
        pdfBase64 = doc.PDFBase64Content(0)
        
        ' 上传到目标API
        Dim http As NotesHTTPRequest
        Set http = session.CreateHTTPRequest()
        Dim postData As String
        postData = "pdfContent=" & URLEncode(pdfBase64)
        
        Dim response As String
        response = http.Post("https://your-api-endpoint/upload", postData)
        
        Print "API上传响应:" & response
    ElseIf doc.HasItem("Error") Then
        Print "处理错误:" & doc.Error(0)
    End If
    
    ' 清理临时文档
    Call doc.Remove(True)
End Sub

方案二:直接在LotusScript中处理PDF(无需Java代理)

如果你的Notes版本支持内置Base64编码,可以直接在LotusScript中完成PDF读取和编码,省去Java代理的步骤:

' 将二进制文件转为Base64字符串
Function FileToBase64(filePath As String) As String
    Dim fileNum As Integer
    Dim fileLength As Long
    Dim byteBuffer As Variant
    Dim base64Str As String
    
    fileNum = Freefile()
    Open filePath For Binary Access Read As fileNum
    fileLength = LOF(fileNum)
    ReDim byteBuffer(1 To fileLength) As Byte
    Get fileNum, , byteBuffer
    Close fileNum
    
    ' 使用Notes内置的Base64编码函数
    base64Str = EncodeBase64(byteBuffer)
    FileToBase64 = base64Str
End Function

Sub Initialize
    Dim pdfPath As String
    pdfPath = "C:\your\pdf\file.pdf"
    Dim pdfBase64 As String
    pdfBase64 = FileToBase64(pdfPath)
    
    ' 上传到目标API
    Dim session As New NotesSession
    Dim http As NotesHTTPRequest
    Set http = session.CreateHTTPRequest()
    
    Dim response As String
    response = http.Post("https://your-api-endpoint/upload", "pdfContent=" & URLEncode(pdfBase64))
    
    Print "上传结果:" & response
End Sub

额外说明(提取PDF文本场景)

如果你的需求是提取PDF中的文本内容(而非传输整个PDF文件),需要在Java代码中使用PDF解析库(如iText 7),示例代码片段如下:

// 引入iText依赖后,提取PDF文本
private String extractPdfText(String filePath) throws IOException {
    StringBuilder text = new StringBuilder();
    try (PdfReader reader = new PdfReader(filePath);
         PdfDocument pdfDoc = new PdfDocument(reader)) {
        for (int i = 1; i <= pdfDoc.getNumberOfPages(); i++) {
            text.append(PdfTextExtractor.getTextFromPage(pdfDoc.getPage(i)));
        }
    }
    return text.toString();
}

内容的提问来源于stack exchange,提问作者Lea Giardili

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 02:35:03