Java Code Examples for com.itextpdf.text.pdf.PdfStamper#getAcroFields()

The following examples show how to use com.itextpdf.text.pdf.PdfStamper#getAcroFields() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SetCheckBox.java    From testarea-itext5 with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/42819618/itext-fill-checkbox-with-value">
 * itext fill checkbox with value
 * </a>
 * <br/>
 * <a href="https://www.poreskaupravars.org/Documents/Doc/PD3100.pdf">
 * PD3100.pdf
 * </a>
 * <p>
 * This test uses a valid name and so at least creates
 * a PDF displayed with a somehow selected field. 
 * </p>
 * @see #testPd3100SetVrPr4ToNoAppend()
 */
@Test
public void testPd3100SetVrPr4ToNo() throws IOException, DocumentException
{
    try (   InputStream resource = getClass().getResourceAsStream("PD3100.pdf");
            OutputStream result = new FileOutputStream(new File(RESULT_FOLDER, "PD3100-SetVrPr4ToNo.pdf"))  )
    {
        PdfReader pdfReader = new PdfReader(resource);
        PdfStamper pdfStamper = new PdfStamper(pdfReader, result, (char)0, false);
        AcroFields acroFields = pdfStamper.getAcroFields();
        System.out.println("Available values for VrPr4: " + Arrays.asList(acroFields.getAppearanceStates("VrPr4")));
        acroFields.setField("VrPr4", "No");
        pdfStamper.close();
    }
}
 
Example 2
Source File: DuplicateHybridFormNames.java    From testarea-itext5 with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
     * <a href="http://stackoverflow.com/questions/28967737/itextsharp-setfield-for-fields-with-same-name-on-different-pages">
     * iTextSharp SetField for fields with same name on different pages
     * </a>
     * <p>
     * Reproducing the issue with iText
     * </p>
     */
    @Test
    public void testFillInForm() throws IOException, DocumentException
    {
        try (   InputStream resource = getClass().getResourceAsStream("ING_bewindvoering_regelen_tcm162-49609.pdf");
                OutputStream result = new FileOutputStream(new File(RESULT_FOLDER, "hybrid-form-fillin.pdf"))   )
        {
            PdfReader reader = new PdfReader(resource);
            PdfStamper stamper = new PdfStamper(reader, result);
            AcroFields fields = stamper.getAcroFields();
            fields.setField("topmostSubform[0].CheckBox2A[0]", "1");
//            fields.setField("topmostSubform[0].Page2[0].CheckBox2A[0]", "1");
            stamper.close();
        }
    }
 
Example 3
Source File: SetRichTextFields.java    From testarea-itext5 with GNU Affero General Public License v3.0 5 votes vote down vote up
void setFields(File result, String value) throws IOException, DocumentException
{
    try ( InputStream resource = getClass().getResourceAsStream("RichTextDoc.pdf")  )
    {
        PdfReader reader = new PdfReader(resource);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(result));
        
        AcroFields fields = stamper.getAcroFields();
        fields.setGenerateAppearances(false);
        Assert.assertFalse("Setting rich text to normal field should fail", fields.setFieldRichValue("NormalText", value));
        Assert.assertTrue("Setting rich text to rich text field should succeed", fields.setFieldRichValue("RichText", value));
        
        stamper.close();
    }
}
 
Example 4
Source File: ChangeSignatureAppearance.java    From testarea-itext5 with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/37027579/how-to-associate-a-previous-signature-in-a-new-signature-field">
 * How to associate a previous signature in a new signature field
 * </a>
 * <br/>
 * <span>BLANK-signed.pdf, <em>a blank file from elsewhere with an invisible signature.</em></span>
 * <p>
 * Quite surprisingly it turns out that changing the signature appearance is possible without
 * breaking the signature, merely a warning appears which can be hidden by simply signing again.
 * </p>
 */
@Test
public void testChangeAppearances() throws IOException, DocumentException
{
    try (   InputStream resource = getClass().getResourceAsStream("BLANK-signed.pdf");
            OutputStream result = new FileOutputStream(new File(RESULT_FOLDER, "BLANK-signed-app.pdf")))
    {
        PdfReader pdfReader = new PdfReader(resource);
        PdfStamper pdfStamper = new PdfStamper(pdfReader, result, '\0', true);

        AcroFields acroFields = pdfStamper.getAcroFields();
        for (String signatureName : acroFields.getSignatureNames())
        {
            Item field = acroFields.getFieldItem(signatureName);
            field.writeToAll(PdfName.RECT, new PdfArray(new int[]{100,100,200,200}), Item.WRITE_WIDGET);
            field.markUsed(acroFields, Item.WRITE_WIDGET);
            
            PdfAppearance appearance = PdfAppearance.createAppearance(pdfStamper.getWriter(), 100, 100);
            appearance.setColorStroke(BaseColor.RED);
            appearance.moveTo(0, 0);
            appearance.lineTo(99, 99);
            appearance.moveTo(0, 99);
            appearance.lineTo(99, 0);
            appearance.stroke();
            
            PdfDictionary appDict = new PdfDictionary();
            appDict.put(PdfName.N, appearance.getIndirectReference());
            field.writeToAll(PdfName.AP, appDict, Item.WRITE_WIDGET);
        }

        pdfStamper.close();
    }
}
 
