Skip to content
ComPDF
Guides

编辑表单域

获取并编辑已添加表单域的外观和内容。

需要注意,不同的表单域类型,允许设置的属性可能不完全一致。

编辑表单域的步骤如下:

  1. 取得需要编辑的表单域对象。

  2. 修改表单域属性。

  3. 更新表单域外观。

以下是编辑表单域的代码:

java
CPDFTextWidget textWidget = (CPDFTextWidget) cpdfWidget;
textWidget.setFillColor(Color.RED);
textWidget.updateAp();
kotlin
val textWidget = myCPDFWidget as CPDFTextWidget
textWidget.setFillColor(Color.RED)
textWidget.updateAp()

ReaderView 同步

如果当前 PDF 正在通过 CPDFReaderView 显示,并且目标页已加载或正在屏幕中显示, 需要使用CPDFPageView刷新对应的表单外观。

仅已显示或即将显示的页面才会存在 CPDFPageView,请务必判空。

java
CPDFPageView pageView = (CPDFPageView) readerView.getChild(pageIndex);

if (pageView != null) {
  CPDFAnnotImpl annotationImpl = pageView.getAnnotImpl(textWidget);
  annotationImpl.onAnnotAttrChange();
  pageView.invalidate();
}
kotlin
val pageView = readerView.getChild(pageIndex) as? CPDFPageView

pageView?.let { view ->
    val annotationImpl = view.getAnnotImpl(annotation)
    annotationImpl.onAnnotAttrChange()
    view.invalidate()
}