Facing problem while invoking java method

I used this java code to add sign image to pdf> It was working fine in command prompt but it wont works in UiPath. While i use java activities. I get this error → Invoke Java Method: One or more errors occurred. (The method could not be invoked.) These are sample input files
12.pdf (248.0 KB)

package pdfmanipulate;

//Adding Image, Date and Text in Existing PDF using Java

//Importing iText libraries
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;

/**
 *
 * How To Read And Add Image, Date and Text To Existing Pdf Example
 * modify the pdf, Using iText library in core java
 *
 */
public class PdfImageAdder {
    public static void main(String inputFilePath, String outputFilePath, String imagePath, String text) throws Exception {


        OutputStream fos = new FileOutputStream(new File(outputFilePath));

        PdfReader pdfReader = new PdfReader(inputFilePath);
        PdfStamper pdfStamper = new PdfStamper(pdfReader, fos);

        //Image to be added in existing pdf file.
        Image image = Image.getInstance(imagePath);
        image.scaleAbsolute(50, 20); //Scale image's width and height
        image.setAbsolutePosition(60, 43); //Set position for image in PDF

        // Font for text
        Font font = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL, BaseColor.BLACK);

        
        // Date to be added in existing pdf file
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        String formattedDate = dateFormat.format(date);

        // loop on all the PDF pages
        // i is the pdfPageNumber
        for (int i = 1; i <= pdfReader.getNumberOfPages(); i++) {
            //getOverContent() allows you to write pdfContentByte on TOP of existing pdf pdfContentByte.
            //getUnderContent() allows you to write pdfContentByte on BELOW of existing pdf pdfContentByte.
            PdfContentByte pdfContentByte = pdfStamper.getOverContent(i);

            // add image
            pdfContentByte.addImage(image);

            // add text
            ColumnText.showTextAligned(pdfContentByte, Element.ALIGN_LEFT, new Phrase(text, font), 70, 38, 0);
            
            // add date
            ColumnText.showTextAligned(pdfContentByte, Element.ALIGN_LEFT, new Phrase(formattedDate, font), 70, 60, 0);
            System.out.println("Image, Date and Text added in "+outputFilePath);
        }

        pdfStamper.close();
        System.out.println("Modified PDF created in >> "+ outputFilePath);

    }
}
1 Like

@Anil_G can you help me please?

@pravin_bindage

Please open the Exception details from locals panel…you can find the error from there…then we can check the issue

cheers

1 Like

RemoteException wrapping System.AggregateException: One or more errors occurred. (The method could not be invoked.) —> RemoteException wrapping System.InvalidOperationException: The method could not be invoked. —> RemoteException wrapping System.InvalidOperationException: An invocation exception has occured during the execution of the target.
Invocation target exceptions:
java.lang.NoClassDefFoundError: com/itextpdf/text/pdf/PdfReader
pdfmanipulate.PdfImageAdder.main(PdfImageAdder.java:22)

Java Invoker program exception:
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:119)
at java.base/java.lang.reflect.Method.invoke(Method.java:578)
at com.UiPath.instance.ObjectInstance.InvokeMethod(ObjectInstance.java:141)
at com.UiPath.invoker.InvokerContext.invokeMethod(InvokerContext.java:143)
at com.UiPath.invoker.InvokerContext.invokeMethod(InvokerContext.java:35)
at com.UiPath.server.DotNetProcessRequest.processRequest(DotNetProcessRequest.java:55)
at com.UiPath.Main.main(Main.java:46)
Caused by: java.lang.NoClassDefFoundError: com/itextpdf/text/pdf/PdfReader
at pdfmanipulate.PdfImageAdder.main(PdfImageAdder.java:22)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
… 6 more
Caused by: java.lang.ClassNotFoundException: com.itextpdf.text.pdf.PdfReader
at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:445)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:588)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
… 8 more

at UiPath.Java.Service.JavaResponse.ThrowExceptionIfNeeded()
at UiPath.Java.JavaInvoker.SendJavaRequest(RequestType requestType,
CancellationToken ct,
String className,
String methodName,
String fieldName,
JavaObject javaObject,
List1 parameters, List1 parametersTypes)
at UiPath.Java.JavaInvoker.InvokeMethod(String methodName,
String className,
JavaObject javaObject,
List1 parameters, List1 parametersTypes,
CancellationToken ct)
at UiPath.Java.Activities.InvokeJavaMethod.ExecuteAsync(AsyncCodeActivityContext context,
CancellationToken cancellationToken)
— End of inner exception stack trace —
at UiPath.Java.Activities.InvokeJavaMethod.ExecuteAsync(AsyncCodeActivityContext context,
CancellationToken cancellationToken)
— End of inner exception stack trace —
at UiPath.Shared.Activities.AsyncTaskCodeActivity.EndExecute(AsyncCodeActivityContext context,
IAsyncResult result)
at System.Activities.AsyncCodeActivity.System.Activities.IAsyncCodeActivity.FinishExecution(AsyncCodeActivityContext context,
IAsyncResult result)
at System.Activities.AsyncCodeActivity.CompleteAsyncCodeActivityData.CompleteAsyncCodeActivityWorkItem.Execute(ActivityExecutor executor,
BookmarkManager bookmarkManager)