Example 5
Source File: CreateEmptyField.java    From testarea-itext5 with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/32332490/new-signature-field-rotated-90-degrees">
 * New Signature Field Rotated 90 Degrees
 * </a>
 * <br/>
 * <a href="https://drive.google.com/open?id=0B3pKBz-WDrXnM3hYeDFtXzhldnM">
 * DA3161-Template.pdf
 * </a>
 * <p>
 * The page in your document is rotated using the Rotate page dictionary entry.
 * When creating a field to be filled-in by others later, you have to add a hint
 * to the field indicating a counter-rotation if you want the field content to
 * effectively appear upright.
 * </p>
 * <p>
 * You do this by setting the MKRotation attribute of the field. This creates a
 * rotation entry R with value 90 in the appearance characteristics dictionary
 * MK of the field.
 * </p>
 */
@Test
public void testDA3161_Template() throws IOException, GeneralSecurityException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, DocumentException
{
    try (   InputStream resource = getClass().getResourceAsStream("DA3161-Template.pdf");
            OutputStream result = new FileOutputStream(new File(RESULT_FOLDER, "DA3161-Template_Field.pdf"))    )
    {
        System.out.println("DA3161-Template.pdf");
        PdfReader reader = new PdfReader(resource);
        PdfStamper pdfStamper = new PdfStamper(reader, result);
        AcroFields fields = pdfStamper.getAcroFields();
        int itemno = 3;

      //Get location to the field where we will place the signature field
        FieldPosition NewPosition = fields.getFieldPositions("DESC_0_" + (itemno + 1)).get(0);
        float l1 = NewPosition.position.getLeft();
        float r1 = NewPosition.position.getRight();
        float t1 = NewPosition.position.getTop();
        float b1 = NewPosition.position.getBottom();

        PdfFormField field = PdfFormField.createSignature(pdfStamper.getWriter());
        field.setFieldName("G4_SignatureX");

        // Set the widget properties
        field.setWidget(new Rectangle(r1, t1, l1, b1), PdfAnnotation.HIGHLIGHT_NONE);
        field.setFlags(PdfAnnotation.FLAGS_PRINT);

        // !!!!!!!!!!!!!!!!!!!
        field.setMKRotation(90);

        // Add the annotation
        pdfStamper.addAnnotation(field, 1);

        pdfStamper.close();
    }
}
 
Example 6
Source File: AddLtvCrls.java    From testarea-itext5 with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/35134568/itext-ltv-enabled-how-to-add-more-crls">
 * iText LTV enabled - how to add more CRLs?
 * </a>
 * <p>
 * The original addLtv method of the OP modified merely to allow the
 * source PDF to be given as {@link InputStream} instead of {@link String}.
 * </p>
 */
public void addLtvJanPokorny(InputStream src, String dest) throws IOException, DocumentException, GeneralSecurityException
{
    PdfReader r = new PdfReader(src);
    FileOutputStream fos = new FileOutputStream(dest);
    PdfStamper stp = new PdfStamper(r, fos, '\0', true);
    LtvVerification v = stp.getLtvVerification();
    AcroFields fields = stp.getAcroFields();

    ArrayList<String> names = fields.getSignatureNames();
    String sigName = names.get(names.size() - 1);
    System.out.println("found signature: " + sigName);
    PdfPKCS7 pkcs7 = fields.verifySignature(sigName);

    //add LTV
    OcspClient ocsp = new OcspClientBouncyCastle();
    CrlClient crlClient1 = new CrlClientOnline("http://www.postsignum.cz/crl/psrootqca2.crl");
    ArrayList<CrlClient> crllist = new ArrayList<CrlClient>();
    crllist.add(crlClient1);
    CrlClient crlClient2 = new CrlClientOnline("http://www.postsignum.cz/crl/pspublicca2.crl");
    crllist.add(crlClient2);
    System.out.println("crllist.size=" + crllist.size());

    if (pkcs7.isTsp())
    {
        for (CrlClient crlclient : crllist)
        {
            if (v.addVerification(sigName, new OcspClientBouncyCastle(), crlclient,
                    LtvVerification.CertificateOption.SIGNING_CERTIFICATE,
                    LtvVerification.Level.CRL,
                    LtvVerification.CertificateInclusion.NO))
            {
                System.out.println("crl " + crlclient.toString() + " added to timestamp");
            }
        }
    }
    else
    {
        for (String name : names)
        {
            for (int i = 0; i < crllist.size(); i++) {
                if (v.addVerification(name, ocsp, crllist.get(i),
                        LtvVerification.CertificateOption.WHOLE_CHAIN,
                        LtvVerification.Level.CRL,
                        LtvVerification.CertificateInclusion.NO))
                {
                    System.out.println("crl " + crllist.get(i).toString() + " added to " + name);
                }
                if (i > 0)
                {
                    System.out.println("found verification, merge");
                    v.merge();
                }
            }
        }
    }
    stp.close();
}
 
Example 7
Source File: AddLtvCrls.java    From testarea-itext5 with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/35134568/itext-ltv-enabled-how-to-add-more-crls">
 * iText LTV enabled - how to add more CRLs?
 * </a>
 * <p>
 * The original addLtv method of the OP modified to allow the source PDF
 * to be given as {@link InputStream} instead of {@link String} and fixed
 * to properly use multiple CRLs.
 * </p>
 */
public void addLtvFixed(InputStream src, String dest) throws IOException, DocumentException, GeneralSecurityException
{
    PdfReader r = new PdfReader(src);
    FileOutputStream fos = new FileOutputStream(dest);
    PdfStamper stp = new PdfStamper(r, fos, '\0', true);
    LtvVerification v = stp.getLtvVerification();
    AcroFields fields = stp.getAcroFields();

    ArrayList<String> names = fields.getSignatureNames();
    String sigName = names.get(names.size() - 1);
    System.out.println("found signature: " + sigName);
    PdfPKCS7 pkcs7 = fields.verifySignature(sigName);

    //add LTV
    OcspClient ocsp = new OcspClientBouncyCastle();
    CrlClient crlClient = new CrlClientOnline("http://www.postsignum.cz/crl/psrootqca2.crl", "http://www.postsignum.cz/crl/pspublicca2.crl");

    if (pkcs7.isTsp())
    {
        if (v.addVerification(sigName, new OcspClientBouncyCastle(), crlClient,
                LtvVerification.CertificateOption.SIGNING_CERTIFICATE,
                LtvVerification.Level.CRL,
                LtvVerification.CertificateInclusion.NO))
        {
            System.out.println("crl " + crlClient.toString() + " added to timestamp");
        }
    }
    else
    {
        for (String name : names)
        {
            if (v.addVerification(name, ocsp, crlClient,
                    LtvVerification.CertificateOption.WHOLE_CHAIN,
                    LtvVerification.Level.CRL,
                    LtvVerification.CertificateInclusion.NO))
            {
                System.out.println("crl " + crlClient.toString() + " added to " + name);
            }
        }
    }
    stp.close();
}
 
Example 8
Source File: SetCheckBox.java    From testarea-itext5 with GNU Affero General Public License v3.0 3 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/42819618/itext-fill-checkbox-with-value">
 * itext fill checkbox with value
 * </a>
 * <br/>
 * <a href="https://www.poreskaupravars.org/Documents/Doc/PD3100.pdf">
 * PD3100.pdf
 * </a>
 * <p>
 * This test uses a valid name in append mode and so creates
 * a PDF displayed in Adobe Reader with a selected field as
 * desired. 
 * </p>
 * @see #testPd3100SetVrPr4ToNoAppend()
 */
@Test
public void testPd3100SetVrPr4ToNoAppend() throws IOException, DocumentException
{
    try (   InputStream resource = getClass().getResourceAsStream("PD3100.pdf");
            OutputStream result = new FileOutputStream(new File(RESULT_FOLDER, "PD3100-SetVrPr4ToNoAppend.pdf"))  )
    {
        PdfReader pdfReader = new PdfReader(resource);
        PdfStamper pdfStamper = new PdfStamper(pdfReader, result, (char)0, true);
        AcroFields acroFields = pdfStamper.getAcroFields();
        System.out.println("Available values for VrPr4: " + Arrays.asList(acroFields.getAppearanceStates("VrPr4")));
        acroFields.setField("VrPr4", "No");
        pdfStamper.close();
    }
}