Skip to content
Advertisement

How to get SignatureLines excel apache poi

Good morning,

I created an excel with signature lines.

enter image description here

I’m trying to obtain signature lines in a excel document with apache poi library.

XSSFWorkbook w = new XSSFWorkbook(mp.getInputStream()); w.get……?

Any suggestion?

Thanks in advance,

Pablo

I see there is a class called XSSFSignatureLine but i don’t see any example to use it.

Advertisement

Answer

To get/set a signature line from/into an Excel sheet, apache poi has introduced XSSFSignatureLine in current apache poi 5.x. This class provides a method parse(XSSFSheet sheet) which gets one signature line per sheet. Seems as if apache poihad not expected that there can be multiple signature lines per sheet.

The text data of the signatures are stored in a VML drawing. So if it is only to get the text data out of the signature lines, then one could get the sheet’s VML drawing and select the data from that XML. Of course the binary data of the signature lines cannot be got from that XML.

Following code sample shows both nof the methods.

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;

import java.io.FileInputStream;

class ExcelGetSignatureLines {
    
 static void getSignatureLines(XSSFSheet sheet) throws Exception {
  XSSFSignatureLine signatureLine = new XSSFSignatureLine();
  signatureLine.parse(sheet);
  System.out.println("Found XSSFSignatureLine:");
  System.out.println(signatureLine.getSuggestedSigner());
  System.out.println(signatureLine.getSuggestedSigner2());
  System.out.println(signatureLine.getSuggestedSignerEmail());
 }
 
 static void getSignatureLinesFromVMLDrawing(XSSFSheet sheet) throws Exception {
  XSSFVMLDrawing vmlDrawing = sheet.getVMLDrawing(false);
  if (vmlDrawing != null) {
   org.apache.poi.schemas.vmldrawing.XmlDocument vmlDrawingDocument = vmlDrawing.getDocument();
   String declareNameSpaces = "declare namespace v='urn:schemas-microsoft-com:vml'; " 
                            + "declare namespace o='urn:schemas-microsoft-com:office:office' ";
   org.apache.xmlbeans.XmlObject[] selectedObjects = vmlDrawingDocument.selectPath(
    declareNameSpaces 
    + ".//v:shape//o:signatureline");
   for (org.apache.xmlbeans.XmlObject object : selectedObjects) {
    if (object instanceof com.microsoft.schemas.office.office.CTSignatureLine) {
     com.microsoft.schemas.office.office.CTSignatureLine ctSignatureLine = (com.microsoft.schemas.office.office.CTSignatureLine)object;
     System.out.println("Found CTSignatureLine:");
     System.out.println(ctSignatureLine.getSuggestedsigner());
     System.out.println(ctSignatureLine.getSuggestedsigner2());
     System.out.println(ctSignatureLine.getSuggestedsigneremail());
    }
   }  
  }
 }
 
 public static void main(String[] args) throws Exception {
  Workbook workbook = WorkbookFactory.create(new FileInputStream("./WorkbookHavingSignatureLines.xlsx"));
  for (Sheet sheet : workbook ) {
   if (sheet instanceof XSSFSheet) {
    System.out.println("Sheet " + sheet.getSheetName());
    getSignatureLines((XSSFSheet)sheet);
    getSignatureLinesFromVMLDrawing((XSSFSheet)sheet);
   }
  }
  workbook.close();
 }
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement