Encountering an issue when invoking a Java method. I have implemented Java code successfully for adding a signature image to a PDF. It functioned well when executed via the command prompt, but when attempting to integrate it into UiPath using Java activities,I encounter an error: “Invoke Java Method: One or more errors occurred. (The method could not be invoked.)” Provided below are sample input files and also following this blog Learn Rpa Uipath.
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 post - 1 participant