Skip to content

创建注释

ComPDFKit 支持全类型的注释,包括便签,链接,图形,标记,图章,手绘,音频注释等,满足多样的注释需求。

注意: 使用函数CPDFPage.convertRectToPage将Android设备坐标转换为PDF页面坐标;使用函数CPDFPage.convertPointFromPage将PDF页面坐标转换为Android设备坐标

创建便签注释

便签注释表现为小型图标或标签,用户点击后可以展开显示相关注释内容,这种注释类型用于添加个人笔记、提醒或备注,使用户能够在文档中添加个性化的附加信息,而不影响原始文本的可读性。

创建便签的步骤如下:

1.获取需要创建注释的页面对象。
2.在该页面上创建便签注释对象。
3.设置注释属性。
4.更新注释外观使其显示在文档上。

创建便签的代码如下:

java
// 插入页码。
int pageNumber = 0;
// 获取pdf的页面实例。
CPDFPage page = document.pageAtIndex(pageNumber);
CPDFTextAnnotation textAnnotation = (CPDFTextAnnotation) page.addAnnot(CPDFAnnotation.Type.TEXT);
// 获取要插入的页面的实际大小。
RectF pageSize = page.getSize();
RectF insertRect = new RectF(0, 0, 50, 50);
// 坐标转换。
insertRect = page.convertRectToPage(
  false,
  pageSize.width(),
  pageSize.height(),
  insertRect);
textAnnotation.setColor(Color.RED);
textAnnotation.setAlpha(255);
textAnnotation.setRect(insertRect);
textAnnotation.setContent("ComPDFKit");
textAnnotation.updateAp();
kotlin
// 插入页码。
val pageNumber = 0
// 获取pdf的页面实例。
val page = document.pageAtIndex(pageNumber)
val textAnnotation = page.addAnnot(CPDFAnnotation.Type.TEXT) as CPDFTextAnnotation
// 获取要插入的页面的实际大小。
val pageSize = page.size
// 坐标转换。
val insertRect = page.convertRectToPage(
  false,
  pageSize.width(),
  pageSize.height(),
  RectF(0f, 0f, 50f, 50f)
)
textAnnotation.setColor(Color.RED)
textAnnotation.alpha = 255
textAnnotation.rect = insertRect
textAnnotation.content = "ComPDFKit"
textAnnotation.updateAp()

创建链接注释

链接注释使用户能够直接跳转到文档中的其他位置或外部资源,提供更丰富的导航体验。

创建链接注释的步骤如下:

  1. 获取需要创建注释的页面对象。
  2. 在该页面上创建链接注释对象。
  3. 通过CPDFDestination设置链接跳转到第2页。
  4. 设置注释属性,并将CPDFDestination对象附加到注释上。

创建链接的代码如下:

java
// 插入页码。
int pageNumber = 1;
// 获取pdf的页面实例。
CPDFPage page = document.pageAtIndex(pageNumber);
CPDFLinkAnnotation linkAnnotation = (CPDFLinkAnnotation) page.addAnnot(CPDFAnnotation.Type.LINK);
RectF pageSize = readerView.getPageNoZoomSize(pageNumber);
RectF insertRect = new RectF(0,0,100,100);
// 坐标转换。
insertRect = document.pageAtIndex(pageNumber).convertRectToPage(readerView.isCropMode(),pageSize.width(),pageSize.height(),insertRect);
linkAnnotation.setRect(insertRect);

// 跳转到指定页码
CPDFGoToAction goToAction = new CPDFGoToAction();
CPDFDestination destination = new CPDFDestination(pageNumber,0,pageSize.height(),1f);
goToAction.setDestination(document, destination);
linkAnnotation.setLinkAction(goToAction);

// 打开指定网页
CPDFUriAction uriAction = new CPDFUriAction();
uriAction.setUri("website");
linkAnnotation.setLinkAction(uriAction);
// 将注释更新到文档上。
linkAnnotation.updateAp();
kotlin
// 插入页码。
val pageNumber = 1
// 获取pdf的页面实例。
val page = document.pageAtIndex(pageNumber)
val linkAnnotation = page.addAnnot(CPDFAnnotation.Type.LINK) as CPDFLinkAnnotation
val pageSize = readerView.getPageNoZoomSize(pageNumber)
// 坐标转换。
linkAnnotation.rect = run {
  var insertRect: RectF? = RectF(0f, 0f, 100f, 100f)
  page.convertRectToPage(readerView.isCropMode, pageSize.width(), pageSize.height(), insertRect)
}

