• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Java PdfSignatureAppearance类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Java中com.itextpdf.text.pdf.PdfSignatureAppearance的典型用法代码示例。如果您正苦于以下问题:Java PdfSignatureAppearance类的具体用法?Java PdfSignatureAppearance怎么用?Java PdfSignatureAppearance使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



PdfSignatureAppearance类属于com.itextpdf.text.pdf包,在下文中一共展示了PdfSignatureAppearance类的18个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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);
}
 
开发者ID:mkl-public,项目名称:testarea-itext5,代码行数:24,代码来源:CreateSignature.java


示例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);
}
 
开发者ID:mkl-public,项目名称:testarea-itext5,代码行数:24,代码来源:CreateSignature.java


示例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);
}
 
开发者ID:mkl-public,项目名称:testarea-itext5,代码行数:39,代码来源:CreateSignature.java


示例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);
}
 
开发者ID:mkl-public,项目名称:testarea-itext5,代码行数:24,代码来源:CreateSignature.java


示例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);
}
 
开发者ID:mkl-public,项目名称:testarea-itext5,代码行数:24,代码来源:CreateSignature.java


示例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);
}
 
开发者ID:mkl-public,项目名称:testarea-itext5,代码行数:42,代码来源:CreateSignature.java


示例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);
}
 
开发者ID:mkl-public,项目名称:testarea-itext5,代码行数:25,代码来源:CreateSignature.java


示例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);
}
 
开发者ID:mkl-public,项目名称:testarea-itext5,代码行数:25,代码来源:CreateSignature.java


示例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);
}
 
开发者ID:mkl-public,项目名称:testarea-itext5,代码行数:25,代码来源:CreateSignature.java


示例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);
}
 
开发者ID:mkl-public,项目名称:testarea-itext5,代码行数:25,代码来源:CreateSignature.java


示例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);
}
 
开发者ID:mkl-public,项目名称:testarea-itext5,代码行数:17,代码来源:CreateSignature.java


示例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);
}
 
开发者ID:mkl-public,项目名称:testarea-itext5,代码行数:25,代码来源:ComplexSignatureFields.java


示例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);
}
 
开发者ID:mkl-public,项目名称:testarea-itext5,代码行数:38,代码来源:ComplexSignatureFields.java


示例14: getServerSigner

import com.itextpdf.text.pdf.PdfSignatureAppearance; //导入依赖的package包/类
/**
 * @param fullPath
 * @param cert
 * @param imageBase64
 * @return
 */
public static ServerSigner getServerSigner(String fullPath,
		Certificate cert, String imageBase64, boolean showSignatureInfo) {
	ServerSigner signer = new ServerSigner(fullPath, cert);
	signer.setSignatureGraphic(imageBase64);
	if(showSignatureInfo) {
		signer.setSignatureAppearance(PdfSignatureAppearance.RenderingMode.GRAPHIC_AND_DESCRIPTION);
	} else {
		signer.setSignatureAppearance(PdfSignatureAppearance.RenderingMode.GRAPHIC);
	}
	return signer;
}
 
开发者ID:VietOpenCPS,项目名称:opencps-v2,代码行数:18,代码来源:BCYSignatureUtil.java


示例15: signPdf

import com.itextpdf.text.pdf.PdfSignatureAppearance; //导入依赖的package包/类
@Override
public byte[] signPdf(byte[] pdfData, PrivateKey pk, Certificate[] chain, Map<PdfSignatureFields, String> params)
    throws DocumentGenerationException {

    try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
        PdfReader reader = new PdfReader(pdfData);
        PdfStamper pdfStamper = PdfStamper.createSignature(reader, bos, '\0');
        PdfSignatureAppearance appearance = pdfStamper.getSignatureAppearance();
        for (PdfSignatureFields key : params.keySet()) {
            switch (key) {
                case LOCATION:
                    appearance.setLocation(params.get(key));
                    break;
                case REASON:
                    appearance.setReason(params.get(key));

            }
        }
        appearance.setCertificationLevel(PdfSignatureAppearance.CERTIFIED_NO_CHANGES_ALLOWED);

        ExternalDigest digest = new BouncyCastleDigest();
        ExternalSignature signature = new PrivateKeySignature(pk, DigestAlgorithms.SHA1, BouncyCastleProvider.PROVIDER_NAME);
        MakeSignature.signDetached(appearance, digest, signature, chain, null, null, null, 0, MakeSignature.CryptoStandard.CMS);

        pdfStamper.close();
        reader.close();
        return bos.toByteArray();

    } catch (IOException | GeneralSecurityException | DocumentException e) {
        throw new DocumentGenerationException(e);
    }
}
 
