【问题标题】:iText 5 PdfAction.javaScript() not working in Adobe Acrobat after adding PushbuttonField添加 PushbuttonField 后 iText 5 PdfAction.javaScript() 在 Adob​​e Acrobat 中不起作用
【发布时间】:2019-10-14 09:51:49
【问题描述】:
  • 我正在使用 iText (v - 5.5.13)Java 8按钮添加到 PDF 文件中
  • 使用此按钮,我将操作设置为 Button,它是一个 JavaScript 警报
  • 按钮添加成功,但是当我点击 PDF 中的按钮时,按钮的操作没有被调用,这意味着 JavaScript 警报不起作用
  • 如果我将 removeXfa() 添加到 AcroFields 则此工作正常,但 PDF 表单字段的验证不起作用,这就是我无法删除 XFA 的原因
  • 看起来这在 Adob​​e Acrobat 中不起作用,但在 Foxit Reader 中却完美运行
  • 其实我想要API调用而不是alert,但是这里alerts不是调用

这是我的 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();
    }
}

这是我的 build.gradle 文件


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'
}


【问题讨论】:

  • 就第一个问题而言,这个问题并不完美,但我仍然会给它 8/10 分,因为你描述了你想做什么,你尝试了什么,什么没用你添加了代码。如果每个人都像您一样,那么 Stack Overflow 将是一个更好的网站。所以我赞成你的问题。
  • 一个细节,与您的问题无关:iText 5.5.13 依赖于 BouncyCastle 1.49。无法保证与 BouncyCastle 1.52 的兼容性。
  • 我更改了 BouncyCastle 版本并尝试了它,但警报仍然不起作用。您还有什么建议吗? @AmedeeVanGasse
  • 就像我写的那样,BouncyCastle 不兼容与您的问题无关。我个人对你没有答案,希望其他人可以提供帮助。

标签: javascript java itext adobe acrobat


【解决方案1】:

确保您安装的 Adob​​e 已启用“启用 Acrobat JavaScript”。

当您打开有问题的文件时,只需转到 Edit => Preferences => JavaScript 并确保在其中勾选正确的选项..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-29
    • 1970-01-01
    • 1970-01-01
    • 2019-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多