// 跳转到指定页码
linkAnnotation.linkAction = CPDFGoToAction().apply { 
  setDestination(document,  CPDFDestination(pageNumber, 0f, pageSize.height(), 1f))
}

// 打开指定网页
linkAnnotation.linkAction = CPDFUriAction().apply { 
  uri = "website"
}
linkAnnotation.updateAp()

创建文本注释

文本注释允许用户在 PDF 文档中插入自由格式的文字,用于添加注解、评论或解释文档内容。

创建文本注释的步骤如下:

1.获取需要创建注释的页面对象。
2.在该页面上创建文本注释对象。
3.设置注释属性。
4.更新注释外观使其显示在文档上。

创建文本注释的代码如下:

java
// 插入页码。
int pageNumber = 0;
// 获取pdf的页面实例。
CPDFPage page = document.pageAtIndex(pageNumber);
CPDFFreetextAnnotation freetextAnnotation = (CPDFFreetextAnnotation) page.addAnnot(CPDFAnnotation.Type.FREETEXT);
RectF pageSize = readerView.getPageNoZoomSize(pageNumber);
RectF insertRect = new RectF(0F,0F,100F,100F);
// 坐标转换。
insertRect = document.pageAtIndex(pageNumber).convertRectToPage(readerView.isCropMode(),pageSize.width(),pageSize.height(),insertRect);
freetextAnnotation.setRect(insertRect);
// 设置注释属性。
freetextAnnotation.setContent("ComPDFKit Samples");
freetextAnnotation.setFreetextAlignment(CPDFFreetextAnnotation.Alignment.ALIGNMENT_CENTER);
CPDFTextAttribute textAttribute = new CPDFTextAttribute(CPDFTextAttribute.FontNameHelper.obtainFontName(
                CPDFTextAttribute.FontNameHelper.FontType.Helvetica, false, false
        ), 12, Color.RED);
freetextAnnotation.setFreetextDa(textAttribute);
// 将注释更新到文档上。
freetextAnnotation.updateAp();
kotlin
val page = document.pageAtIndex(0)
(page.addAnnot(CPDFAnnotation.Type.FREETEXT) as CPDFFreetextAnnotation).apply {
  // 坐标转换。
	val size = page.size
  rect = page.convertRectToPage(false, size.width(), size.height(),  RectF(0f, 0f, 100f, 100f))
  // 设置注释属性。
  freetextAlignment = CPDFFreetextAnnotation.Alignment.ALIGNMENT_CENTER
  freetextDa = CPDFTextAttribute(CPDFTextAttribute.FontNameHelper.obtainFontName(
    CPDFTextAttribute.FontNameHelper.FontType.Courier, false, false
  ), 12f, Color.RED)
  alpha = 255
  content = "ComPDFKit Samples"
  // 将注释更新到文档上。
  updateAp()
}

创建图形注释

图形注释包括矩形、圆形、线条和箭头等形状,用于在文档中绘制图形以突出或标记特定区域,表达文字不易描述的信息。

创建图形注释的步骤如下:

1.获取需要创建注释的页面对象。
2.在该页面上依次创建矩形,圆形,线段注释对象。
3. 设置注释属性。
4.依次更新注释外观使其显示在文档上。

创建图形注释的代码如下:

java
// 插入页码。
int pageNumber = 0;
// 获取pdf的页面实例。
CPDFPage page = document.pageAtIndex(pageNumber);

// 在该页面上创建矩形释对象。
CPDFSquareAnnotation squareAnnotation = (CPDFSquareAnnotation) page.addAnnot(CPDFAnnotation.Type.SQUARE);
RectF pageSize = readerView.getPageNoZoomSize(pageNumber);
RectF insertRect = new RectF(0,0,100,100);
// 坐标转换。
insertRect = document.pageAtIndex(pageNumber).convertRectToPage(readerView.isCropMode(),pageSize.width(),pageSize.height(),insertRect);
squareAnnotation.setRect(insertRect);
// 设置注释属性。
squareAnnotation.setBorderColor(Color.YELLOW);
CPDFBorderStyle borderStyle = new CPDFBorderStyle(CPDFBorderStyle.Style.Border_Solid, 10, new float[]{8.0F, 0F});
borderStyle.setBorderWidth(10F);
squareAnnotation.setBorderStyle(borderStyle);
squareAnnotation.setBorderAlpha(255);
squareAnnotation.setFillColor(Color.RED);
squareAnnotation.setFillAlpha(255);
// 将注释更新到文档上。
squareAnnotation.updateAp();

// 在该页面上创建圆形注释对象。
CPDFCircleAnnotation circleAnnotation = (CPDFCircleAnnotation) page.addAnnot(CPDFAnnotation.Type.CIRCLE);
RectF pageSize = readerView.getPageNoZoomSize(pageNumber);
RectF insertRect = new RectF(0,0,100,100);
// 坐标转换。
insertRect = document.pageAtIndex(pageNumber).convertRectToPage(readerView.isCropMode(),pageSize.width(),pageSize.height(),insertRect);
circleAnnotation.setRect(insertRect);
// 设置注释属性。
circleAnnotation.setBorderColor(Color.YELLOW);
CPDFBorderStyle borderStyle = new CPDFBorderStyle(CPDFBorderStyle.Style.Border_Solid, 10, new float[]{8.0F, 0F});
borderStyle.setBorderWidth(10F);
circleAnnotation.setBorderStyle(borderStyle);
circleAnnotation.setBorderAlpha(255);
circleAnnotation.setFillColor(Color.RED);
circleAnnotation.setFillAlpha(255);
// 将注释更新到文档上。
circleAnnotation.updateAp();

// 在该页面上创建线段注释对象。
CPDFLineAnnotation lineAnnotation = (CPDFLineAnnotation) page.addAnnot(CPDFAnnotation.Type.LINE);
float lineWidth = 10f;
RectF pageSize = readerView.getPageNoZoomSize(pageNumber);
// 坐标转换。
PointF startPoint = new PointF(0,0);
PointF endPoint = new PointF(200,200);
RectF area = new RectF();
area.left = Math.min(startPoint.x, endPoint.x);
area.right = Math.max(startPoint.x, endPoint.x);
area.top = Math.min(startPoint.y, endPoint.y);
area.bottom = Math.max(startPoint.y, endPoint.y);
area.left -= lineWidth * 2;
area.top -= lineWidth * 2;
area.right += lineWidth * 2;
area.bottom += lineWidth * 2;
area.set(page.convertRectToPage(readerView.isCropMode(), pageSize.width(), pageSize.height(), area));
startPoint.set(page.convertPointToPage(readerView.isCropMode(), pageSize.width(), pageSize.height(), startPoint));
endPoint.set(page.convertPointToPage(readerView.isCropMode(), pageSize.width(), pageSize.height(), endPoint));
lineAnnotation.setRect(area);
lineAnnotation.setLinePoints(startPoint, endPoint);
// 设置注释属性。
lineAnnotation.setLineType(CPDFLineAnnotation.LineType.LINETYPE_NONE, CPDFLineAnnotation.LineType.LINETYPE_ARROW);
CPDFBorderStyle borderStyle = new CPDFBorderStyle(CPDFBorderStyle.Style.Border_Solid,lineWidth,null);
lineAnnotation.setBorderStyle(borderStyle);
lineAnnotation.setBorderWidth(10F);
lineAnnotation.setLineAlpha(255);
lineAnnotation.setLineColor(Color.RED);
// 将注释更新到文档上。
lineAnnptation.updateAp();
kotlin
// 插入页码。
val pageNumber = 0
// 获取pdf的页面实例。
val page = document.pageAtIndex(pageNumber)

// 在该页面上创建矩形释对象。
val squareAnnotation = page.addAnnot(CPDFAnnotation.Type.SQUARE) as CPDFSquareAnnotation
val pageSize = readerView.getPageNoZoomSize(pageNumber)
// 坐标转换。
squareAnnotation.rect = page.convertRectToPage(readerView.isCropMode, pageSize.width(), pageSize.height(), RectF(0F, 0F, 100F, 100F))

squareAnnotation.apply {
  // 设置注释属性。
  borderColor = Color.YELLOW
  borderStyle = CPDFBorderStyle(CPDFBorderStyle.Style.Border_Solid, 10F, floatArrayOf(8F, 0F)).apply {
    borderWidth = 10F
  }
  borderAlpha = 255
  fillColor = Color.RED
  fillAlpha = 255
}
// 将注释更新到文档上。
squareAnnotation.updateAp()