开发者ID:khipu,项目名称:khpdf,代码行数:33,代码来源:PdfSignerImpl.java


示例16: 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);
    }
}
 
开发者ID:mkl-public,项目名称:testarea-itext5,代码行数:67,代码来源:CreateSignature.java


示例17: preSign

import com.itextpdf.text.pdf.PdfSignatureAppearance; //导入依赖的package包/类
public DigestInfo preSign(List<DigestInfo> arg0, List<X509Certificate> certificates)
		throws NoSuchAlgorithmException {
	System.out.println("SignatureServiceImpl::preSign");
	
	HttpSession session = getSession();
	SignatureRequest request = (SignatureRequest)session.getAttribute(BeidConstants.SIGNATUREREQUEST_SESSION_NAME);
	ContentStream content = request.getDocument().getContentStream();
	
	try 
	{
		Certificate[] chain = new Certificate[certificates.size()];
		int index = 0;
		for (X509Certificate cert: certificates)
		{
			//System.out.println("CERT: "+cert);
			chain[index++] = cert;
		}
		
		// we create a reader and a stamper
		PdfReader reader = new PdfReader(content.getStream());
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		PdfStamper stamper = PdfStamper.createSignature(reader, baos, '\0');
		// we create the signature appearance
		PdfSignatureAppearance sap = stamper.getSignatureAppearance();
		
		request.fillAppearance(sap, reader);

		sap.setCertificate(chain[0]);
		// we create the signature infrastructure
		PdfSignature dic = new PdfSignature(PdfName.ADOBE_PPKLITE, PdfName.ADBE_PKCS7_DETACHED);
		dic.setReason(sap.getReason());
		dic.setLocation(sap.getLocation());
		dic.setContact(sap.getContact());
		dic.setDate(new PdfDate(sap.getSignDate()));
		sap.setCryptoDictionary(dic);
		HashMap<PdfName, Integer> exc = new HashMap<PdfName, Integer>();
		exc.put(PdfName.CONTENTS, new Integer(8192 * 2 + 2));
		sap.preClose(exc);
		ExternalDigest externalDigest = new ExternalDigest() {
			public MessageDigest getMessageDigest(String hashAlgorithm)
					throws GeneralSecurityException {
				return DigestAlgorithms.getMessageDigest(hashAlgorithm,
						null);
			}
		};
		PdfPKCS7 sgn = new PdfPKCS7(null, chain, "SHA256", null, externalDigest, false);
		InputStream data = sap.getRangeStream();
		byte hash[] = DigestAlgorithms.digest(data, externalDigest.getMessageDigest("SHA256"));
		Calendar cal = Calendar.getInstance();
		byte[] sh = sgn.getAuthenticatedAttributeBytes(hash, cal, null, null, CryptoStandard.CMS);
		sh = MessageDigest.getInstance("SHA256", "BC").digest(sh);
		
		// We store the objects we'll need for post signing in a session
		session.setAttribute(BeidConstants.SIGNATURE_SESSION_NAME, sgn);
		session.setAttribute(BeidConstants.HASH_SESSION_NAME, hash);
		session.setAttribute(BeidConstants.CAL_SESSION_NAME, cal);
		session.setAttribute(BeidConstants.SAP_SESSION_NAME, sap);
		session.setAttribute(BeidConstants.BAOS_SESSION_NAME, baos);
		DigestInfo info = new DigestInfo(sh, "SHA-256", "BeidSign");
		
		return info;
	}
	catch(Exception e)
	{
		e.printStackTrace();
	}
	
	return null;
}
 
开发者ID:sueastside,项目名称:BEIDSign,代码行数:70,代码来源:SignatureServiceImpl.java


示例18: 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;
    }
 
开发者ID:ruhr-universitaet-bochum,项目名称:jpdfsigner,代码行数:66,代码来源:ITextSigner.java



注:本文中的com.itextpdf.text.pdf.PdfSignatureAppearance类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java Description类代码示例发布时间:2022-05-22
下一篇:
Java ConnectionStateListener类代码示例发布时间:2022-05-22
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap