Create Annotations
ComPDFKit supports a wide range of annotation types, including notes, links, shapes, highlights, stamps, freehand drawings, and audio annotations, catering to diverse annotation requirements.
Create Note Annotations
Note annotations appear as small icons or labels. When clicked by the user, they can expand to display relevant annotation content. This annotation type is used for adding personal notes, reminders, or comments, allowing users to add personalized additional information to the document without affecting the readability of the original text.
The steps to create a note are as follows:
Obtain the page object where the annotation needs to be created.
Create a note annotation object on that page.
Set the annotation properties.
Update the annotation appearance to display it on the document.
This example shows how to create note annotations:
// Insert page numbers.
int pageNumber = 0;
// Retrieve an instance of the PDF page.
CPDFPage page = document.pageAtIndex(pageNumber);
CPDFTextAnnotation textAnnotation = (CPDFTextAnnotation) page.addAnnot(CPDFAnnotation.Type.TEXT);
// Retrieve the actual size of the page to be inserted.
RectF pageSize = page.getSize();
RectF insertRect = new RectF(0, 0, 50, 50);
// Coordinate transformation.
insertRect = page.convertRectToPage(
false,
pageSize.width(),
pageSize.height(),
insertRect);
textAnnotation.setColor(Color.RED);
textAnnotation.setAlpha(255);
textAnnotation.setRect(insertRect);
textAnnotation.setContent("ComPDFKit");
textAnnotation.updateAp();
// Insert page numbers.
val pageNumber = 0
// Retrieve an instance of the PDF page.
val page = document.pageAtIndex(pageNumber)
val textAnnotation = page.addAnnot(CPDFAnnotation.Type.TEXT) as CPDFTextAnnotation
// Retrieve the actual size of the page to be inserted.
val pageSize = page.size
// Coordinate transformation.
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()
Create Link Annotations
Link annotations allow users to navigate directly to other locations within the document or external resources, enhancing the navigation experience.
The steps to create link annotations are as follows:
- Obtain the page object where the annotation needs to be created.
- Create a link annotation object on that page.
- Use
CPDFDestination
to specify the destination page, for example, jumping to page 2. - Set the annotation properties and attach the
CPDFDestination
object to the annotation.
This example shows how to create link annotations:
// Insert page numbers.
int pageNumber = 0;
// // Retrieve an instance of the PDF page.
CPDFPage page = document.pageAtIndex(pageNumber);
CPDFLinkAnnotation linkAnnotation = (CPDFLinkAnnotation) page.addAnnot(CPDFAnnotation.Type.LINK);
RectF pageSize = page.getSize();
RectF insertRect = new RectF(0,0,100,100);
// Coordinate transformation.
insertRect = page.convertRectToPage(false , pageSize.width(), pageSize.height(), insertRect);
linkAnnotation.setRect(insertRect);
// Navigate to a specified page number.
CPDFGoToAction goToAction = new CPDFGoToAction();
CPDFDestination destination = new CPDFDestination(pageNumber,0,pageSize.height(),1f);
goToAction.setDestination(document, destination);
linkAnnotation.setLinkAction(goToAction);
// Open a specific webpage.
CPDFUriAction uriAction = new CPDFUriAction();
uriAction.setUri("website");
linkAnnotation.setLinkAction(uriAction);
// Update the annotations onto the document.
linkAnnotation.updateAp();
// Insert page numbers.
val pageNumber = 0
// Retrieve an instance of the PDF page.
val page = document.pageAtIndex(pageNumber)
val linkAnnotation = page.addAnnot(CPDFAnnotation.Type.LINK) as CPDFLinkAnnotation
val pageSize = page.size
// Coordinate transformation.
linkAnnotation.rect = run {
var insertRect: RectF? = RectF(0F, 0F, 100F, 100F)
page.convertRectToPage(false, pageSize.width(), pageSize.height(), insertRect)
}
// Navigate to a specified page number.
linkAnnotation.linkAction = CPDFGoToAction().apply {
setDestination(document, CPDFDestination(pageNumber, 0F, pageSize.height(), 1F))
}
// Open a specific webpage.
linkAnnotation.linkAction = CPDFUriAction().apply {
uri = "website"
}
// Update the annotations onto the document.
linkAnnotation.updateAp()
Create Free Text Annotations
Free text annotations enable users to insert free-form text into PDF documents, serving the purpose of adding annotations, comments, or explanations to document content.
The steps to create a free text annotation are as follows:
Obtain the page object where the annotation needs to be created.
Create a free text annotation object on that page.
Set the annotation properties.
Update the annotation appearance to display it on the document.
This example shows how to create free text annotations:
// Insert page numbers.
int pageNumber = 0;
// Retrieve an instance of the PDF page.
CPDFPage page = document.pageAtIndex(pageNumber);
CPDFFreetextAnnotation freetextAnnotation = (CPDFFreetextAnnotation) page.addAnnot(CPDFAnnotation.Type.FREETEXT);
RectF pageSize = page.getSize();
RectF insertRect = new RectF(0F,0F,100F,100F);
// Coordinate transformation.
insertRect = page.convertRectToPage(false,pageSize.width(),pageSize.height(),insertRect);
freetextAnnotation.setRect(insertRect);
// Set annotation properties
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);
// Update the annotations onto the document.
freetextAnnotation.updateAp();
// Insert page numbers.
val pageNumber = 0
// Retrieve an instance of the PDF page.
val page = document.pageAtIndex(pageNumber)
(page.addAnnot(CPDFAnnotation.Type.FREETEXT) as CPDFFreetextAnnotation).apply {
// Coordinate transformation.
val size = page.size
rect = page.convertRectToPage(false, size.width(), size.height(), RectF(0f, 0f, 100f, 100f))
// Set annotation properties
freetextAlignment = CPDFFreetextAnnotation.Alignment.ALIGNMENT_CENTER
freetextDa = CPDFTextAttribute(CPDFTextAttribute.FontNameHelper.obtainFontName(
CPDFTextAttribute.FontNameHelper.FontType.Courier, false, false
), 12f, Color.RED)
alpha = 255
content = "ComPDFKit Samples"
// Update the annotations onto the document.
updateAp()
}
Create Shape Annotations
Graphic annotations encompass shapes such as rectangles, circles, lines, and arrows, used to draw attention to or highlight specific areas in a document, conveying information that may be challenging to describe with text alone.
The steps to create graphic annotations are as follows:
Obtain the page object where the annotations need to be created.
Sequentially create rectangle, circle, and line annotations on that page.
Set the annotation properties.
Sequentially update the annotation appearance to display them on the document.
This example shows how to create shape annotations:
// Insert page numbers.
int pageNumber = 0;
// Retrieve an instance of the PDF page.
CPDFPage page = document.pageAtIndex(pageNumber);
// Create a rectangle annotation object on the page.
CPDFSquareAnnotation squareAnnotation = (CPDFSquareAnnotation) page.addAnnot(CPDFAnnotation.Type.SQUARE);
RectF pageSize = page.getSize();
RectF insertRect = new RectF(0,0,100,100);
// Coordinate transformation.
insertRect = page.convertRectToPage(false,pageSize.width(),pageSize.height(),insertRect);
squareAnnotation.setRect(insertRect);
// Set annotation properties.
squareAnnotation.setBorderColor(Color.YELLOW);
CPDFBorderStyle borderStyle = new CPDFBorderStyle(CPDFBorderStyle.Style.Border_Solid, 10, new float[]{8.0F, 0F});
squareAnnotation.setBorderStyle(borderStyle);
squareAnnotation.setBorderAlpha(255);
squareAnnotation.setFillColor(Color.RED);
squareAnnotation.setFillAlpha(255);
// Update the annotations onto the document.
squareAnnotation.updateAp();
// Create a circular annotation object on the page.
CPDFCircleAnnotation circleAnnotation = (CPDFCircleAnnotation) page.addAnnot(CPDFAnnotation.Type.CIRCLE);
RectF pageSize = page.getSize();
RectF insertRect = new RectF(0,0,100,100);
// Coordinate transformation.
insertRect = page.convertRectToPage(false,pageSize.width(),pageSize.height(),insertRect);
circleAnnotation.setRect(insertRect);
// Set annotation properties.
circleAnnotation.setBorderColor(Color.YELLOW);
CPDFBorderStyle borderStyle = new CPDFBorderStyle(CPDFBorderStyle.Style.Border_Solid, 10, new float[]{8.0F, 0F});
circleAnnotation.setBorderStyle(borderStyle);
circleAnnotation.setBorderAlpha(255);
circleAnnotation.setFillColor(Color.RED);
circleAnnotation.setFillAlpha(255);
// Update the annotations onto the document.
circleAnnotation.updateAp();
// Create a line annotation object on the page.
CPDFLineAnnotation lineAnnotation = (CPDFLineAnnotation) page.addAnnot(CPDFAnnotation.Type.LINE);
float lineWidth = 10f;
RectF pageSize = page.getSize();
// Coordinate transformation.
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(false, pageSize.width(), pageSize.height(), area));
startPoint.set(page.convertPointToPage(false, pageSize.width(), pageSize.height(), startPoint));
endPoint.set(page.convertPointToPage(false, pageSize.width(), pageSize.height(), endPoint));
lineAnnotation.setRect(area);
lineAnnotation.setLinePoints(startPoint, endPoint);
// Set annotation properties.
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.setBorderAlpha(255);
lineAnnotation.setBorderColor(Color.RED);
// Update the annotations onto the document.
lineAnnotation.updateAp();
// Insert page numbers.
val pageNumber = 0
// Retrieve an instance of the PDF page.
val page = document.pageAtIndex(pageNumber)
// Create a rectangle annotation object on the page.
val squareAnnotation = page.addAnnot(CPDFAnnotation.Type.SQUARE) as CPDFSquareAnnotation
val pageSize = page.size
// Coordinate transformation.
squareAnnotation.rect = page.convertRectToPage(false, pageSize.width(), pageSize.height(), RectF(0F, 0F, 100F, 100F))
squareAnnotation.apply {
// Set annotation properties.
borderColor = Color.YELLOW
borderStyle = CPDFBorderStyle(CPDFBorderStyle.Style.Border_Solid, 10F, floatArrayOf(8F, 0F))
borderAlpha = 255
fillColor = Color.RED
fillAlpha = 255
}
// Update the annotations onto the document.
squareAnnotation.updateAp()
// Create a circular annotation object on the page.
val circleAnnotation = page.addAnnot(CPDFAnnotation.Type.CIRCLE) as CPDFCircleAnnotation
val pageSize = page.size
// Coordinate transformation.
circleAnnotation.rect = page.convertRectToPage(false, pageSize.width(), pageSize.height(), RectF(0F, 0F, 100F, 100F))
// Set annotation properties.
circleAnnotation.borderColor = Color.YELLOW
circleAnnotation.borderStyle = CPDFBorderStyle(CPDFBorderStyle.Style.Border_Solid, 10F, floatArrayOf(8F, 0F))
circleAnnotation.borderAlpha = 255
circleAnnotation.fillColor = Color.RED
circleAnnotation.fillAlpha = 255
// Update the annotations onto the document.
circleAnnotation.updateAp()
// Create a line annotation object on the page.
val lineAnnotation = page.addAnnot(CPDFAnnotation.Type.LINE) as CPDFLineAnnotation
val lineWidth = 10F
val pageSize = page.size
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(false, pageSize.width(), pageSize.height(), this))
}
// Coordinate transformation.
listOf(startPoint, endPoint).forEach {
point ->
point.set(page.convertPointToPage(
false,
pageSize.width(),
pageSize.height(),
point))
}
// Set annotation properties.
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.borderAlpha = 255
lineAnnotation.borderColor = Color.RED
// Update the annotations onto the document.
lineAnnotation.updateAp()
Explanation of Line Types
Name | Description |
---|---|
LINETYPE_UNKNOWN | Non-standard or invalid line segment endpoint. |
LINETYPE_NONE | No line segment endpoint. |
LINETYPE_ARROW | Two short lines intersect at a sharp angle, forming an open arrow. |
LINETYPE_CLOSEDARROW | Two short lines intersect at a sharp angle, similar to style one, and are connected by a third line, forming a triangular closed arrow filled with the interior color of the annotation. |
LINETYPE_SQUARE | A square filled with the interior color of the annotation. |
LINETYPE_CIRCLE | A circle filled with the interior color of the annotation. |
LINETYPE_DIAMOND | A diamond shape filled with the interior color of the annotation. |
LINETYPE_BUTT | A short line perpendicular to the line itself, located at the endpoint. |
LINETYPE_ROPENARROW | Two short lines in opposite directions. |
LINETYPE_RCLOSEDARROW | A triangular closed arrow in opposite directions. |
LINETYPE_SLASH | A short line, located at the endpoint, rotated approximately 30 degrees clockwise from the direction perpendicular to the line itself. |
Create Markup Annotations
Incorporate annotations in a PDF document to highlight, emphasize, or explain specific content, such as important paragraphs, lines, words, keywords, tables, etc. ComPDFKit provides four types of markup annotations: highlight, underline, squiggly line, and strikethrough.
The steps to create a markup annotation are as follows:
Obtain the page object where the annotation needs to be created.
Create a text object through the page object.
Use the text object to identify the location of the text to be marked.
Create the corresponding markup object on that page.
Set the properties of the markup object.
Update the annotation appearance to display it on the document.
This example shows how to create markup annotations:
// First, retrieve a TextSelection array from a specific region on the page to select text.
CPDFPage pdfPage = document.pageAtIndex(0);
RectF size = pdfPage.getSize();
CPDFTextPage pdfTextPage = pdfPage.getTextPage();
RectF selectRect = new RectF(0f, 0f, 500f, 500f);
selectRect = pdfPage.convertRectFromPage(false, size.width(), size.height(), selectRect);
CPDFTextSelection[] textSelectionArr = pdfTextPage.getSelectionsByLineForRect(selectRect);
// Then, add a highlight annotation to the specific region for highlighting.
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();
// First, retrieve a TextSelection array from a specific region on the page to select text.
val page = document.pageAtIndex(0)
val size = page.size
val pdfTextPage = page.textPage
val selectRect = RectF(0f, 0f, 500f, 500f)
val convertedRect = page.convertRectFromPage(false, size.width(), size.height(), selectRect)
val textSelectionArr = pdfTextPage.getSelectionsByLineForRect(convertedRect)
val highlightAnnotation = page.addAnnot(CPDFAnnotation.Type.HIGHLIGHT) as CPDFHighlightAnnotation
highlightAnnotation.color = Color.YELLOW
highlightAnnotation.alpha = 255 / 4
val quadRects = mutableListOf<RectF>()
val markedTextSb = StringBuilder()
// Then, add a highlight annotation to the specific region for highlighting.
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()
Create Stamp Annotations
Stamp annotations are used to identify and validate the source and authenticity of a document. ComPDFKit supports standard stamps, text stamps, and image stamps.
The steps to create a stamp annotation are as follows:
Obtain the page object where the annotation needs to be created.
Create the corresponding stamp on that page.
Set the properties of the stamp.
Update the annotation appearance to display it on the document.
This example shows how to create stamp annotations:
// Retrieve the page object for creating annotations.
int pageNumber = 0;
CPDFPage page = document.pageAtIndex(pageNumber);
// Create a standard stamp.
CPDFStampAnnotation standardStamp = (CPDFStampAnnotation)page.addAnnot(CPDFAnnotation.Type.STAMP);
standardStamp.setStandardStamp(CPDFStampAnnotation.StandardStamp.COMPLETED);
RectF pageSize = page.getSize();
RectF insertRect = standardStamp.getRect();
insertRect.set(page.convertRectFromPage(false, 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()));
standardStamp.setRect(page.convertRectToPage(false, pageSize.width(), pageSize.height(), insertRect));
standardStamp.updateAp();
// Create a text stamp.
CPDFStampAnnotation textStamp = (CPDFStampAnnotation) page.addAnnot(CPDFAnnotation.Type.STAMP);
textStamp.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 = page.getSize();
RectF insertRect = textStamp.getRect();
insertRect.set(page.convertRectFromPage(false, 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()));
textStamp.setRect(page.convertRectToPage(false, pageSize.width(), pageSize.height(), insertRect));
textStamp.updateAp();
// Create an image stamp.
CPDFStampAnnotation imageStamp = (CPDFStampAnnotation) page.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 =page.getSize();
insertRect.set(page.convertRectToPage(false, pageSize.width(), pageSize.height(), insertRect));
imageStamp.setRect(insertRect);
if (imagePath.endsWith("png")){
BitmapFactory.Options tmpOptions = new BitmapFactory.Options();
tmpOptions.inMutable = true;
Bitmap bitmap = BitmapFactory.decodeFile(imagePath, tmpOptions);
imageStamp.updateApWithBitmap(bitmap);
bitmap.recycle();
}else {
imageStamp.setImageStamp(imagePath);
imageStamp.updateAp();
}
// Retrieve the page object for creating annotations.
val pageNumber = 0
val page = document.pageAtIndex(pageNumber)
// Create a standard signature stamp.
val standardStamp = page.addAnnot(CPDFAnnotation.Type.STAMP) as CPDFStampAnnotation
standardStamp.setStandardStamp(CPDFStampAnnotation.StandardStamp.COMPLETED)
val pageSize = page.size
val insertRect = standardStamp.rect
insertRect.set(
page.convertRectFromPage(false, 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 = page.convertRectToPage(false, pageSize.width(), pageSize.height(), insertRect)
standardStamp.updateAp()
// Create a text signature stamp.
val textStamp = page.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(
page.convertRectFromPage(false, 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 = page.convertRectToPage(false, pageSize.width(), pageSize.height(), textStampInsertRect)
textStamp.updateAp()
// Create an image stamp.
val imageStamp = page.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 = page.convertRectToPage(false, 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()
}
Create Ink Annotations
Ink annotations provide a direct and convenient method for drawing annotations.
The steps to create a ink annotation are as follows:
Obtain the page object where the annotation needs to be created.
Create a ink annotation on that page.
Set the path taken by the ink annotation.
Set other properties of the annotation.
Update the annotation appearance to display it on the document.
This example shows how to create ink annotations:
// Retrieve the page object for creating annotations.
int pageNumber = 0;
CPDFPage page = document.pageAtIndex(pageNumber);
// Set the path traced by the freehand annotation.
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);
// Create a ink annotation on the page.
CPDFInkAnnotation inkAnnotation = (CPDFInkAnnotation) page.addAnnot(CPDFInkAnnotation.Type.INK);
float scaleValue = 1;
inkAnnotation.setColor(Color.BLUE);
inkAnnotation.setAlpha(255);
inkAnnotation.setBorderWidth(10 / scaleValue);
RectF rect = null;
RectF size = page.getSize();
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);
/****** Calculate the minimum bounding rectangle for the path ******/
if (rect == null) {
rect = new RectF(point.x, point.y, point.x, point.y);
} else {
rect.union(point.x, point.y);
}
/****** Calculate the coordinates transformed to the page and stored in the linePath collection ******/
linePath[pointIndex] = (page.convertPointToPage(false, size.width(), size.height(), point));
}
path[lineIndex] = linePath;
}
float dx = 10 / scaleValue / 2;
rect.inset(-dx, -dx);
rect.set(page.convertRectToPage(false, size.width(), size.height(), rect));
inkAnnotation.setInkPath(path);
inkAnnotation.setRect(rect);
inkAnnotation.updateAp();
// Retrieve the page object for creating annotations.
val pageNumber = 0
val page = document.pageAtIndex(pageNumber)
// Set the path traced by the freehand annotation.
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)
// Create a ink annotation on the page.
val inkAnnotation = page.addAnnot(CPDFAnnotation.Type.INK) as CPDFInkAnnotation
val scaleValue = 1
inkAnnotation.color = Color.BLUE
inkAnnotation.alpha = 255
inkAnnotation.borderWidth = 10F / scaleValue
var rect: RectF? = null
val size = page.size
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]
/****** Calculate the minimum bounding rectangle for the path ******/
if (rect == null) {
rect = RectF(point.x, point.y, point.x, point.y)
} else {
rect.union(point.x, point.y)
}
/****** Calculate the coordinates transformed to the page and stored in the linePath collection ******/
linePath[pointIndex] = page.convertPointToPage(
false,
size.width(),
size.height(),
point
)
}
path[lineIndex] = linePath
}
val dx = 10F / scaleValue / 2
rect?.inset(-dx, -dx)
rect?.set(page.convertRectToPage(false, size.width(), size.height(), rect))
inkAnnotation.inkPath = path
inkAnnotation.rect = rect
inkAnnotation.updateAp()
Create Audio Annotations
The steps to create an audio annotation are as follows:
Obtain the page object where the annotation needs to be created.
Create an audio annotation on that page.
Set the audio file.
Set other properties.
Update the annotation appearance to display it on the document.
This example shows how to create audio annotations:
// Retrieve the page object for creating annotations.
CPDFPage page = document.pageAtIndex(0);
CPDFSoundAnnotation soundAnnotation = (CPDFSoundAnnotation) page.addAnnot(CPDFAnnotation.Type.SOUND);
RectF insertRect = new RectF(50, 300, 100, 350);
RectF size = page.getSize();
insertRect = page.convertRectToPage(false, size.width(), size.height(), insertRect);
soundAnnotation.setRect(insertRect);
soundAnnotation.setSoundPath(FileUtils.getAssetsTempFile(SampleApplication.getInstance(), "Bird.wav"));
soundAnnotation.updateAp();
val page = document.pageAtIndex(0)
val soundAnnotation = page.addAnnot(CPDFAnnotation.Type.SOUND) as CPDFSoundAnnotation
var insertRect = RectF(50F, 300F, 100F, 350F)
val size = page.size
insertRect = page.convertRectToPage(false, size.width(), size.height(), insertRect)
soundAnnotation.rect = insertRect
soundAnnotation.setSoundPath(getAssetsTempFile(context(), "Bird.wav"))
soundAnnotation.updateAp()