// 在该页面上创建圆形注释对象。
val circleAnnotation = page.addAnnot(CPDFAnnotation.Type.CIRCLE) as CPDFCircleAnnotation
val pageSize = readerView.getPageNoZoomSize(pageNumber)
// 坐标转换。
circleAnnotation.rect = page.convertRectToPage(readerView.isCropMode, pageSize.width(), pageSize.height(), RectF(0F, 0F, 100F, 100F))
// 设置注释属性。
circleAnnotation.borderColor = Color.YELLOW
circleAnnotation.borderStyle = CPDFBorderStyle(CPDFBorderStyle.Style.Border_Solid, 10F, floatArrayOf(8F, 0F)).apply {
  borderWidth = 10F
}
circleAnnotation.borderAlpha = 255
circleAnnotation.fillColor = Color.RED
circleAnnotation.fillAlpha = 255
// 将注释更新到文档上。
circleAnnotation.updateAp()

// 在该页面上创建线段注释对象。
val lineAnnotation = page.addAnnot(CPDFAnnotation.Type.LINE) as CPDFLineAnnotation
val lineWidth = 10F
val pageSize = readerView.getPageNoZoomSize(pageNumber)
val startPoint = PointF(0F, 0F)
val endPoint = PointF(200F, 200F)

val area = RectF().apply {
  left = min(startPoint.x, endPoint.x) - lineWidth * 2
  top = min(startPoint.y, endPoint.y) - lineWidth * 2
  right = max(startPoint.x, endPoint.x) + lineWidth * 2
  bottom = max(startPoint.y, endPoint.y) + lineWidth * 2
  set(page.convertRectToPage(readerView.isCropMode, pageSize.width(), pageSize.height(), this))
}
// 坐标转换。
listOf(startPoint, endPoint).forEach { point ->
  point.set(page.convertPointToPage(
    readerView.isCropMode, 
    pageSize.width(), 
    pageSize.height(), 
    point))
}
// 设置注释属性。
lineAnnotation.rect = area
lineAnnotation.setLinePoints(startPoint, endPoint)
lineAnnotation.setLineType(CPDFLineAnnotation.LineType.LINETYPE_NONE, CPDFLineAnnotation.LineType.LINETYPE_ARROW)
lineAnnotation.borderStyle = CPDFBorderStyle(CPDFBorderStyle.Style.Border_Solid, lineWidth, null)
lineAnnotation.borderWidth = 10F
lineAnnotation.borderAlpha = 255
lineAnnotation.borderColor = Color.RED
// 将注释更新到文档上。
lineAnnotation.updateAp();

线段注释类型枚举

名称描述
LINETYPE_UNKNOWN非标准或无效的线段端点。
LINETYPE_NONE没有线段端点。
LINETYPE_ARROW两条短线以尖锐的角度相交,形成一个开放的箭头。
LINETYPE_CLOSEDARROW两条短线以尖锐的角度相交,就像样式一样,并通过第三条线连接,形成一个三角形闭合的箭头,内部填充了注释的内部颜色。
LINETYPE_SQUARE一个填充有注释内部颜色的正方形。
LINETYPE_CIRCLE一个填充有注释内部颜色的圆形。
LINETYPE_DIAMOND一个填充有注释内部颜色的菱形形状。
LINETYPE_BUTT一条与线本身垂直的短线,位于端点上。
LINETYPE_ROPENARROW两条与相反方向的短线。
LINETYPE_RCLOSEDARROW一个与相反方向的三角形闭合箭头。
LINETYPE_SLASH一条短线,位于端点上,大约比与线本身垂直的方向顺时针旋转30度。

创建标记注释

在 PDF 文档中添加标记,以突出、强调或说明特定内容,例如重要的段落、行或单词、关键词或表格等。ComPDFKit 提供高亮,下划线,波浪线,删除线四种标记注释。

创建标记注释的步骤如下:

1.获取需要创建注释的页面对象。
2.通过页面对象创建文本对象。
3.使用该文本对象取得需要添加标记的文本位置。
4.在该页面上创建对应的标记对象。
5.设置标记对象的属性。
6.更新注释外观使其显示在文档上。

以高亮注释为例,创建标记注释的代码如下:

java
// 首先,从页面上的特定区域获取TextSelection数组,选择文本。
RectF size = readerView.getPageNoZoomSize(1);
CPDFPage pdfPage = readerView.getPDFDocument().pageAtIndex(1);
CPDFTextPage pdfTextPage = pdfPage.getTextPage();
RectF selectRect = new RectF(0f, 0f, 500f, 500f);
selectRect = pdfPage.convertRectFromPage(readerView.isCropMode(), size.width(), size.height(), selectRect);
CPDFTextSelection[] textSelectionArr = pdfTextPage.getSelectionsByLineForRect(selectRect);

// 然后,为特定区域添加高亮显示注释。
RectF annotRect = new RectF();
CPDFHighlightAnnotation highlightAnnotation = (CPDFHighlightAnnotation) pdfPage.addAnnot(CPDFAnnotation.Type.HIGHLIGHT);
highlightAnnotation.setColor(Color.YELLOW);
highlightAnnotation.setAlpha((255 / 4));
RectF[] quadRects = new RectF[textSelectionArr.length];
StringBuilder markedTextSb = new StringBuilder();
int len = textSelectionArr.length;
for (int i = 0; i < len; i++) {
  CPDFTextSelection textSelection = textSelectionArr[i];
  if (textSelection == null) {
    continue;
  }
  RectF rect = new RectF(textSelection.getRectF());
  if (annotRect.isEmpty()) {
    annotRect.set(rect);
  } else {
    annotRect.union(rect);
  }

  quadRects[i] = new RectF(textSelection.getRectF());
  String text = pdfTextPage.getText(textSelection.getTextRange());
  if (!TextUtils.isEmpty(text)) {
    markedTextSb.append(text);
  }
}

highlightAnnotation.setQuadRects(quadRects);
highlightAnnotation.setMarkedText(markedTextSb.toString());
highlightAnnotation.setRect(annotRect);
highlightAnnotation.updateAp();
kotlin
// 首先,从页面上的特定区域获取TextSelection数组,选择文本。
val size = readerView.getPageNoZoomSize(1)
val pdfPage = readerView.pdfDocument.pageAtIndex(1)
val pdfTextPage = pdfPage.textPage
val selectRect = RectF(0f, 0f, 500f, 500f)
val convertedRect = pdfPage.convertRectFromPage(readerView.isCropMode, size.width(), size.height(), selectRect)
val textSelectionArr = pdfTextPage.getSelectionsByLineForRect(convertedRect)
val highlightAnnotation = pdfPage.addAnnot(CPDFAnnotation.Type.HIGHLIGHT) as CPDFHighlightAnnotation
highlightAnnotation.color = Color.YELLOW
highlightAnnotation.alpha = 255 / 4

val quadRects = mutableListOf<RectF>()
val markedTextSb = StringBuilder()
// 然后,为特定区域添加高亮显示注释。
val annotRect = RectF()

textSelectionArr.forEach { textSelection ->
    textSelection?.let {
      val rect = RectF(it.rectF)
      if (annotRect.isEmpty) {
        annotRect.set(rect)
      } else {
        annotRect.union(rect)
      }
      quadRects.add(RectF(it.rectF))
      val text = pdfTextPage.getText(it.textRange)
      if (!TextUtils.isEmpty(text)) {
        markedTextSb.append(text)
      }
    }
}

highlightAnnotation.quadRects = quadRects.toTypedArray()
highlightAnnotation.markedText = markedTextSb.toString()
highlightAnnotation.rect = annotRect
highlightAnnotation.updateAp()

创建图章注释

图章注释用于标识和验证文档的来源和真实性,ComPDFKit 支持标准图章,文字图章,图像图章。

创建图章注释的步骤如下:

1.获取需要创建注释的页面对象。
2.在该页面上创建对应的图章。
3.设置图章属性。
4.更新注释外观使其显示在文档上。

创建图章注释的代码如下:

java
// 获取需要创建注释的页面对象。
int pageNumber = 0;
CPDFPage pdfPage = document.pageAtIndex(pageNumber);

