dummysignaturehandler.java

来自「Java生成PDF Java生成PDF Java生成PDF」· Java 代码 · 共 92 行

JAVA
92
字号
import org.faceless.pdf2.*;import java.io.*;import java.util.zip.CRC32;import java.security.KeyStore;import java.security.GeneralSecurityException;/** * This simple class shows how to create a SignatureHandler class * that verifies the document using a CRC-32 checksum. */public class DummySignatureHandler extends SignatureHandler{    public DummySignatureHandler()    {        super();    }    public String getFilter()    {        return "Dummy.CRC32";    }    protected void prepareToSign(KeyStore store, String alias, char[] password)        throws GeneralSecurityException    {        // not using a keystore in this example, so just ignore them.        super.prepareToSign(store, alias, password);        putNumericValue("Version", 1);	// Add some values to the signature dictionary    }    public byte[] sign(InputStream in)    {        CRC32 crc = new CRC32();        int b;	try {	    while ((b=in.read())>=0) {		crc.update(b);	    }	} catch (IOException e) {	    throw new RuntimeException("Unexpected IOException!");	}	int val = (int)crc.getValue();        byte[] out = new byte[4];        out[0]=(byte)(val>>24);        out[1]=(byte)(val>>16);        out[2]=(byte)(val>>8);        out[3]=(byte)(val);        return out;    }    public boolean verify(InputStream in)    {        CRC32 crc = new CRC32();        int b;	try {	    while ((b=in.read())>=0) {		crc.update(b);	    }	} catch (IOException e) {	    throw new RuntimeException("Unexpected IOException!");	}        int val = (int)crc.getValue();        byte[] buf = getStringValue("Contents");        return val == ((buf[0]<<24 & 0xff000000) +                       (buf[1]<<16 & 0xff0000) +                       (buf[2]<<8 & 0xff00) +                       (buf[3] & 0xff));    }        public String[] getLayerNames()    {        final String[] names = { "n0" };        return names;    }    public PDFCanvas getLayerAppearance(String name, PDFStyle style)    {	if (name.equals("n0")) {	    PDFCanvas canvas = new PDFCanvas(100, 100);	    canvas.setStyle(style);	    LayoutBox box = new LayoutBox(80);	    box.addTextNoBreak("CRC-32", style, null);	    canvas.drawLayoutBox(box, 10, 50);	    return canvas;	} else {	    // This will never happen, as we're specifying the only layer is "n0"	    // in the method above. Still, it's an example so we'll do it properly.	    throw new IllegalArgumentException("Unknown layer '"+name+"' requested");	}    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?