【问题标题】:No text when printing PDF created with iText 7打印使用 iText 7 创建的 PDF 时没有文本
【发布时间】:2017-05-16 09:25:21
【问题描述】:

各位开发者您好,

打印使用 iText 7 使用 Java 应用程序自动生成的 PDF 时遇到问题。打印此类 PDF 时,打印输出包含所有图片和图形,但没有任何文本。

谁能告诉我可能是什么问题?我在 Adob​​e 中尝试了“打印为图像”选项并得出了相同的结果。

非常感谢。

编辑/添加代码和链接:

Link to PDF file created this way

document = new Document(new PdfDocument(new PdfWriter(new FileOutputStream(dest))));        
this.form = PdfAcroForm.getAcroForm(document.getPdfDocument(), true);
PdfTextFormField fw1Field = PdfTextFormField.createText(document.getPdfDocument(),
                        new Rectangle(Variables.llx, Variables.lly, Variables.urx, Variables.ury), "Feld1");
fw1Field.setValue(fw1);
fw1Field.setReadOnly(Variables.readonly);
fw1Field.setBorderColor(Color.WHITE);
form.addField(fw1Field);

PdfTextFormField fsText = PdfTextFormField.createText(document.getPdfDocument(),
                        new Rectangle(Variables.llx + 150, Variables.lly, Variables.urx  + 50, Variables.ury), "FSText");
fsText.setValue("Freigabeschein:");
fsText.setBackgroundColor(Variables.backgroundColourText);
fsText.setBorderColor(Color.WHITE);
fsText.setReadOnly(Variables.readonlyText);
fsText.setBorderColor(Color.WHITE);
form.addField(fsText);

PdfTextFormField idField = PdfTextFormField.createText(document.getPdfDocument(),
                        new Rectangle(Variables.llx + 250, Variables.lly, Variables.urx, Variables.ury), "Freigabeschein Nummer");
idField.setValue(id);
idField.setReadOnly(Variables.readonly);
idField.setBorderColor(Color.WHITE);
form.addField(idField);

PdfTextFormField datumText = PdfTextFormField.createText(document.getPdfDocument(),
new Rectangle(Variables.llx + 350, Variables.lly, Variables.urx, Variables.ury), "DatumText");
datumText.setValue("Datum:");
datumText.setBackgroundColor(Variables.backgroundColourText);
datumText.setBorderColor(Color.WHITE);
datumText.setReadOnly(Variables.readonlyText);
form.addField(datumText);

//more Text, created exactly as above

PdfButtonFormField buttonSpeichern = PdfFormField.createPushButton(document.getPdfDocument(), new Rectangle(450, 20, 100, 30), "speichern", "SPEICHERN");
buttonSpeichern.setBackgroundColor(Color.LIGHT_GRAY);
buttonSpeichern.setValue("Speichern");
buttonSpeichern.setVisibility(PdfFormField.VISIBLE_BUT_DOES_NOT_PRINT);
buttonSpeichern.setAdditionalAction(PdfName.D, PdfAction.createJavaScript("saveFSFunction(1);"));
form.addField(buttonSpeichern);

PdfButtonFormField buttonDrucken = PdfFormField.createPushButton(document.getPdfDocument(), new Rectangle(300, 20, 100, 30), "drucken", "DRUCKEN");
buttonDrucken.setBackgroundColor(Color.LIGHT_GRAY);
buttonDrucken.setValue("Drucken");                                             
buttonDrucken.setVisibility(PdfFormField.VISIBLE_BUT_DOES_NOT_PRINT);
buttonDrucken.setAdditionalAction(PdfName.D, PdfAction.createJavaScript("printFunction(1, 0, 1, \"FS\");"));
form.addField(buttonDrucken);

document.close();

【问题讨论】:

  • 请分享 A) 用于创建 PDF 的代码的关键部分 B) 重现该问题的示例 PDF。
  • 感谢您的回复,我添加了代码 sn-p 链接到带有问题的示例 PDF。
  • 您是否检查过您是否将 Acrobat 设置为打印表单域?要么,要么您的文本字段都将其可见性设置为可见,但不打印。虽然,我不认为这是使用 iText 创建文本字段时的默认设置,而且您似乎没有在此处共享的代码中进行设置...
  • @SamuelHuylebroeck,非常感谢。我必须设置所有字段 .setVisibility(0); 没有可见的枚举,所以我认为这是标准行为 - 显然不是。
  • @Harry 下一版本中会有一个。我记得前段时间在 slackoverflow 上遇到过类似的问题,并为它添加了一个枚举。仅供参考,这里的数字 0 没有什么特别之处,其他可见性选项映射为 1、2 和 3,并且方法中的默认 switch 语句设置为可见和打印设置。

标签: java pdf adobe itext7


【解决方案1】:

使用iText7对问题及其解决方案的简要说明:

表单字段小部件的可见性(即您将在查看器或打印中看到的表示形式)由存储在小部件字典的 /F 条目中的无符号 32 位整数控制。此条目是可选的,默认值为 0。 整数被解释为一系列标志,其含义基于它们的位置,从低到高。 位置 3 的标志控制小部件的打印行为。如果设置为 0,则永远不会打印小部件。

在 iText7 中,创建 textField 注解时,/F 条目不会自动添加到小部件字典中,除非使用 PdfTextFormField.setVisibility() 显式设置值,因此此处的默认行为将大致对应于 VISIBLE_BUT_DO_NOT_PRINT

在 7.0.2 和更早的版本中,没有可见和打印的关键字,但行为被实现为 PdfTextFormField.setVisibility() 的默认情况,使用除 1,2 或 3 之外的任何数字调用方法,将导致这种行为。 (1、2 和 3 分别映射到 HIDDENVISIBLE_BUT_DO_NOT_PRINTHIDDEN_BUT_PRINTABLE)。

在 7.0.3(撰写本文时仍在开发中)及更高版本中,为方便起见并避免混淆,添加了关键字 VISIBLE。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-04
    • 2019-03-03
    • 2012-11-03
    • 2021-05-06
    • 1970-01-01
    • 2016-03-06
    • 2016-11-20
    • 1970-01-01
    相关资源
    最近更新 更多