// 创建标准印章。
CPDFStampAnnotation standard = (CPDFStampAnnotation)pdfPage.addAnnot(CPDFAnnotation.Type.STAMP);
standard.setStandardStamp(CPDFStampAnnotation.StandardStamp.COMPLETED);
RectF pageSize = readerView.getPageNoZoomSize(pageNumber);
RectF insertRect = standard.getRect();
insertRect.set(pdfPage.convertRectFromPage(readerView.isCropMode(), pageSize.width(), pageSize.height(), insertRect));
float defaultWidth = 200f;
PointF vertex = new PointF(0,0);
insertRect.set(vertex.x, vertex.y, vertex.x + defaultWidth, vertex.y + defaultWidth * Math.abs(insertRect.height() / insertRect.width()));
standard.setRect(pdfPage.convertRectToPage(readerView.isCropMode(), pageSize.width(), pageSize.height(), insertRect));
standard.updateAp();

// 创建文字印章。
CPDFStampAnnotation standard = (CPDFStampAnnotation) pdfPage.addAnnot(CPDFAnnotation.Type.STAMP);
standard.setTextStamp(new CPDFStampAnnotation.TextStamp("Hello world","2022/01/01 12:00:00", CPDFStampAnnotation.TextStampShape.TEXTSTAMP_RIGHT_TRIANGLE.id, CPDFStampAnnotation.TextStampColor.TEXTSTAMP_GREEN.id));
RectF pageSize = readerView.getPageNoZoomSize(pageNumber);
RectF insertRect = standard.getRect();
insertRect.set(pdfPage.convertRectFromPage(readerView.isCropMode(), pageSize.width(), pageSize.height(), insertRect));
float defaultWidth = 200f;
PointF vertex = new PointF(0,0);
insertRect.set(vertex.x, vertex.y, vertex.x + defaultWidth, vertex.y + defaultWidth * Math.abs(insertRect.height() / insertRect.width()));
standard.setRect(pdfPage.convertRectToPage(readerView.isCropMode(), pageSize.width(), pageSize.height(), insertRect));
standard.updateAp();

// 创建图片印章。
CPDFStampAnnotation standard = (CPDFStampAnnotation) pdfPage.addAnnot(CPDFAnnotation.Type.STAMP);
String imagePath = "imagePath";
float defaultWidth = 200f;
PointF vertex = new PointF(0,0);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imagePath, options);
RectF insertRect = new RectF(vertex.x, vertex.y, vertex.x + defaultWidth, vertex.y + defaultWidth * options.outHeight / options.outWidth);
RectF pageSize = readerView.getPageNoZoomSize(pageNumber);
insertRect.set(pdfPage.convertRectToPage(readerView.isCropMode(), pageSize.width(), pageSize.height(), insertRect));
standard.setRect(insertRect);

if (imagePath.endsWith("png")){
  BitmapFactory.Options tmpOptions = new BitmapFactory.Options();
  tmpOptions.inMutable = true;
  Bitmap bitmap = BitmapFactory.decodeFile(imagePath, tmpOptions);
  standard.updateApWithBitmap(bitmap);
  bitmap.recycle();
}else {
  standard.setImageStamp(imagePath);
  standard.updateAp();
}
kotlin
// 获取需要创建注释的页面对象。
val pageNumber = 0
val pdfPage = document.pageAtIndex(pageNumber)

// 创建标准印章。
val standardStamp = pdfPage.addAnnot(CPDFAnnotation.Type.STAMP) as CPDFStampAnnotation
standardStamp.setStandardStamp(CPDFStampAnnotation.StandardStamp.COMPLETED)
val pageSize = readerView.getPageNoZoomSize(pageNumber)
val insertRect = standardStamp.rect
insertRect.set(
  pdfPage.convertRectFromPage(readerView.isCropMode, pageSize.width(), pageSize.height(), insertRect)
)
val defaultWidth = 200F
val vertex = PointF(0f, 0F)
insertRect.set(
  vertex.x,
  vertex.y,
  vertex.x + defaultWidth,
  vertex.y + defaultWidth * kotlin.math.abs(insertRect.height() / insertRect.width())
)
standardStamp.rect = pdfPage.convertRectToPage(readerView.isCropMode, pageSize.width(), pageSize.height(), insertRect)
standardStamp.updateAp()

