itext pdf verify if signature is visible or invisible

点点圈 提交于 2019-12-11 16:55:57

问题


I am currently using Itext 5.4.4 and I would like to know if it is possible to check if a signature in my current PDF is visible or invisible. Is this possible? Or do I have to upgrade to a newer versión of Itext?


回答1:


You can do it like the code below. Other flags are easily added..

    PdfReader reader; //instantiate
    AcroFields acroFields = reader.getAcroFields();
    Map<String, Item> fieldNames = acroFields.getFields();
    Set<Entry<String, Item>> entries = fieldNames.entrySet();
    Iterator<Entry<String, Item>> it = entries.iterator();

while(it.hasNext()){
    Entry<String, Item> entry = it.next();
    //Check flags
    boolean invisible = isInvisible(entry.getValue());
    boolean mandatory = isMandatory(entry.getValue());
    boolean noView = isNoView(entry.getValue());
    boolean hidden = isHidden(entry.getValue());
    ...
}

public static boolean isInvisible(Item item) {
    //Add a nullcheck!

    if(item.size()>0) {
        PdfDictionary d = item.getMerged(0);
        PdfNumber num = (PdfNumber) d.get(PdfName.F);
        return num == null ? false : ((num.intValue() & PdfAnnotation.FLAGS_INVISIBLE) == PdfAnnotation.FLAGS_INVISIBLE);
    }
    return false;
}

public static boolean isMandatory(Item item) {
    //Add a null check here!

    if(item.size()>0) {
        PdfDictionary d = item.getMerged(0);
        PdfNumber num = (PdfNumber) d.get(PdfName.FF);
        return num == null ? false : ((num.intValue() & PdfFormField.FF_REQUIRED) == PdfFormField.FF_REQUIRED);
    }
    return false;
}

public static boolean isNoView(Item item) {
    //nullcheck!

    if(item.size()>0) {
        PdfDictionary d = item.getMerged(0);
        PdfNumber num = (PdfNumber) d.get(PdfName.F);
        return num == null ? false : ((num.intValue() & PdfAnnotation.FLAGS_NOVIEW) == PdfAnnotation.FLAGS_NOVIEW);
    }
    return false;
}

public static boolean isHidden(Item item) {

    //Nullcheck!
    if(item.size()>0) {
        PdfDictionary d = item.getMerged(0);
        PdfNumber num = (PdfNumber) d.get(PdfName.F);
        return num == null ? false : ((num.intValue() & PdfAnnotation.FLAGS_HIDDEN) == PdfAnnotation.FLAGS_HIDDEN);
    }
    return false;
}


来源:https://stackoverflow.com/questions/51063791/itext-pdf-verify-if-signature-is-visible-or-invisible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!