Create & Edit Annotations
ComPDFKit PDF SDK includes a wide variety of standard annotations, and each of them is added to the project in a similar way.
Note: To convert Android device coordinate to PDF page coordiante with functionCPDFPage.convertRectToPage
;To convert PDF page coordinate to Android device coordiante with functionCPDFPage.convertPointFromPage
Note
To add a sticky note (text annotation) to a PDF Document page by using the following method.
// open pdf document
CPDFReaderView readerView = findViewById(R.id.readerview);
CPDFDocument document = new CPDFDocument(context);
CPDFDocument.PDFDocumentError error = document.open("pdfPath");
if (error == CPDFDocument.PDFDocumentError.PDFDocumentErrorSuccess) {
//The document is opened successfully and data can be parsed and manipulated.
readerView.setPDFDocument(document);
if (document.getPageCount() > 0){
//insert page number
int pageNumber = 0;
//get page instance of pdf
CPDFPage page = document.pageAtIndex(pageNumber);
CPDFTextAnnotation textAnnotation = (CPDFTextAnnotation) page.addAnnot(CPDFAnnotation.Type.TEXT);
//get the actual size of the page you want to insert
RectF pageSize = readerView.getPageNoZoomSize(pageNumber);
RectF insertRect = new RectF(0,0,50,50);
//coordinate conversion
insertRect = document.pageAtIndex(pageNumber).convertRectToPage(readerView.isCropMode(),pageSize.width(),pageSize.height(),insertRect);
textAnnotation.setRect(insertRect);
textAnnotation.setContent("Hello world!");
textAnnotation.updateAp();
readerView.reloadPages();
}
} else {
//The PDF file failed to open. You can refer to the API file for specific error messages.
}
Link
To add a hyperlink or intra-document link annotation to a PDF Document page by using the following method.
CPDFLinkAnnotation linkAnnotation = (CPDFLinkAnnotation) page.addAnnot(CPDFAnnotation.Type.LINK);
RectF pageSize = readerView.getPageNoZoomSize(pageNumber);
RectF insertRect = new RectF(0,0,100,100);
//coordinate conversion
insertRect = document.pageAtIndex(pageNumber).convertRectToPage(readerView.isCropMode(),pageSize.width(),pageSize.height(),insertRect);
linkAnnotation.setRect(insertRect);
//destination
CPDFDestination destination = new CPDFDestination(pageNumber,0,pageSize.height(),1f);
linkAnnotation.setDestination(document,destination);
//website
CPDFUriAction uriAction = new CPDFUriAction();
uriAction.setUri("website");
linkAnnotation.setLinkAction(uriAction);
Free Text
To add a free text annotation to a PDF Document page by using the following method.
CPDFFreetextAnnotation freetextAnnotation = (CPDFFreetextAnnotation) page.addAnnot(CPDFAnnotation.Type.FREETEXT);
RectF pageSize = readerView.getPageNoZoomSize(pageNumber);
RectF insertRect = new RectF(0,0,100,100);
//coordinate conversion
insertRect = document.pageAtIndex(pageNumber).convertRectToPage(readerView.isCropMode(),pageSize.width(),pageSize.height(),insertRect);
freetextAnnotation.setRect(insertRect);
freetextAnnotation.setContent("Hello World!");
freetextAnnotation.setFreetextAlignment(CPDFFreetextAnnotation.Alignment.ALIGNMENT_LEFT);
CPDFTextAttribute textAttribute = freetextAnnotation.getFreetextDa();
textAttribute.setColor(Color.YELLOW);
textAttribute.setFontSize(14);
textAttribute.setFontName(CPDFTextAttribute.FontNameHelper.obtainFontName(CPDFTextAttribute.FontNameHelper.FontType.Helvetica,false,false));
freetextAnnotation.setFreetextDa(textAttribute);
readerView.reloadPages();
Shapes
To add a shape annotation to a PDF Document page by using the following method.
// Square
CPDFSquareAnnotation squareAnnotation = (CPDFSquareAnnotation) page.addAnnot(CPDFAnnotation.Type.SQUARE);
RectF pageSize = readerView.getPageNoZoomSize(pageNumber);
RectF insertRect = new RectF(0,0,100,100);
//coordinate conversion
insertRect = document.pageAtIndex(pageNumber).convertRectToPage(readerView.isCropMode(),pageSize.width(),pageSize.height(),insertRect);
squareAnnotation.setRect(insertRect);
squareAnnotation.setLineColor(Color.YELLOW);
squareAnnotation.getBorderStyle().setBorderWidth(10f);
squareAnnotation.setLineAlpha(255);
squareAnnotation.setBgColor(Color.RED);
squareAnnotation.setBgAlpha(255);
readerView.reloadPages();
// Circle
CPDFCircleAnnotation circleAnnotation = (CPDFCircleAnnotation) page.addAnnot(CPDFAnnotation.Type.CIRCLE);
RectF pageSize = readerView.getPageNoZoomSize(pageNumber);
RectF insertRect = new RectF(0,0,100,100);
//coordinate conversion
insertRect = document.pageAtIndex(pageNumber).convertRectToPage(readerView.isCropMode(),pageSize.width(),pageSize.height(),insertRect);
circleAnnotation.setRect(insertRect);
circleAnnotation.setLineColor(Color.YELLOW);
circleAnnotation.getBorderStyle().setBorderWidth(10f);
circleAnnotation.setLineAlpha(255);
circleAnnotation.setBgColor(Color.RED);
circleAnnotation.setBgAlpha(255);
readerView.reloadPages();
// Line
CPDFLineAnnotation lineAnnotation = (CPDFLineAnnotation) page.addAnnot(CPDFAnnotation.Type.LINE);
float lineWidth = 10f;
lineAnnotation.getBorderStyle().setBorderWidth(10f);
RectF pageSize = readerView.getPageNoZoomSize(pageNumber);
//coordinate conversion
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(TBorderStyle.Style.Border_Solid,lineWidth,null);
lineAnnotation.setBorderStyle(borderStyle);
lineAnnotation.setLineAlpha(255);
lineAnnotation.setLineColor(Color.RED);
readerView.reloadPages();
Note: CPDFLineAnnotation
properties (startPoint
, endPoint
) point is specified in page-space coordinates. Page space is a coordinate system with the origin at the lower-left corner of the current page.
Markup
To add a highlight annotation to a PDF Document page by using the following method, and add other markup annotations in a similar way.
// First, get array of TextSelection from the specific area on a page.
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);
//Then, add a highlight annotation for the specific area.
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();
Stamp
To add standard, text, and image stamps to a PDF document page by using the following method.
// Standard
CPDFStampAnnotation standard = (CPDFStampAnnotation)page.addAnnot(CPDFAnnotation.Type.STAMP);
standard.setStandardStamp(CPDFStampAnnotation.StandardStamp.COMPLETED);
RectF pageSize = readerView.getPageNoZoomSize(pageNumber);
RectF insertRect = standard.getRect();
insertRect.set(page.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(page.convertRectToPage(readerView.isCropMode(), pageSize.width(), pageSize.height(), insertRect));
standard.updateAp();
readerView.reloadPages();
// Text
CPDFStampAnnotation standard = (CPDFStampAnnotation) page.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(page.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(page.convertRectToPage(readerView.isCropMode(), pageSize.width(), pageSize.height(), insertRect));
standard.updateAp();
readerView.reloadPages();
// Image
CPDFStampAnnotation standard = (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 = readerView.getPageNoZoomSize(pageNumber);
insertRect.set(page.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();
}
readerView.reloadPages();