// 创建文字印章。
val textStamp = pdfPage.addAnnot(CPDFAnnotation.Type.STAMP) as CPDFStampAnnotation
textStamp.textStamp = CPDFStampAnnotation.TextStamp(
  "Hello world",
  "2022/01/01 12:00:00",
  CPDFStampAnnotation.TextStampShape.TEXTSTAMP_RIGHT_TRIANGLE.id,
  CPDFStampAnnotation.TextStampColor.TEXTSTAMP_GREEN.id
)
val textStampInsertRect = textStamp.rect
textStampInsertRect.set(
  pdfPage.convertRectFromPage(readerView.isCropMode, pageSize.width(), pageSize.height(), textStampInsertRect)
)
textStampInsertRect.set(
  vertex.x,
  vertex.y,
  vertex.x + defaultWidth,
  vertex.y + defaultWidth * kotlin.math.abs(textStampInsertRect.height() / textStampInsertRect.width())
)
textStamp.rect = pdfPage.convertRectToPage(readerView.isCropMode, pageSize.width(), pageSize.height(), textStampInsertRect)
textStamp.updateAp()

// 创建图片印章。
val imageStamp = pdfPage.addAnnot(CPDFAnnotation.Type.STAMP) as CPDFStampAnnotation
val imagePath = "imagePath"
val options = BitmapFactory.Options()
options.inJustDecodeBounds = true
BitmapFactory.decodeFile(imagePath, options)
val imageStampInsertRect = RectF(
  vertex.x,
  vertex.y,
  vertex.x + defaultWidth,
  vertex.y + defaultWidth * options.outHeight / options.outWidth
)
imageStamp.rect = pdfPage.convertRectToPage(readerView.isCropMode, pageSize.width(), pageSize.height(), imageStampInsertRect)

if (imagePath.endsWith("png")) {
  val tmpOptions = BitmapFactory.Options()
  tmpOptions.inMutable = true
  val bitmap = BitmapFactory.decodeFile(imagePath, tmpOptions)
  imageStamp.updateApWithBitmap(bitmap)
  bitmap.recycle()
} else {
  imageStamp.setImageStamp(imagePath)
  imageStamp.updateAp()
}

创建手绘注释

手绘注释直接快捷,用于直接绘制标注。

创建手绘注释的步骤如下:

1.获取需要创建注释的页面对象。
2.在该页面上创建手绘注释。
3.设置手绘注释经过的路径。
4.设置注释其他属性。
5.更新注释外观使其显示在文档上。

创建手绘注释的代码如下:

java
// 获取需要创建注释的页面对象。
int pageNumber = 0;
CPDFPage page = document.pageAtIndex(pageNumber);
// 设置手绘注释经过的路径。
ArrayList<ArrayList<PointF>> mDrawing = new ArrayList<>();
ArrayList<PointF> arc1 = new ArrayList<>();
arc1.add(new PointF(0,0));
arc1.add(new PointF(10,10));
arc1.add(new PointF(20,20));
mDrawing.add(arc1);
ArrayList<PointF> arc2 = new ArrayList<>();
arc2.add(new PointF(5,5));
arc2.add(new PointF(15,15));
arc2.add(new PointF(25,25));
mDrawing.add(arc2);
// 在该页面上创建手绘注释。
CPDFInkAnnotation inkAnnotation = (CPDFInkAnnotation) pdfPage.addAnnot(CPDFInkAnnotation.Type.INK);
CPDFInkAttr inkAttr = readerView.getReaderAttribute().getAnnotAttribute().getInkAttr();
float scaleValue = pageView.getScaleValue();
inkAnnotation.setColor(inkAttr.getColor());
inkAnnotation.setAlpha(inkAttr.getAlpha());
inkAnnotation.setBorderWidth(inkAttr.getBorderWidth() / scaleValue);
RectF rect = null;
RectF size = readerView.getPageNoZoomSize(pageView.getPageNum());
if (size.isEmpty()) {
	return;
}
int lineCount = mDrawing.size();
PointF[][] path = new PointF[lineCount][];
for (int lineIndex = 0; lineIndex < lineCount; lineIndex++) {
   ArrayList<PointF> line = mDrawing.get(lineIndex);
   int pointCount = line.size();
   PointF[] linePath = new PointF[pointCount];
   for (int pointIndex = 0; pointIndex < pointCount; pointIndex++) {
      PointF point = line.get(pointIndex);
      /****** 计算路径所包围的最小矩形 ******/
      if (rect == null) {
          rect = new RectF(point.x, point.y, point.x, point.y);
      } else {
          rect.union(point.x, point.y);
      }
      /****** 计算转换为页面并存储在linePath集合中的坐标点 ******/
      linePath[pointIndex] = (pdfPage.convertPointToPage(readerView.isCropMode(), size.width(), size.height(), point));
    }
    path[lineIndex] = linePath;
}
float dx = inkAttr.getBorderWidth() / scaleValue / 2;
rect.inset(-dx, -dx);
rect.set(pdfPage.convertRectToPage(readerView.isCropMode(), size.width(), size.height(), rect));
inkAnnotation.setInkPath(path);
inkAnnotation.setRect(rect);
inkAnnotation.updateAp();
kotlin
// 获取需要创建注释的页面对象。
val pageNumber = 0
val pdfPage = document.pageAtIndex(pageNumber)
// 设置手绘注释经过的路径。
val mDrawing = mutableListOf<MutableList<PointF>>()
val arc1 = mutableListOf(PointF(0f, 0f), PointF(10f, 10f), PointF(20f, 20f))
mDrawing.add(arc1)
val arc2 = mutableListOf(PointF(5f, 5f), PointF(15f, 15f), PointF(25f, 25f))
mDrawing.add(arc2)
// 在该页面上创建手绘注释。
val inkAnnotation = pdfPage.addAnnot(CPDFAnnotation.Type.INK) as CPDFInkAnnotation
val inkAttr = readerView.readerAttribute.annotAttribute.inkAttr
val scaleValue = pageView.scaleValue
inkAnnotation.color = inkAttr.color
inkAnnotation.alpha = inkAttr.alpha
inkAnnotation.borderWidth = inkAttr.borderWidth / scaleValue

