- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试向现有 PDF 添加交互式字段,我使用 PdfReader 和 PdfStamper 来执行此操作,当我在平板电脑上打开它时,一切正常。但是,当我想在我的计算机上打开它时,没有任何字段。
这是我的代码示例,每一页都有多个字段,但我在这里只打印了两个字段:
public class SelfNoteFragment extends Fragment {
private PdfStamper pdfStamper;
class MyCellField implements PdfPCellEvent {
protected String fieldname;
protected int page;
public MyCellField(String fieldname, int page) {
this.fieldname = fieldname;
this.page = page;
}
public void cellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases) {
final PdfWriter writer = canvases[0].getPdfWriter();
final TextField textField = new TextField(writer, rectangle, fieldname);
try {
final PdfFormField field = textField.getTextField();
pdfStamper.addAnnotation(field, page);
} catch (final IOException ioe) {
throw new ExceptionConverter(ioe);
} catch (final DocumentException de) {
throw new ExceptionConverter(de);
}
}
}
class CheckboxCellEvent implements PdfPCellEvent {
protected String name;
protected boolean check;
protected int page;
public CheckboxCellEvent(String name, boolean check, int page) {
this.check = check;
this.name = name;
this.page = page;
}
public void cellLayout(PdfPCell cell, Rectangle position,
PdfContentByte[] canvases) {
PdfWriter writer = canvases[0].getPdfWriter();
float x = position.getLeft();
float y = position.getBottom();
Rectangle rect = new Rectangle(x-5, y-5, x+5, y+5);
RadioCheckField checkbox = new RadioCheckField(
writer, rect, name, "Yes");
checkbox.setCheckType(RadioCheckField.TYPE_CROSS);
checkbox.setChecked(check);
try {
pdfStamper.addAnnotation(checkbox.getCheckField(), page);
} catch (Exception e) {
throw new ExceptionConverter(e);
}
}
}
private void createPdf(int idPrevision) throws FileNotFoundException, DocumentException {
try {
PdfReader pdfReader = new PdfReader("/storage/emulated/0/Documents/fiche_chantier2.pdf");
//Create time stamp
Date date = new Date() ;
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(date);
File pdfFolder = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOCUMENTS), "Prevision_");
if (!pdfFolder.exists()) {
pdfFolder.mkdir();
}
File myFile = new File(pdfFolder + timeStamp + ".pdf");
OutputStream output = new FileOutputStream(myFile);
this.pdfStamper = new PdfStamper(pdfReader, output);
PdfContentByte canvas1;
PdfContentByte canvas2;
canvas1 = pdfStamper.getOverContent(1);
canvas2 = pdfStamper.getOverContent(2);
PdfPCell cellFillFieldPage1 = new PdfPCell();
cellFillFieldPage1.setCellEvent(new MyCellField("", 1));
cellFillFieldPage1.setFixedHeight(15);
cellFillFieldPage1.setBorder(Rectangle.NO_BORDER);
cellFillFieldPage1.setVerticalAlignment(Element.ALIGN_MIDDLE);
PdfPCell cellCheckBoxPage2 = new PdfPCell();
cellCheckBoxPage2.setCellEvent(new CheckboxCellEvent("", false, 2));
cellCheckBoxPage2.setBorder(Rectangle.NO_BORDER);
// ************** PAGE 1 ************** //
// SET TABLE
PdfPTable tableSection1Page1 = new PdfPTable(1);
tableSection1Page1.setTotalWidth(136);
tableSection1Page1.setWidthPercentage(100.0f);
tableSection1Page1.setLockedWidth(true);
// ADD CELLS TO TABLE
tableSection1Page1.addCell(cellFillFieldPage1);
// PRINT TABLES
tableSection1Page1.writeSelectedRows(0, -1, 165, 730, canvas1);
// ************ PAGE 2 ************ //
// SET TABLES
PdfPTable tableSection1Page2 = new PdfPTable(1);
tableSection1Page2.setTotalWidth(10);
tableSection1Page2.setWidthPercentage(100.0f);
tableSection1Page2.setLockedWidth(true);
// ADD CELLS TO TABLE
tableSection1Page2.addCell(cellCheckBoxPage2);
// PRINT TABLES
tableSection1Page2.writeSelectedRows(0, -1, 182, 736, canvas2);
// I tried this, but it didn't change anything
pdfStamper.setFormFlattening(false);
pdfStamper.close();
pdfReader.close();
} catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
您知道为什么我创建的 PDF 在我的平板电脑上很好,但在我的计算机上却不好(当我想通过邮件发送它时也是如此)?如果是这样,您知道如何解决这个问题吗?
谢谢你,科朗坦
最佳答案
您的代码实际上有两个问题,
您创建名称为空的字段:
cellFillFieldPage1.setCellEvent(new MyCellField("", 1));
...
cellCheckBoxPage2.setCellEvent(new CheckboxCellEvent("", false, 2));
首先,字段需要有一个名称。此外,即使允许选择空名称,两个具有相同名称的字段也会被视为同一抽象字段的两种表示形式,如果一个是文本字段而另一个是复选框字段,则很难实现这一点。
一旦您使用非空的不同名称命名这些字段,您就会在 Adobe Reader DC/Win 上看到您的字段:您可以使用 TAB 键切换这些字段并更改它们的值。但不幸的是,它们是无国界的,因此,如果是空的,就很难被发现。这是因为
您没有为字段设置边框颜色。因此,iText 创建无边框的表单字段外观流。
您可以像这样设置边框颜色:
...
final TextField textField = new TextField(writer, rectangle, fieldname);
textField.setBorderColor(BaseColor.BLACK);
...
RadioCheckField checkbox = new RadioCheckField(writer, rect, name, "Yes");
checkbox.setBorderColor(BaseColor.BLACK);
...
完成后,您会得到带有黑色边框的字段...
如果您想知道为什么不同的观看者(即使是同一家公司的同名观众)在这里表现不同......
一些 PDF 查看器很早就意识到他们无法正确处理名称为空的字段,因此甚至不显示它们,而其他查看器稍后在尝试保存或发布表单时才意识到这一点,或者根本无法识别它,从而产生损坏的输出。
PDF 查看器可以忽略表单字段的外观流(实际上是一般注释)并创建自己的外观;因此,在当前的情况下,他们可能会忽略 iText 生成的无边框外观并绘制具有可见边框的外观。
关于java - iText - Java Android - 将字段添加到现有 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35630320/
我有一个如下图所示的情节。对于这个情节,我想在情节(右下角或左下角)的某处添加类似的线图。我正在使用的子图的命令是 plot( 1:121, sample(1:121),type='l' ) 它绘制在
我有一个单表数据库,我继承并迁移到 SQL Server,然后通过创建、链接和填充一大堆表示主表中项目的查找类型表来规范化它。我现在想用它们的外键替换原始表中的那些项目。我是不是一直在写一堆查询或 U
我有一个 Web 应用程序,它当前正在从服务器获取 PDF 的 base64 表示。我可以使用 Mozilla 的 pdf.js 在 上显示它并使用下拉菜单切换页面。 根据我所能找到的一切和Can
在 DB2 上运行的 Moodle 2 安装中,删除用户不成功,返回从数据库读取错误: Debug info: [IBM][CLI Driver][DB2/LINUXX8664] SQL0206N "
我在grails项目的RH包中添加了一个名为Authorization的新域类。 然后,我从grails菜单自动生成了 Controller 和 View 。 但是当我尝试输入 Controller
今天,我发现了一个有趣的plunker,经过谷歌大量搜索后一无所获,希望我能在这里找到答案。我只是想要那个笨蛋的副本。我不想使用复制和粘贴技术。有什么方法可以获取已建立的 plunk 的副本吗?我如何
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: Migrate normal sqlite3 database to core data? 是否可以将现有的 sql
我正在尝试在我的应用程序上添加启动画面。我干净地构建了程序,但我选择了错误的文件。现在我第二次编辑了 VM 选项并再次干净构建,现在我收到此错误: C:\Users\User\Documents\Ne
我已经查看了很多问题,我不相信这是重复使用单元格的结果,因为新的单元格图像是正确的,但是现有的单元格图像不正确并且曾经是正确的。我会先发布图片,以便更容易理解问题。 我有一个图像单元的 Collect
我在来自 Vaadin 的 ContainerHierarchicalWrapper 的这段代码中有一个非常奇怪的错误: for (Object object : children.keySet())
到目前为止,我正在使用 Globalize用于我的 JavaScript 应用程序的 i18n 和 l10n(使用 jQuery UI 构建)。这行得通,但它将我的代码与另一个特定的库联系在一起。现在
我正在创建一个 JHipster 应用程序,现在确定了 full text search 的必要性.我知道 JHipster 与 Elasticseach 集成,但我在创建项目时没有启用它。有没有一种
我一直在寻找堆栈中的建议,但我仍然不能 100% 确定改进它的最佳方法。我有一个存储大约 130K 条记录的 mysql INNODB“产品”表。杂项产品数据等大约有 80 个字段,然后我们一直在为每
我在一本书上看到,它说:当我们使用另一个初始化新创建的对象时 - 使用复制构造函数创建一个临时对象,然后使用赋值运算符将值复制到新对象! 后来在书中我读到:当使用另一个对象初始化新对象时,编译器创建一
我第一次安装现有的 Django 项目时遇到了启动服务器 python manage.py runserver 的问题 这是我做的 1.克隆仓库, 2.制作虚拟环境 3.pip安装要求.txt 4.生
我有一个网站,还有一个登录表单。我不想使用 PHP 来检查我的 MySQL 数据库,因此我正在寻找一种方法来检查用户凭据以查看是否已有 Linux 用户。我知道 PAM,但我还没有找到任何有关如何从网
我有一个现有的 Umbraco 项目在 IIS 服务器上运行。当我开始这个项目时,我基本上是将 Umbraco 直接安装到服务器上,并通过管理界面进行编码,直到网站启动并上线。 现在,客户想要一些更改
我是 Android 开发新手,目前正在学习一些教程。当我在 Eclipse 中设置一个新的 Android 项目,并选择 Windows -> Android SDK and AVD Manager
我有这个注册页面可以正常工作,但对于电子邮件字段,我需要确保电子邮件正确有效1:正确2 : 有效 为了正确添加电子邮件,我正在使用 Java 脚本验证来维护abc@def.com 很好用 但我的问题是
首先让我说我不熟悉 COM 引用,并且我在 Windows 7 64 位计算机上使用 VS2010。今天早上,我从 TFS 中删除了一个现有项目。然后我尝试构建项目并收到此错误: The type o
我是一名优秀的程序员,十分优秀!