@pravin_bindage

It is unable to identify the class itext…looks like the path variable in the environment is not pointing properly…can you check and add the location in the path variable in your windows

cheers

1 Like

Now i get this one – >
RemoteException wrapping System.AggregateException: One or more errors occurred. (The method could not be invoked.) —> RemoteException wrapping System.InvalidOperationException: The method could not be invoked. —> RemoteException wrapping System.InvalidOperationException: Method could not be invoked.
Java Invoker program exception:
java.lang.NoSuchMethodException
at com.UiPath.instance.ObjectInstance.getMethod(ObjectInstance.java:223)
at com.UiPath.instance.ObjectInstance.InvokeMethod(ObjectInstance.java:140)
at com.UiPath.invoker.InvokerContext.invokeMethod(InvokerContext.java:143)
at com.UiPath.invoker.InvokerContext.invokeMethod(InvokerContext.java:35)
at com.UiPath.server.DotNetProcessRequest.processRequest(DotNetProcessRequest.java:55)
at com.UiPath.Main.main(Main.java:46)

at UiPath.Java.Service.JavaResponse.ThrowExceptionIfNeeded()
at UiPath.Java.JavaInvoker.SendJavaRequest(RequestType requestType,
CancellationToken ct,
String className,
String methodName,
String fieldName,
JavaObject javaObject,
List1 parameters, List1 parametersTypes)
at UiPath.Java.JavaInvoker.InvokeMethod(String methodName,
String className,
JavaObject javaObject,
List1 parameters, List1 parametersTypes,
CancellationToken ct)
at UiPath.Java.Activities.InvokeJavaMethod.ExecuteAsync(AsyncCodeActivityContext context,
CancellationToken cancellationToken)
— End of inner exception stack trace —
at UiPath.Java.Activities.InvokeJavaMethod.ExecuteAsync(AsyncCodeActivityContext context,
CancellationToken cancellationToken)
— End of inner exception stack trace —
at UiPath.Shared.Activities.AsyncTaskCodeActivity.EndExecute(AsyncCodeActivityContext context,
IAsyncResult result)
at System.Activities.AsyncCodeActivity.System.Activities.IAsyncCodeActivity.FinishExecution(AsyncCodeActivityContext context,
IAsyncResult result)
at System.Activities.AsyncCodeActivity.CompleteAsyncCodeActivityData.CompleteAsyncCodeActivityWorkItem.Execute(ActivityExecutor executor,
BookmarkManager bookmarkManager)

@pravin_bindage

Have you configured the java library path ? which would be ideally java jome from env variables?

Can you try testing with a small code without any imports first and then we can add more?

And are you using any maven or anything for creating your jar?

ANd May I know why do you have two jars that you are loading?

cheers

1 Like

image

I didn’t use any maven

@pravin_bindage

Can you check the java_Home is having the same path please

cheers

yes it is at same path

@pravin_bindage

Did you happen to try with a simple java script and see if ita working…

Nothing looks off here…

Tey removing system.out.println…this is not needed in a way…not like its because of that but a caution…

Alternately try changing the return type to string or so than void …this might be the issue as well

First try using a simple method…and check may be a addition and use it…

Cheers

1 Like

Hi @pravin_bindage

Try below vb.net code in UiPath

Vb.net code:

Dim inputFilePath As String = “C:\input.pdf”
Dim outputFilePath As String = “C:\output.pdf”
Dim imagePath As String = “C:\image.jpg”
Dim text As String = “Hello World”

Dim pdfReader As New iTextSharp.text.pdf.PdfReader(inputFilePath)
Dim fos As New System.IO.FileStream(outputFilePath, System.IO.FileMode.Create)
Dim pdfStamper As New iTextSharp.text.pdf.PdfStamper(pdfReader, fos)

Dim image As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(imagePath)
image.ScaleAbsolute(50, 20) 'Scale image’s width and height
image.SetAbsolutePosition(60, 43) 'Set position for image in PDF

Dim font As iTextSharp.text.Font = iTextSharp.text.FontFactory.GetFont(iTextSharp.text.FontFactory.HELVETICA, 12, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.BLACK)

Dim dateNow As DateTime = DateTime.Now
Dim formattedDate As String = dateNow.ToString(“dd/MM/yyyy”)

For i As Integer = 1 To pdfReader.NumberOfPages
Dim pdfContentByte As iTextSharp.text.pdf.PdfContentByte =
pdfStamper.GetOverContent(i)
pdfContentByte.AddImage(image)

Dim textPhrase As New iTextSharp.text.Phrase(text, font)
ColumnText.ShowTextAligned(pdfContentByte, iTextSharp.text.Element.ALIGN_LEFT, textPhrase, 70, 38, 0)

Dim datePhrase As New iTextSharp.text.Phrase(formattedDate, font)
ColumnText.ShowTextAligned(pdfContentByte, iTextSharp.text.Element.ALIGN_LEFT, datePhrase, 70, 60, 0)NextpdfStamper.Close()

Regards,
Kaviyarasu N

1 Like