var rect: RectF? = null
val size = readerView.getPageNoZoomSize(pageView.pageNum)
if (size.isEmpty) {
  return
}
val lineCount = mDrawing.size
val path = Array(lineCount) { emptyArray<PointF>() }
for (lineIndex in 0 until lineCount) {
  val line = mDrawing[lineIndex]
  val pointCount = line.size
  val linePath = Array(pointCount) { PointF() }
  for (pointIndex in 0 until pointCount) {
    val point = line[pointIndex]
    // 计算路径所包围的最小矩形。
    if (rect == null) {
      rect = RectF(point.x, point.y, point.x, point.y)
    } else {
      rect.union(point.x, point.y)
    }
    // 计算转换为页面并存储在linePath集合中的坐标点。
    linePath[pointIndex] = pdfPage.convertPointToPage(
      readerView.isCropMode,
      size.width(),
      size.height(),
      point
    )
  }
  path[lineIndex] = linePath
}
val dx = inkAttr.borderWidth / scaleValue / 2
rect?.inset(-dx, -dx)
rect?.set(pdfPage.convertRectToPage(readerView.isCropMode, size.width(), size.height(), rect))
inkAnnotation.inkPath = path
inkAnnotation.rect = rect
inkAnnotation.updateAp()

创建音频注释

创建音频注释的步骤如下:

1.获取需要创建注释的页面对象。
2.在该页上创建音频注释。
3.设置音频文件。
4.设置其他属性。
5.更新注释外观使其显示在文档上。

创建音频注释的代码如下:

java
// 获取需要创建注释的页面对象。
CPDFPage page = document.pageAtIndex(3);
CPDFSoundAnnotation soundAnnotation = (CPDFSoundAnnotation) page.addAnnot(CPDFAnnotation.Type.SOUND);
RectF insertRect = getConvertRect(page, new RectF(50, 300, 100, 350));
soundAnnotation.setRect(insertRect);
soundAnnotation.setSoundPath(FileUtils.getAssetsTempFile(SampleApplication.getInstance(), "Bird.wav"));
soundAnnotation.updateAp();
kotlin
// 获取需要创建注释的页面对象。
val page = document.pageAtIndex(3)
val soundAnnotation = page.addAnnot(CPDFAnnotation.Type.SOUND) as CPDFSoundAnnotation
val insertRect = getConvertRect(page, RectF(50F, 300F, 100F, 350F))
soundAnnotation.rect = insertRect
soundAnnotation.setSoundPath(getAssetsTempFile(context(), "Bird.wav"))
soundAnnotation.updateAp()