本文整理汇总了Java中com.itextpdf.text.pdf.PdfSignatureAppearance.setVisibleSignature方法的典型用法代码示例。如果您正苦于以下问题:Java PdfSignatureAppearance.setVisibleSignature方法的具体用法?Java PdfSignatureAppearance.setVisibleSignature怎么用?Java PdfSignatureAppearance.setVisibleSignature使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.itextpdf.text.pdf.PdfSignatureAppearance
的用法示例。
在下文中一共展示了PdfSignatureAppearance.setVisibleSignature方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sign50MBruno
import com.itextpdf.text.pdf.PdfSignatureAppearance; //导入方法依赖的package包/类
@Test
public void sign50MBruno() throws IOException, DocumentException, GeneralSecurityException
{
String filepath = "src/test/resources/mkl/testarea/itext5/signature/50m.pdf";
String digestAlgorithm = "SHA512";
CryptoStandard subfilter = CryptoStandard.CMS;
// Creating the reader and the stamper
PdfReader reader = new PdfReader(filepath);
FileOutputStream os = new FileOutputStream(new File(RESULT_FOLDER, "50m-signedBruno.pdf"));
PdfStamper stamper =
PdfStamper.createSignature(reader, os, '\0', RESULT_FOLDER, false);
// Creating the appearance
PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
appearance.setReason("reason");
appearance.setLocation("location");
appearance.setVisibleSignature(new Rectangle(36, 748, 144, 780), 1, "sig");
// Creating the signature
ExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm, "BC");
ExternalDigest digest = new BouncyCastleDigest();
MakeSignature.signDetached(appearance, digest, pks, chain,
null, null, null, 0, subfilter);
}
示例2: sign50MBrunoAppend
import com.itextpdf.text.pdf.PdfSignatureAppearance; //导入方法依赖的package包/类
@Test
public void sign50MBrunoAppend() throws IOException, DocumentException, GeneralSecurityException
{
String filepath = "src/test/resources/mkl/testarea/itext5/signature/50m.pdf";
String digestAlgorithm = "SHA512";
CryptoStandard subfilter = CryptoStandard.CMS;
// Creating the reader and the stamper
PdfReader reader = new PdfReader(filepath);
FileOutputStream os = new FileOutputStream(new File(RESULT_FOLDER, "50m-signedBrunoAppend.pdf"));
PdfStamper stamper =
PdfStamper.createSignature(reader, os, '\0', RESULT_FOLDER, true);
// Creating the appearance
PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
appearance.setReason("reason");
appearance.setLocation("location");
appearance.setVisibleSignature(new Rectangle(36, 748, 144, 780), 1, "sig");
// Creating the signature
ExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm, "BC");
ExternalDigest digest = new BouncyCastleDigest();
MakeSignature.signDetached(appearance, digest, pks, chain,
null, null, null, 0, subfilter);
}
示例3: sign50MNaive
import com.itextpdf.text.pdf.PdfSignatureAppearance; //导入方法依赖的package包/类
/**
* <a href="http://stackoverflow.com/questions/30449348/signing-pdf-memory-consumption">
* Signing PDF - memory consumption
* </a>
* <br>
* <a href="http://50mpdf.tk/50m.pdf">50m.pdf</a>
* <p>
* {@link #sign50MNaive()} tests the naive approach,
* {@link #sign50MBruno()} tests Bruno's original approach,
* {@link #sign50MBrunoPartial()} tests Bruno's approach with partial reading,
* {@link #sign50MBrunoAppend()} tests Bruno's approach with append mode, and
* {@link #sign50MBrunoPartialAppend()} tests Bruno's approach with partial reading and append mode.
* </p>
*/
// runs with -Xmx240m, fails with -Xmx230m
@Test
public void sign50MNaive() throws IOException, DocumentException, GeneralSecurityException
{
String filepath = "src/test/resources/mkl/testarea/itext5/signature/50m.pdf";
String digestAlgorithm = "SHA512";
CryptoStandard subfilter = CryptoStandard.CMS;
// Creating the reader and the stamper
PdfReader reader = new PdfReader(filepath);
FileOutputStream os = new FileOutputStream(new File(RESULT_FOLDER, "50m-signedNaive.pdf"));
PdfStamper stamper =
PdfStamper.createSignature(reader, os, '\0');
// Creating the appearance
PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
appearance.setReason("reason");
appearance.setLocation("location");
appearance.setVisibleSignature(new Rectangle(36, 748, 144, 780), 1, "sig");
// Creating the signature
ExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm, "BC");
ExternalDigest digest = new BouncyCastleDigest();
MakeSignature.signDetached(appearance, digest, pks, chain,
null, null, null, 0, subfilter);
}
示例4: sign50MBrunoPartial
import com.itextpdf.text.pdf.PdfSignatureAppearance; //导入方法依赖的package包/类
@Test
public void sign50MBrunoPartial() throws IOException, DocumentException, GeneralSecurityException
{
String filepath = "src/test/resources/mkl/testarea/itext5/signature/50m.pdf";
String digestAlgorithm = "SHA512";
CryptoStandard subfilter = CryptoStandard.CMS;
// Creating the reader and the stamper
PdfReader reader = new PdfReader(filepath, null, true);
FileOutputStream os = new FileOutputStream(new File(RESULT_FOLDER, "50m-signedBrunoPartial.pdf"));
PdfStamper stamper =
PdfStamper.createSignature(reader, os, '\0', RESULT_FOLDER, false);
// Creating the appearance
PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
appearance.setReason("reason");
appearance.setLocation("location");
appearance.setVisibleSignature(new Rectangle(36, 748, 144, 780), 1, "sig");
// Creating the signature
ExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm, "BC");
ExternalDigest digest = new BouncyCastleDigest();
MakeSignature.signDetached(appearance, digest, pks, chain,
null, null, null, 0, subfilter);
}
示例5: sign50MBrunoPartialAppend
import com.itextpdf.text.pdf.PdfSignatureAppearance; //导入方法依赖的package包/类
@Test
public void sign50MBrunoPartialAppend() throws IOException, DocumentException, GeneralSecurityException
{
String filepath = "src/test/resources/mkl/testarea/itext5/signature/50m.pdf";
String digestAlgorithm = "SHA512";
CryptoStandard subfilter = CryptoStandard.CMS;
// Creating the reader and the stamper
PdfReader reader = new PdfReader(filepath, null, true);
FileOutputStream os = new FileOutputStream(new File(RESULT_FOLDER, "50m-signedBrunoPartialAppend.pdf"));
PdfStamper stamper =
PdfStamper.createSignature(reader, os, '\0', RESULT_FOLDER, true);
// Creating the appearance
PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
appearance.setReason("reason");
appearance.setLocation("location");
appearance.setVisibleSignature(new Rectangle(36, 748, 144, 780), 1, "sig");
// Creating the signature
ExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm, "BC");
ExternalDigest digest = new BouncyCastleDigest();
MakeSignature.signDetached(appearance, digest, pks, chain,
null, null, null, 0, subfilter);
}
示例6: signCertifyG
import com.itextpdf.text.pdf.PdfSignatureAppearance; //导入方法依赖的package包/类
/**
* <a href="http://stackoverflow.com/questions/30526254/sign-concatenated-pdf-in-append-mode-with-certified-no-changes-allowed">
* Sign concatenated PDF in append mode with CERTIFIED_NO_CHANGES_ALLOWED
* </a>
* <br>
* <a href="https://www.dropbox.com/s/lea6r9fup6th44c/test_pdf.zip?dl=0">test_pdf.zip</a>
*
* {@link #signCertifyG()} certifies g.pdf, OK
* {@link #sign2g()} merely signs 2g.pdf, OK
* {@link #signCertify2gNoAppend()} certifies 2g.pdf but not in append mode, OK
* {@link #tidySignCertify2g()} first tidies, then certifies 2g.pdf, OK
* {@link #signCertify2g()} certifies 2g.pdf, Adobe says invalid
* {@link #signCertify2gFix()} certifies 2g-fix.pdf, OK!
*
* 2g-fix.pdf is a patched version of 2g.pdf with a valid /Size trailer entry
* and a valid, single-sectioned cross reference table
*/
@Test
public void signCertifyG() throws IOException, DocumentException, GeneralSecurityException
{
String filepath = "src/test/resources/mkl/testarea/itext5/signature/g.pdf";
String digestAlgorithm = "SHA512";
CryptoStandard subfilter = CryptoStandard.CMS;
// Creating the reader and the stamper
PdfReader reader = new PdfReader(filepath, null, true);
FileOutputStream os = new FileOutputStream(new File(RESULT_FOLDER, "g-certified.pdf"));
PdfStamper stamper =
PdfStamper.createSignature(reader, os, '\0', RESULT_FOLDER, true);
// Creating the appearance
PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
appearance.setCertificationLevel(PdfSignatureAppearance.CERTIFIED_NO_CHANGES_ALLOWED);
appearance.setReason("reason");
appearance.setLocation("location");
appearance.setVisibleSignature(new Rectangle(36, 748, 144, 780), 1, "sig");
// Creating the signature
ExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm, "BC");
ExternalDigest digest = new BouncyCastleDigest();
MakeSignature.signDetached(appearance, digest, pks, chain,
null, null, null, 0, subfilter);
}
示例7: sign2g
import com.itextpdf.text.pdf.PdfSignatureAppearance; //导入方法依赖的package包/类
@Test
public void sign2g() throws IOException, DocumentException, GeneralSecurityException
{
String filepath = "src/test/resources/mkl/testarea/itext5/signature/2g.pdf";
String digestAlgorithm = "SHA512";
CryptoStandard subfilter = CryptoStandard.CMS;
// Creating the reader and the stamper
PdfReader reader = new PdfReader(filepath, null, true);
FileOutputStream os = new FileOutputStream(new File(RESULT_FOLDER, "2g-signed.pdf"));
PdfStamper stamper =
PdfStamper.createSignature(reader, os, '\0', RESULT_FOLDER, true);
// Creating the appearance
PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
//appearance.setCertificationLevel(PdfSignatureAppearance.CERTIFIED_NO_CHANGES_ALLOWED);
appearance.setReason("reason");
appearance.setLocation("location");
appearance.setVisibleSignature(new Rectangle(36, 748, 144, 780), 1, "sig");
// Creating the signature
ExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm, "BC");
ExternalDigest digest = new BouncyCastleDigest();
MakeSignature.signDetached(appearance, digest, pks, chain,
null, null, null, 0, subfilter);
}
示例8: signCertify2g
import com.itextpdf.text.pdf.PdfSignatureAppearance; //导入方法依赖的package包/类
@Test
public void signCertify2g() throws IOException, DocumentException, GeneralSecurityException
{
String filepath = "src/test/resources/mkl/testarea/itext5/signature/2g.pdf";
String digestAlgorithm = "SHA512";
CryptoStandard subfilter = CryptoStandard.CMS;
// Creating the reader and the stamper
PdfReader reader = new PdfReader(filepath, null, true);
FileOutputStream os = new FileOutputStream(new File(RESULT_FOLDER, "2g-certified.pdf"));
PdfStamper stamper =
PdfStamper.createSignature(reader, os, '\0', RESULT_FOLDER, true);
// Creating the appearance
PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
appearance.setCertificationLevel(PdfSignatureAppearance.CERTIFIED_NO_CHANGES_ALLOWED);
appearance.setReason("reason");
appearance.setLocation("location");
appearance.setVisibleSignature(new Rectangle(36, 748, 144, 780), 1, "sig");
// Creating the signature
ExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm, "BC");
ExternalDigest digest = new BouncyCastleDigest();
MakeSignature.signDetached(appearance, digest, pks, chain,
null, null, null, 0, subfilter);
}
示例9: signCertify2gNoAppend
import com.itextpdf.text.pdf.PdfSignatureAppearance; //导入方法依赖的package包/类
@Test
public void signCertify2gNoAppend() throws IOException, DocumentException, GeneralSecurityException
{
String filepath = "src/test/resources/mkl/testarea/itext5/signature/2g.pdf";
String digestAlgorithm = "SHA512";
CryptoStandard subfilter = CryptoStandard.CMS;
// Creating the reader and the stamper
PdfReader reader = new PdfReader(filepath, null, true);
FileOutputStream os = new FileOutputStream(new File(RESULT_FOLDER, "2g-certified-noAppend.pdf"));
PdfStamper stamper =
PdfStamper.createSignature(reader, os, '\0', RESULT_FOLDER);
// Creating the appearance
PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
appearance.setCertificationLevel(PdfSignatureAppearance.CERTIFIED_NO_CHANGES_ALLOWED);
appearance.setReason("reason");
appearance.setLocation("location");
appearance.setVisibleSignature(new Rectangle(36, 748, 144, 780), 1, "sig");
// Creating the signature
ExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm, "BC");
ExternalDigest digest = new BouncyCastleDigest();
MakeSignature.signDetached(appearance, digest, pks, chain,
null, null, null, 0, subfilter);
}
示例10: signCertify2gFix
import com.itextpdf.text.pdf.PdfSignatureAppearance; //导入方法依赖的package包/类
@Test
public void signCertify2gFix() throws IOException, DocumentException, GeneralSecurityException
{
String filepath = "src/test/resources/mkl/testarea/itext5/signature/2g-fix.pdf";
String digestAlgorithm = "SHA512";
CryptoStandard subfilter = CryptoStandard.CMS;
// Creating the reader and the stamper
PdfReader reader = new PdfReader(filepath, null, true);
FileOutputStream os = new FileOutputStream(new File(RESULT_FOLDER, "2g-fix-certified.pdf"));
PdfStamper stamper =
PdfStamper.createSignature(reader, os, '\0', RESULT_FOLDER, true);
// Creating the appearance
PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
appearance.setCertificationLevel(PdfSignatureAppearance.CERTIFIED_NO_CHANGES_ALLOWED);
appearance.setReason("reason");
appearance.setLocation("location");
appearance.setVisibleSignature(new Rectangle(36, 748, 144, 780), 1, "sig");
// Creating the signature
ExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm, "BC");
ExternalDigest digest = new BouncyCastleDigest();
MakeSignature.signDetached(appearance, digest, pks, chain,
null, null, null, 0, subfilter);
}
示例11: C2_01_SignHelloWorld_sign
import com.itextpdf.text.pdf.PdfSignatureAppearance; //导入方法依赖的package包/类
public void C2_01_SignHelloWorld_sign(String src, String dest, Certificate[] chain, PrivateKey pk, String digestAlgorithm, String provider, CryptoStandard subfilter, String reason, String location)
throws GeneralSecurityException, IOException, DocumentException {
// Creating the reader and the stamper
PdfReader reader = new PdfReader(src);
FileOutputStream os = new FileOutputStream(dest);
PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0');
// Creating the appearance
PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
appearance.setReason(reason);
appearance.setLocation(location);
appearance.setVisibleSignature(new Rectangle(36, 748, 144, 780), 1, "sig");
// Creating the signature
ExternalDigest digest = new BouncyCastleDigest();
ExternalSignature signature = new PrivateKeySignature(pk, digestAlgorithm, provider);
MakeSignature.signDetached(appearance, digest, signature, chain, null, null, null, 0, subfilter);
}
示例12: sign2274_2007_H_PROVISIONAL_multifield
import com.itextpdf.text.pdf.PdfSignatureAppearance; //导入方法依赖的package包/类
@Test
public void sign2274_2007_H_PROVISIONAL_multifield() throws IOException, DocumentException, GeneralSecurityException
{
String filepath = "src/test/resources/mkl/testarea/itext5/signature/2274_2007_H_PROVISIONAL - multifield.pdf";
String digestAlgorithm = "SHA512";
CryptoStandard subfilter = CryptoStandard.CMS;
// Creating the reader and the stamper
PdfReader reader = new PdfReader(filepath, null, true);
FileOutputStream os = new FileOutputStream(new File(RESULT_FOLDER, "2274_2007_H_PROVISIONAL - multifield-signed.pdf"));
PdfStamper stamper =
PdfStamper.createSignature(reader, os, '\0', RESULT_FOLDER, true);
// Creating the appearance
PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
//appearance.setCertificationLevel(PdfSignatureAppearance.CERTIFIED_NO_CHANGES_ALLOWED);
appearance.setReason("reason");
appearance.setLocation("location");
appearance.setVisibleSignature("IntSig1");
// Creating the signature
ExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm, "BC");
ExternalDigest digest = new BouncyCastleDigest();
MakeSignature.signDetached(appearance, digest, pks, chain,
null, null, null, 0, subfilter);
}
示例13: signTest_2_user2699460
import com.itextpdf.text.pdf.PdfSignatureAppearance; //导入方法依赖的package包/类
/**
* <a href="http://stackoverflow.com/questions/32818522/itextsharp-setvisiblesignature-not-working-as-expected">
* ITextSharp SetVisibleSignature not working as expected
* </a>
* <p>
* The issue observed by the OP (user2699460) occurs since iText(Sharp) 5.5.7
* both of iText and iTextSharp.
* </p>
* <p>
* The file signed in this sample, test-2-user2699460-signed.pdf, has been created
* as intermediary result using a simplified version of the OP's c# code.
* </p>
*/
@Test
public void signTest_2_user2699460() throws IOException, DocumentException, GeneralSecurityException
{
String filepath = "src/test/resources/mkl/testarea/itext5/signature/test-2-user2699460.pdf";
String digestAlgorithm = "SHA512";
CryptoStandard subfilter = CryptoStandard.CMS;
// Creating the reader and the stamper
PdfReader reader = new PdfReader(filepath, null, true);
FileOutputStream os = new FileOutputStream(new File(RESULT_FOLDER, "test-2-user2699460-signed.pdf"));
PdfStamper stamper =
PdfStamper.createSignature(reader, os, '\0', RESULT_FOLDER, true);
// Creating the appearance
PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
//appearance.setCertificationLevel(PdfSignatureAppearance.CERTIFIED_NO_CHANGES_ALLOWED);
appearance.setReason("reason");
appearance.setLocation("location");
appearance.setVisibleSignature("Bunker");
// Creating the signature
ExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm, "BC");
ExternalDigest digest = new BouncyCastleDigest();
MakeSignature.signDetached(appearance, digest, pks, chain,
null, null, null, 0, subfilter);
}
示例14: signWithCustomLayer2
import com.itextpdf.text.pdf.PdfSignatureAppearance; //导入方法依赖的package包/类
/**
* <a href="https://stackoverflow.com/questions/45062602/itext-pdfappearence-issue">
* Text - PDFAppearence issue
* </a>
* <p>
* This test shows how one can create a custom signature layer 2.
* As the OP of the question at hand mainly wants to generate a
* pure DESCRIPTION appearance that uses the whole area, we here
* essentially copy the PdfSignatureAppearance.getAppearance code
* for generating layer 2 in pure DESCRIPTION mode and apply it
* to a plain pre-fetched layer 2.
* </p>
*/
@Test
public void signWithCustomLayer2() throws IOException, DocumentException, GeneralSecurityException
{
String digestAlgorithm = "SHA512";
CryptoStandard subfilter = CryptoStandard.CMS;
try ( InputStream resource = getClass().getResourceAsStream("/mkl/testarea/itext5/extract/test.pdf") )
{
PdfReader reader = new PdfReader(resource);
FileOutputStream os = new FileOutputStream(new File(RESULT_FOLDER, "test-customLayer2.pdf"));
PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0');
PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
appearance.setReason("reason");
appearance.setLocation("location");
appearance.setVisibleSignature(new Rectangle(36, 748, 144, 780), 1, "sig");
// This essentially is the PdfSignatureAppearance.getAppearance code
// for generating layer 2 in pure DESCRIPTION mode applied to a plain
// pre-fetched layer 2.
// vvvvv
PdfTemplate layer2 = appearance.getLayer(2);
String text = "We're using iText to put a text inside a signature placeholder in a PDF. "
+ "We use a code snippet similar to this to define the Signature Appearence.\n"
+ "Everything works fine, but the signature text does not fill all the signature "
+ "placeholder area as expected by us, but the area filled seems to have an height "
+ "that is approximately the 70% of the available space.\n"
+ "As a result, sometimes especially if the length of the signature text is quite "
+ "big, the signature text does not fit in the placeholder and the text is striped "
+ "away.";
Font font = new Font();
float size = font.getSize();
final float MARGIN = 2;
Rectangle dataRect = new Rectangle(
MARGIN,
MARGIN,
appearance.getRect().getWidth() - MARGIN,
appearance.getRect().getHeight() - MARGIN);
if (size <= 0) {
Rectangle sr = new Rectangle(dataRect.getWidth(), dataRect.getHeight());
size = ColumnText.fitText(font, text, sr, 12, appearance.getRunDirection());
}
ColumnText ct = new ColumnText(layer2);
ct.setRunDirection(appearance.getRunDirection());
ct.setSimpleColumn(new Phrase(text, font), dataRect.getLeft(), dataRect.getBottom(), dataRect.getRight(), dataRect.getTop(), size, Element.ALIGN_LEFT);
ct.go();
// ^^^^^
ExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm, "BC");
ExternalDigest digest = new BouncyCastleDigest();
MakeSignature.signDetached(appearance, digest, pks, chain, null, null, null, 0, subfilter);
}
}
示例15: doSign
import com.itextpdf.text.pdf.PdfSignatureAppearance; //导入方法依赖的package包/类
public ByteArrayOutputStream doSign(byte[] pdf, Rectangle stampPos, int pageNmbrForStamp) throws IOException, DocumentException, NoSuchAlgorithmException, InvalidKeyException, SignatureException {
Certificate[] chain = signCert.toArray(new Certificate[0]);
PdfReader reader = new PdfReader(pdf);
ByteArrayOutputStream byteOS = new ByteArrayOutputStream();
PdfStamper stp = PdfStamper.createSignature(reader, byteOS, '\0', null, true);
PdfSignatureAppearance sap = stp.getSignatureAppearance();
if (stampPos != null) {
sap.setVisibleSignature(new com.itextpdf.text.Rectangle(stampPos.x, stampPos.y, stampPos.width, stampPos.height), pageNmbrForStamp, null);
sap.setRenderingMode(PdfSignatureAppearance.RenderingMode.NAME_AND_DESCRIPTION);
sap.setAcro6Layers(true);
}
// Siganture Appearance
PdfSignature dic = new PdfSignature(PdfName.ADOBE_PPKLITE, new PdfName("adbe.pkcs7.detached"));
log.info("Creating signature with reason: " + ParamValidator.getInstance().getSignatureReason());
sap.setReason(ParamValidator.getInstance().getSignatureReason());
sap.setLocation("Ruhr-Universität Bochum");
Image i = Image.getInstance(getClass().getResource("/de/rub/dez6a3/jpdfsigner/resources/images/sign.png"));
sap.setImage(i);
sap.setCrypto((PrivateKey) signPrivKey, chain, null, PdfSignatureAppearance.WINCER_SIGNED);
dic.setReason(ParamValidator.getInstance().getSignatureReason());
dic.setLocation("Ruhr-Universität Bochum");
sap.setCryptoDictionary(dic);
// preserve some space for the contents
int contentEstimated = 15000;
HashMap<PdfName, Integer> exc = new HashMap<PdfName, Integer>();
exc.put(PdfName.CONTENTS, new Integer(contentEstimated * 2 + 2));
sap.preClose(exc);
// make the digest
InputStream data = sap.getRangeStream();
MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
byte buf[] = new byte[8192];
int n;
while ((n = data.read(buf)) > 0) {
messageDigest.update(buf, 0, n);
}
byte hash[] = messageDigest.digest();
Calendar cal = Calendar.getInstance();
// If we add a time stamp:
TSAClient tsc = new TSAClientBouncyCastle("http://zeitstempel.dfn.de/");
// Create the signature
PdfPKCS7 sgn;
try {
sgn = new PdfPKCS7((PrivateKey) signPrivKey, chain, null, "SHA1", null, false);
byte sh[] = sgn.getAuthenticatedAttributeBytes(hash, cal, null);
sgn.update(sh, 0, sh.length);
byte[] encodedSig = sgn.getEncodedPKCS7(hash, cal, tsc, null);
if (contentEstimated + 2 < encodedSig.length) {
throw new DocumentException("Not enough space");
}
byte[] paddedSig = new byte[contentEstimated];
System.arraycopy(encodedSig, 0, paddedSig, 0, encodedSig.length);
// Replace the contents
PdfDictionary dic2 = new PdfDictionary();
dic2.put(PdfName.CONTENTS, new PdfString(paddedSig).setHexWriting(true));
sap.close(dic2);
} catch (NoSuchProviderException ex) {
ex.printStackTrace();
}
return byteOS;
}