Question
· Oct 23, 2023

import jar file and call one of public class public method in objectscript

Hi Friends,

We have a requirement to read pdf text in ensemble object script code. As object script doesn't have direct solution , i tried to implement python code , but

iris 2020 , there is no support for python in object script.

so I have found one java utill using pdfbox api.

Now I have created jar and want to register into iris and want to make a call to mymethod to read pdf text.

please help me to way forward to achieve it.

java code:

============

package org.optimus.utils;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

import java.io.File;
import java.io.IOException;

public class PdfToText {
   
    public static String  getText(String filepath) {
        // Load the PDF file
        String text="";
        File pdfFile = new File(filepath); // Replace with your PDF file's path
        PDDocument document = null;
        try {
            document = PDDocument.load(pdfFile);


        // Create a PDFTextStripper object
        PDFTextStripper textStripper = new PDFTextStripper();

        // Get the text from the PDF document
        text = textStripper.getText(document);

        // Close the PDF document
        document.close();
    }catch (IOException e) {
        e.printStackTrace();
    }
        return text;
    }
}

created jar file named PdfText.jar.

/// Description

ClassMethod MethodName() As %Status

{

  Set jarFilePath = "C:\\InterSystems\\IRISHealth\\lib\\PdfReader.jar"  ; Replace with the actual path to your JAR file

  Set jarName = "MyJar"  ; This is the name you'll use to refer to the JAR file in your code

 

  Set sc =  ##class(%Net.Remote.Java.JavaGateway).RegisterJar(jarName, jarFilePath)

  If $$$ISERR(sc) {

    Write "Error registering JAR file: ", sc

  }

  Else {

    Write "JAR file registered successfully."

  }

}

I am getting below error

Product version: IRIS 2020.2
Discussion (4)4
Log in or sign up to continue

Hi,

Using the new(ish) External Java Server and Gateway connection, this worked for me. I copied your class, refactored it for the Apache PDFBox version 3.0.0 and then called it from the command line. I did receive some warnings that have nothing to do with PDFBox, they are related to the larger project that I placed this code into. The call worked just fine. I don't know exactly when the new external language server/gateway support was added but it is documented in 2021.1: https://docs.intersystems.com/iris20211/csp/docbook/DocBook.UI.Page.cls?KEY=BEXTSERV_intro. I'm pasting the IRIS Session commands I used:

USER>set java = $system.external.getJavaGateway()

USER>do java.addToPath("<path to my project/target/intersystems-demo-1.0-SNAPSHOT.jar")

USER>set text = java.invoke("demo.intersystems.utils.PdfToText","getText","/home/danp/Downloads/DM32.2-2017-00157-ambiguity-in-JSON-array-constructor.pdf")

USER>zw text
text="ISO/IEC JTC1/SC32 WG3:CMH-023"_$c(10)_"ANSI INCITS DM32.2-2017-00157"_$c(10)_"- 1 of 7 -"_$c(10)_"Title: Ambiguity in <JSON array constructor>"_$c(10)_"Author: <<snipped>>

I even have a nice way to return the text as an instance of %Stream.Object if that is interesting to you.

HTH,

Dan