Skip to content

Instantly share code, notes, and snippets.

Code to Digitally Sign an Excel Document using Java. For further details: https://kb.aspose.com/cells/java/how-to-digitally-sign-an-excel-document-using-java/
import java.io.FileInputStream;
import java.io.InputStream;
import com.aspose.cells.DigitalSignature;
import com.aspose.cells.DigitalSignatureCollection;
import com.aspose.cells.Workbook;
public class AsposeTest {
public static void main(String[] args) throws Exception {//Main function to add a digital signature to a spreadsheet in java
// Instantiate a license
com.aspose.cells.License slidesLicense = new com.aspose.cells.License();
slidesLicense.setLicense("Aspose.Total.lic");
// Using the cryptography PKCS12, create a Keystore
java.security.KeyStore keyStore = java.security.KeyStore.getInstance("PKCS12");
// Load certificate into the InputStream
InputStream inStreamCert = new FileInputStream("TestCert1.pfx");
// Load the certificate into the Keystore by providing the certificate file and its password
keyStore.load(inStreamCert, "testcert1".toCharArray());
// Create the digital signature
DigitalSignature digtSign = new DigitalSignature(keyStore, "testcert1", "New digital signature is added to a workbook",com.aspose.cells.DateTime.getNow());
// Instantiate a collection of digital signatures
DigitalSignatureCollection digtSignColl = new DigitalSignatureCollection();
// Add the digital signature to the collection
digtSignColl.add(digtSign);
// Load the spreadsheet
Workbook wbToBeSigned = new Workbook("sampleSpreadsheet.xlsx");
// Add the collection of digital signatures to the workbook
wbToBeSigned.addDigitalSignature(digtSignColl);
// Save the workbook with an electronic signature
wbToBeSigned.save("outputDigitallySignedByCells.xlsx");
System.out.println("Done");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment