1
votes
  • Here i'm doing add push button into PDF file using iText (v - 5.5.13) and Java 8.
  • With this button I set action to Button that is one JavaScript alert
  • Button added successfully but action of button is not call when I click on button in PDF, means JavaScript alert is not working.
  • If I add removeXfa() to AcroFields then this working but validation of PDF form field is not working thats why I can't remove XFA
  • Look like that this not work in Adobe Acrobat but in Foxit Reader this works perfectly
  • Actually I want API call instead of alert, but here alerts is not call

Here is my code in Java

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.GrayColor;
import com.itextpdf.text.pdf.PdfAction;
import com.itextpdf.text.pdf.PdfAnnotation;
import com.itextpdf.text.pdf.PdfFormField;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PushbuttonField;

public class AddField {

    public static final String SRC = "E:/projects/PDF_Manipulation/SRC/abc.pdf";
    public static final String DEST = "E:/projects/PDF_Manipulation/DEST/abc-added-button.pdf";

    public static void main(String[] args) throws DocumentException, IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new AddField().manipulatePdf(SRC, DEST);
    }

    public void manipulatePdf(String src, String dest) throws DocumentException, IOException {

        PdfReader reader = new PdfReader(src);
        PdfReader.unethicalreading = true;
        reader.removeUsageRights();

        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));

        /* If I add removeXfa() to AcroFields then this working but validation of PDF form field is not working thats why i can't remove XFA */
        /* final AcroFields formx = stamper.getAcroFields();
        formx.removeXfa();
        formx.setGenerateAppearances(true); */

        PushbuttonField button = new PushbuttonField(stamper.getWriter(), new Rectangle(36, 700, 72, 730), "SAVE");
        button.setText("SAVE");
        button.setBackgroundColor(new GrayColor(0.75f));
        button.setVisibility(PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT);

        PdfFormField submit = button.getField();
        Rectangle rect = new Rectangle(550, 5, 580, 20);
        submit.setWidget(rect, PdfAnnotation.HIGHLIGHT_INVERT);

        String exportPdfJavascript = "app.alert('Testing........')";
        submit.setAction(PdfAction.javaScript(exportPdfJavascript, stamper.getWriter()));

        stamper.addAnnotation(submit, 1);

        stamper.close();
    }
}

Here is my build.gradle file


apply plugin: 'java-library'

repositories {
    jcenter()
}

dependencies {
    // https://mvnrepository.com/artifact/com.itextpdf/itext7-core
    compile group: 'com.itextpdf', name: 'itextpdf', version: '5.5.13'

    // https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk15on
    compile group: 'org.bouncycastle', name: 'bcprov-jdk15on', version: '1.52'

    // https://mvnrepository.com/artifact/net.sf.dozer/dozer
    compile group: 'net.sf.dozer', name: 'dozer', version: '5.5.1'

    compile group: 'com', name: 'itextpdf', version: 'pdfxfa'
}


1
As far as first questions go, this one isn't perfect, but I would still score it an 8/10 because you describe what you want to do, what you tried, what didn't work and you added code. If everyone would be like you then Stack Overflow would be a much better site. So I'm upvoting your question. - Amedee Van Gasse
One detail, unrelated to your question: iText 5.5.13 has a dependency on BouncyCastle 1.49. Compatibility with BouncyCastle 1.52 cannot be guaranteed. - Amedee Van Gasse
I changed BouncyCastle version and try it but alert still not working.can you have any more suggestion ? @AmedeeVanGasse - Harsh Patel
Like I wrote, the BouncyCastle incompatibility was unrelated to your question. I personally have no answer for you, I hope that someone else can help. - Amedee Van Gasse

1 Answers

0
votes

Make sure that your install of Adobe has "Enable Acrobat JavaScript" enabled.

When you have the problematic file opened simply go to Edit => Preferences => JavaScript and ensure that the correct options are ticked in there..