-
I'm trying to create a PDF signing demo using iText. My code depends on public static String signPdf(String base64Pdf, String base64PrivateKey, String base64Certificate) {
try {
setupBouncyCastle();
byte[] pdfBytes = Base64.decodeBase64(base64Pdf.getBytes());
byte[] privateKeyBytes = Base64.decodeBase64(base64PrivateKey.getBytes());
byte[] certBytes = Base64.decodeBase64(base64Certificate.getBytes());
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
X509Certificate cert = (X509Certificate) certFactory.generateCertificate(new ByteArrayInputStream(certBytes));
PdfReader reader = new PdfReader(new ByteArrayInputStream(pdfBytes));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter writer = new PdfWriter(baos);
PdfSigner signer = new PdfSigner(reader, writer, new StampingProperties().useAppendMode());
IExternalSignature pks = new PrivateKeySignature(privateKey, DigestAlgorithms.SHA256, CounterService.provider.getName());
IExternalDigest digest = new BouncyCastleDigest();
signer
.setReason("Test")
.setLocation("Test")
.setPageRect(new Rectangle(0, 0, 200, 100))
.setPageNumber(1)
.setFieldName("Test_" + System.currentTimeMillis());
signer.signDetached(digest, pks, new X509Certificate[]{cert}, null, null, null, 0, PdfSigner.CryptoStandard.CADES);
writer.close();
reader.close();
String result = Arrays.toString(Base64.encodeBase64(baos.toByteArray()));
System.out.println("✅ Sign Successful " + result);
return result;
} catch (Exception e) {
e.printStackTrace();
return null;
}
} Console output: java.lang.NoClassDefFoundError: java.util.function.Supplier
at java.lang.VMClassLoader.findClassInClasspathForLoader(Native Method)
at java.lang.PathClassLoader.findClass(PathClassLoader.java:193)
at java.lang.ClassLoader.loadClass(ClassLoader.java:515)
at java.lang.ClassLoader.loadClass(ClassLoader.java:475)
at com.itextpdf.bouncycastleconnector.BouncyCastleFactoryCreator.populateFactoriesMap(BouncyCastleFactoryCreator.java:108)
at com.itextpdf.bouncycastleconnector.BouncyCastleFactoryCreator.<clinit>(BouncyCastleFactoryCreator.java:52)
at com.itextpdf.signatures.DigestAlgorithms.<clinit>(DigestAlgorithms.java:118)
at com.itextpdf.signatures.PrivateKeySignature.<init>(PrivateKeySignature.java:89)
at com.itextpdf.signatures.PrivateKeySignature.<init>(PrivateKeySignature.java:72)
at org.robovm.samples.myjavaframework.CounterService.signPdf(CounterService.java:124)
at org.robovm.samples.myjavaframework.CounterService.increase(CounterService.java:67) My <config>
<os>ios</os>
<arch>arm64</arch>
<arch>thumbv7</arch>
<target>framework</target>
<imageName>${framework.name}</imageName>
<infoPList>Info.plist.xml</infoPList>
<treeShaker>aggressive</treeShaker>
<forceLinkClasses>
<pattern>java.io.ByteArrayInputStream</pattern>
<pattern>java.io.ByteArrayOutputStream</pattern>
<pattern>java.io.IOException</pattern>
<pattern>java.security.KeyFactory</pattern>
<pattern>java.security.PrivateKey</pattern>
<pattern>java.security.Provider</pattern>
<pattern>java.security.Security</pattern>
<pattern>java.security.cert.CertificateFactory</pattern>
<pattern>java.security.cert.X509Certificate</pattern>
<pattern>java.security.spec.PKCS8EncodedKeySpec</pattern>
<pattern>org.robovm.samples.myjavaframework.**</pattern>
<pattern>com.itextpdf.signatures.PdfSigner</pattern>
<pattern>com.itextpdf.signatures.IExternalSignature</pattern>
<pattern>com.itextpdf.signatures.DigestAlgorithms</pattern>
<pattern>com.itextpdf.signatures.IExternalDigest</pattern>
<pattern>com.itextpdf.signatures.PrivateKeySignature</pattern>
<pattern>com.itextpdf.signatures.BouncyCastleDigest</pattern>
<pattern>com.itextpdf.bouncycastleconnector.**</pattern>
<pattern>com.android.org.conscrypt.OpenSSLProvider</pattern>
<pattern>com.android.org.conscrypt.OpenSSLRandom</pattern>
<pattern>com.android.org.conscrypt.OpenSSLBIOInputStream</pattern>
<pattern>org.bouncycastle.**</pattern>
<pattern>java.util.logging.**</pattern>
</forceLinkClasses>
<exportedSymbols>
<symbol>JNI_CreateJavaVM</symbol>
</exportedSymbols>
</config>
My pom.xml file: <?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.robovm</groupId>
<artifactId>robovm-samples-parent</artifactId>
<version>1.0</version>
</parent>
<artifactId>robovm-samples-dynamiclib</artifactId>
<name>RoboVM dynamic library sample</name>
<packaging>jar</packaging>
<repositories>
<repository>
<id>central</id>
<name>Maven Central</name>
<url>https://repo1.maven.org/maven2/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>oss-sonatype</id>
<name>oss-sonatype</name>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.itextpdf.android</groupId>
<artifactId>kernel-android</artifactId>
<version>8.0.3</version>
</dependency>
<dependency>
<groupId>com.itextpdf.android</groupId>
<artifactId>layout-android</artifactId>
<version>8.0.3</version>
</dependency>
<dependency>
<groupId>com.itextpdf.android</groupId>
<artifactId>sign-android</artifactId>
<version>8.0.3</version>
</dependency>
<dependency>
<groupId>com.itextpdf.android</groupId>
<artifactId>bouncy-castle-adapter-android</artifactId>
<version>8.0.3</version>
</dependency>
<dependency>
<groupId>com.itextpdf.android</groupId>
<artifactId>bouncy-castle-connector-android</artifactId>
<version>8.0.3</version>
</dependency>
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.12.2</version>
</dependency>
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
<version>1.4.01</version>
</dependency>
<dependency>
<groupId>com.mobidevelop.robovm</groupId>
<artifactId>robovm-rt</artifactId>
<version>2.3.21</version>
</dependency>
</dependencies>
</project> |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
current version of MobiVM is Android 4,4 based which means its stuck at java7 level. |
Beta Was this translation helpful? Give feedback.
-
Thanks! I'll give the experimental build a try |
Beta Was this translation helpful? Give feedback.
-
The experimental build fixed the issue. Thanks again! |
Beta Was this translation helpful? Give feedback.
current version of MobiVM is Android 4,4 based which means its stuck at java7 level.
java.util.function.Supplier
class is part of Function API that was added in Java8 and not completely available.your option is either use older Bouncy Castle or not use it at all and generate certificates with available in RoboVM libraries.
there is an experimental build with Android12 runtime https://github.com/robovmx/robovmx/releases, you can give it a try.
but it was not updated for a while