|
@@ -0,0 +1,513 @@
|
|
|
+/**
|
|
|
+ * Copyright © 2014-2023 PDF Technologies, Inc. All Rights Reserved.
|
|
|
+ * <p>
|
|
|
+ * THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
|
|
|
+ * AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
|
|
|
+ * UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
|
|
|
+ * This notice may not be removed from this file.
|
|
|
+ */
|
|
|
+
|
|
|
+package com.compdfkit.samples.samples;
|
|
|
+
|
|
|
+import android.graphics.Color;
|
|
|
+import android.graphics.PointF;
|
|
|
+import android.graphics.RectF;
|
|
|
+import android.os.Environment;
|
|
|
+import android.text.TextUtils;
|
|
|
+
|
|
|
+import com.compdfkit.core.annotation.CPDFAnnotation;
|
|
|
+import com.compdfkit.core.annotation.CPDFBorderStyle;
|
|
|
+import com.compdfkit.core.annotation.CPDFCircleAnnotation;
|
|
|
+import com.compdfkit.core.annotation.CPDFFreetextAnnotation;
|
|
|
+import com.compdfkit.core.annotation.CPDFHighlightAnnotation;
|
|
|
+import com.compdfkit.core.annotation.CPDFInkAnnotation;
|
|
|
+import com.compdfkit.core.annotation.CPDFLineAnnotation;
|
|
|
+import com.compdfkit.core.annotation.CPDFLinkAnnotation;
|
|
|
+import com.compdfkit.core.annotation.CPDFSoundAnnotation;
|
|
|
+import com.compdfkit.core.annotation.CPDFSquareAnnotation;
|
|
|
+import com.compdfkit.core.annotation.CPDFStampAnnotation;
|
|
|
+import com.compdfkit.core.annotation.CPDFTextAnnotation;
|
|
|
+import com.compdfkit.core.annotation.CPDFTextAttribute;
|
|
|
+import com.compdfkit.core.document.CPDFDestination;
|
|
|
+import com.compdfkit.core.document.CPDFDocument;
|
|
|
+import com.compdfkit.core.document.action.CPDFGoToAction;
|
|
|
+import com.compdfkit.core.page.CPDFPage;
|
|
|
+import com.compdfkit.core.page.CPDFTextPage;
|
|
|
+import com.compdfkit.core.page.CPDFTextRange;
|
|
|
+import com.compdfkit.core.page.CPDFTextSearcher;
|
|
|
+import com.compdfkit.core.page.CPDFTextSelection;
|
|
|
+import com.compdfkit.samples.PDFSamples;
|
|
|
+import com.compdfkit.samples.R;
|
|
|
+import com.compdfkit.samples.SampleApplication;
|
|
|
+import com.compdfkit.samples.util.FileUtils;
|
|
|
+import com.compdfkit.samples.util.OutputListener;
|
|
|
+import com.compdfkit.ui.attribute.CPDFFreetextAttr;
|
|
|
+import com.compdfkit.ui.reader.CPDFReaderView;
|
|
|
+import com.compdfkit.ui.textsearch.ITextSearcher;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+
|
|
|
+public class AnnotationTest extends PDFSamples {
|
|
|
+
|
|
|
+ public AnnotationTest() {
|
|
|
+ setTitle(R.string.annotation_test_title);
|
|
|
+ setDescription(R.string.annotation_test_desc);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void run(OutputListener outputListener) {
|
|
|
+ super.run(outputListener);
|
|
|
+ printHead();
|
|
|
+ CPDFDocument document = new CPDFDocument(SampleApplication.getInstance());
|
|
|
+ document.open(FileUtils.getAssetsTempFile(SampleApplication.getInstance(), "CommonFivePage.pdf"));
|
|
|
+ //------------------------------------------
|
|
|
+ //Samples 1 : add freetext annotation
|
|
|
+ addFreeText(document);
|
|
|
+
|
|
|
+ //------------------------------------------
|
|
|
+ //Samples 2 : add ink annotation
|
|
|
+ addInk(document);
|
|
|
+
|
|
|
+ //------------------------------------------
|
|
|
+ //Samples 3 : add line annotation
|
|
|
+ addLine(document);
|
|
|
+
|
|
|
+ //------------------------------------------
|
|
|
+ //Samples 4 : add circle annotation
|
|
|
+ addCircleShape(document);
|
|
|
+
|
|
|
+ //------------------------------------------
|
|
|
+ //Samples 5 : add square annotation
|
|
|
+ addSquareShape(document);
|
|
|
+
|
|
|
+ //------------------------------------------
|
|
|
+ //Samples 6 : add highlight(markup) annotation
|
|
|
+// addHighlight(document);
|
|
|
+
|
|
|
+ //------------------------------------------
|
|
|
+ //Samples 7 : add link annotation
|
|
|
+ addLink(document);
|
|
|
+
|
|
|
+ //------------------------------------------
|
|
|
+ //Samples 8 : add note annotation
|
|
|
+ addNote(document);
|
|
|
+
|
|
|
+ //------------------------------------------
|
|
|
+ //Samples 8 : add sound annotation
|
|
|
+ addSound(document);
|
|
|
+
|
|
|
+ //------------------------------------------
|
|
|
+ //Samples 9 : add stamp annotation
|
|
|
+ addStamp(document);
|
|
|
+
|
|
|
+ //------------------------------------------
|
|
|
+ //Samples 10 : print annotation list info
|
|
|
+ printAnnotationList(document);
|
|
|
+
|
|
|
+ File file = new File(SampleApplication.getInstance().getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS),
|
|
|
+ "AnnotationTest/CreateAnnotationTest.pdf");
|
|
|
+ saveSamplePDF(document, file, true);
|
|
|
+ outputListener.println("Done. Result saved in CreateAnnotationTest.pdf");
|
|
|
+
|
|
|
+ //------------------------------------------
|
|
|
+ //Samples 11 : delete annotation
|
|
|
+ deleteAnnotation();
|
|
|
+
|
|
|
+ printFooter();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void addFreeText(CPDFDocument document) {
|
|
|
+ CPDFPage page1 = document.pageAtIndex(0);
|
|
|
+ CPDFFreetextAnnotation freetextAnnotation = (CPDFFreetextAnnotation) page1.addAnnot(CPDFAnnotation.Type.FREETEXT);
|
|
|
+ RectF freeText1Rect = getConvertRect(page1, new RectF(10, 200, 160, 570));
|
|
|
+ freetextAnnotation.setRect(freeText1Rect);
|
|
|
+ freetextAnnotation.setFreetextAlignment(CPDFFreetextAnnotation.Alignment.ALIGNMENT_LEFT);
|
|
|
+ CPDFBorderStyle borderStyle = new CPDFBorderStyle();
|
|
|
+ borderStyle.setBorderWidth(2);
|
|
|
+ borderStyle.setStyle(CPDFBorderStyle.Style.Border_Dashed);
|
|
|
+ freetextAnnotation.setBorderStyle(borderStyle);
|
|
|
+ CPDFTextAttribute textAttribute = new CPDFTextAttribute(CPDFTextAttribute.FontNameHelper.obtainFontName(
|
|
|
+ CPDFTextAttribute.FontNameHelper.FontType.Courier, false, false
|
|
|
+ ), 12, Color.RED);
|
|
|
+ freetextAnnotation.setFreetextDa(textAttribute);
|
|
|
+ freetextAnnotation.setAlpha(255);
|
|
|
+ freetextAnnotation.setBorderRGBColor(255, 0, 0);
|
|
|
+ freetextAnnotation.setContent("Some swift brown fox snatched a gray hare out of the air by freezing it with an angry glare.\n" +
|
|
|
+ "\n" +
|
|
|
+ "Aha!\n" +
|
|
|
+ "\n" +
|
|
|
+ "And there was much rejoicing!");
|
|
|
+ freetextAnnotation.updateAp();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void addInk(CPDFDocument document) {
|
|
|
+ CPDFPage page1 = document.pageAtIndex(0);
|
|
|
+ ArrayList<ArrayList<PointF>> mDrawing = new ArrayList<>();
|
|
|
+ ArrayList<PointF> arc1 = new ArrayList<>();
|
|
|
+ arc1.add(new PointF(100, 100));
|
|
|
+ arc1.add(new PointF(110, 110));
|
|
|
+ arc1.add(new PointF(120, 120));
|
|
|
+ mDrawing.add(arc1);
|
|
|
+ ArrayList<PointF> arc2 = new ArrayList<>();
|
|
|
+ arc2.add(new PointF(115, 115));
|
|
|
+ arc2.add(new PointF(130, 130));
|
|
|
+ arc2.add(new PointF(160, 160));
|
|
|
+ mDrawing.add(arc2);
|
|
|
+ float scaleValue = 1F;
|
|
|
+ float borderWidth = 5F;
|
|
|
+ CPDFInkAnnotation inkAnnotation = (CPDFInkAnnotation) page1.addAnnot(CPDFInkAnnotation.Type.INK);
|
|
|
+ inkAnnotation.setColor(Color.RED);
|
|
|
+ inkAnnotation.setAlpha(255);
|
|
|
+ inkAnnotation.setBorderWidth(borderWidth);
|
|
|
+ RectF rect = null;
|
|
|
+ RectF size = document.getPageSize(0);
|
|
|
+ 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 smallest Rect that the Path is surrounded by ******/
|
|
|
+ if (rect == null) {
|
|
|
+ rect = new RectF(point.x, point.y, point.x, point.y);
|
|
|
+ } else {
|
|
|
+ rect.union(point.x, point.y);
|
|
|
+ }
|
|
|
+ /****** Calculate the coordinate points that are converted to the Page and stored in the linePath collection ******/
|
|
|
+ linePath[pointIndex] = (page1.convertPointToPage(false, size.width(), size.height(), point));
|
|
|
+ }
|
|
|
+ path[lineIndex] = linePath;
|
|
|
+ }
|
|
|
+ float dx = borderWidth / scaleValue / 2;
|
|
|
+ rect.inset(-dx, -dx);
|
|
|
+ rect.set(page1.convertRectToPage(false, size.width(), size.height(), rect));
|
|
|
+ inkAnnotation.setInkPath(path);
|
|
|
+ inkAnnotation.setRect(rect);
|
|
|
+ inkAnnotation.updateAp();
|
|
|
+ mDrawing.clear();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void addLine(CPDFDocument document) {
|
|
|
+ // Add a green dotted line annotation
|
|
|
+ CPDFPage page2 = document.pageAtIndex(1);
|
|
|
+ CPDFLineAnnotation lineAnnotation = (CPDFLineAnnotation) page2.addAnnot(CPDFAnnotation.Type.LINE);
|
|
|
+ PointF startPoint = new PointF(200, 100);
|
|
|
+ PointF endPoint = new PointF(50, 300);
|
|
|
+ RectF area = new RectF();
|
|
|
+
|
|
|
+ convertLinePoint(page2, startPoint, endPoint, area);
|
|
|
+ lineAnnotation.setRect(area);
|
|
|
+ lineAnnotation.setLinePoints(startPoint, endPoint);
|
|
|
+ lineAnnotation.setLineType(CPDFLineAnnotation.LineType.LINETYPE_NONE, CPDFLineAnnotation.LineType.LINETYPE_NONE);
|
|
|
+ CPDFBorderStyle borderStyle = new CPDFBorderStyle(CPDFBorderStyle.Style.Border_Dashed, 10, new float[]{8, 4F});
|
|
|
+ lineAnnotation.setBorderStyle(borderStyle);
|
|
|
+ lineAnnotation.setBorderWidth(3);
|
|
|
+ lineAnnotation.setBorderAlpha(255);
|
|
|
+ lineAnnotation.setBorderColor(Color.GREEN);
|
|
|
+ lineAnnotation.updateAp();
|
|
|
+
|
|
|
+ //Add a solid red line annotation with an arrow type
|
|
|
+ CPDFLineAnnotation lineAnnotation2 = (CPDFLineAnnotation) page2.addAnnot(CPDFAnnotation.Type.LINE);
|
|
|
+ PointF startPoint2 = new PointF(200, 350);
|
|
|
+ PointF endPoint2 = new PointF(50, 550);
|
|
|
+ RectF area2 = new RectF();
|
|
|
+
|
|
|
+ convertLinePoint(page2, startPoint2, endPoint2, area2);
|
|
|
+ lineAnnotation2.setRect(area2);
|
|
|
+ lineAnnotation2.setLinePoints(startPoint2, endPoint2);
|
|
|
+ lineAnnotation2.setLineType(CPDFLineAnnotation.LineType.LINETYPE_CIRCLE, CPDFLineAnnotation.LineType.LINETYPE_ARROW);
|
|
|
+ CPDFBorderStyle borderStyle2 = new CPDFBorderStyle(CPDFBorderStyle.Style.Border_Solid, 10, new float[]{8, 0});
|
|
|
+ lineAnnotation2.setBorderStyle(borderStyle2);
|
|
|
+ lineAnnotation2.setBorderWidth(5);
|
|
|
+ lineAnnotation2.setBorderAlpha(255);
|
|
|
+ lineAnnotation2.setBorderColor(Color.RED);
|
|
|
+ lineAnnotation2.updateAp();
|
|
|
+
|
|
|
+ // Add a solid red line annotation
|
|
|
+ CPDFLineAnnotation lineAnnotation3 = (CPDFLineAnnotation) page2.addAnnot(CPDFAnnotation.Type.LINE);
|
|
|
+ PointF startPoint3 = new PointF(400, 100);
|
|
|
+ PointF endPoint3 = new PointF(250, 300);
|
|
|
+ RectF area3 = new RectF();
|
|
|
+
|
|
|
+ convertLinePoint(page2, startPoint3, endPoint3, area3);
|
|
|
+ lineAnnotation3.setRect(area3);
|
|
|
+ lineAnnotation3.setLinePoints(startPoint3, endPoint3);
|
|
|
+ lineAnnotation3.setLineType(CPDFLineAnnotation.LineType.LINETYPE_NONE, CPDFLineAnnotation.LineType.LINETYPE_NONE);
|
|
|
+ CPDFBorderStyle borderStyle3 = new CPDFBorderStyle(CPDFBorderStyle.Style.Border_Solid, 10, new float[]{8, 0});
|
|
|
+ lineAnnotation3.setBorderStyle(borderStyle3);
|
|
|
+ lineAnnotation3.setBorderWidth(5);
|
|
|
+ lineAnnotation3.setBorderAlpha(255);
|
|
|
+ lineAnnotation3.setBorderColor(Color.BLUE);
|
|
|
+ lineAnnotation3.updateAp();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void convertLinePoint(CPDFPage page, PointF startPoint, PointF endPoint, RectF area) {
|
|
|
+ RectF pageSize = page.getSize();
|
|
|
+ float lineWidth = 10f;
|
|
|
+ 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));
|
|
|
+ }
|
|
|
+
|
|
|
+ private void addCircleShape(CPDFDocument document) {
|
|
|
+ // Add a circular annotation with a green border and blue fill
|
|
|
+ CPDFPage page3 = document.pageAtIndex(2);
|
|
|
+ CPDFCircleAnnotation circleAnnotation = (CPDFCircleAnnotation) page3.addAnnot(CPDFAnnotation.Type.CIRCLE);
|
|
|
+ RectF pageSize = page3.getSize();
|
|
|
+ RectF insertRect = new RectF(50, 50, 150, 150);
|
|
|
+ insertRect = page3.convertRectToPage(false, pageSize.width(), pageSize.height(), insertRect);
|
|
|
+ circleAnnotation.setRect(insertRect);
|
|
|
+ circleAnnotation.setBorderColor(Color.parseColor("#3863F1"));
|
|
|
+ CPDFBorderStyle borderStyle = new CPDFBorderStyle(CPDFBorderStyle.Style.Border_Solid, 10, new float[]{8.0F, 0F});
|
|
|
+ borderStyle.setBorderWidth(5F);
|
|
|
+ circleAnnotation.setBorderStyle(borderStyle);
|
|
|
+ circleAnnotation.setBorderAlpha(255);
|
|
|
+ circleAnnotation.setFillColor(Color.parseColor("#31BC98"));
|
|
|
+ circleAnnotation.setFillAlpha(255);
|
|
|
+ circleAnnotation.updateAp();
|
|
|
+
|
|
|
+ //Add a dotted border circular annotation
|
|
|
+ CPDFCircleAnnotation circleAnnotation2 = (CPDFCircleAnnotation) page3.addAnnot(CPDFAnnotation.Type.CIRCLE);
|
|
|
+ RectF insertRect2 = new RectF(50, 200, 150, 300);
|
|
|
+ insertRect2 = page3.convertRectToPage(false, pageSize.width(), pageSize.height(), insertRect2);
|
|
|
+ circleAnnotation2.setRect(insertRect2);
|
|
|
+ circleAnnotation2.setBorderColor(Color.parseColor("#3863F1"));
|
|
|
+ CPDFBorderStyle borderStyle2 = new CPDFBorderStyle(CPDFBorderStyle.Style.Border_Dashed, 10, new float[]{8.0F, 4F});
|
|
|
+ borderStyle2.setBorderWidth(5F);
|
|
|
+ circleAnnotation2.setBorderStyle(borderStyle2);
|
|
|
+ circleAnnotation2.setBorderAlpha(127);
|
|
|
+ circleAnnotation2.setFillColor(Color.parseColor("#31BC98"));
|
|
|
+ circleAnnotation2.setFillAlpha(127);
|
|
|
+ circleAnnotation2.updateAp();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void addSquareShape(CPDFDocument document) {
|
|
|
+ CPDFPage page3 = document.pageAtIndex(2);
|
|
|
+ CPDFSquareAnnotation squareAnnotation = (CPDFSquareAnnotation) page3.addAnnot(CPDFAnnotation.Type.SQUARE);
|
|
|
+ RectF pageSize = page3.getSize();
|
|
|
+ RectF insertRect = new RectF(50, 350, 300, 450);
|
|
|
+ insertRect = page3.convertRectToPage(false, pageSize.width(), pageSize.height(), insertRect);
|
|
|
+ squareAnnotation.setRect(insertRect);
|
|
|
+ squareAnnotation.setBorderColor(Color.parseColor("#3863F1"));
|
|
|
+ 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.parseColor("#31BC98"));
|
|
|
+ squareAnnotation.setFillAlpha(255);
|
|
|
+ squareAnnotation.updateAp();
|
|
|
+
|
|
|
+ CPDFSquareAnnotation squareAnnotation2 = (CPDFSquareAnnotation) page3.addAnnot(CPDFAnnotation.Type.SQUARE);
|
|
|
+ RectF insertRect2 = new RectF(50, 500, 300, 600);
|
|
|
+ insertRect2 = page3.convertRectToPage(false, pageSize.width(), pageSize.height(), insertRect2);
|
|
|
+ squareAnnotation2.setRect(insertRect2);
|
|
|
+ squareAnnotation2.setBorderColor(Color.parseColor("#3863F1"));
|
|
|
+ CPDFBorderStyle borderStyle2 = new CPDFBorderStyle(CPDFBorderStyle.Style.Border_Dashed, 10, new float[]{8.0F, 4F});
|
|
|
+ borderStyle2.setBorderWidth(10F);
|
|
|
+ squareAnnotation2.setBorderStyle(borderStyle2);
|
|
|
+ squareAnnotation2.setBorderAlpha(127);
|
|
|
+ squareAnnotation2.setFillColor(Color.parseColor("#31BC98"));
|
|
|
+ squareAnnotation2.setFillAlpha(127);
|
|
|
+ squareAnnotation2.updateAp();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void addHighlight(CPDFDocument document) {
|
|
|
+ CPDFReaderView readerView = new CPDFReaderView(SampleApplication.getInstance());
|
|
|
+ readerView.setPDFDocument(document);
|
|
|
+ readerView.reloadPages();
|
|
|
+ //Also search for the `Page` keyword in the 3rd of the document
|
|
|
+ List<CPDFTextRange> list = startSearch(readerView, "Page");
|
|
|
+ if (list != null && list.size() > 0) {
|
|
|
+ CPDFTextRange textRange = list.get(0);
|
|
|
+ CPDFPage pdfPage = document.pageAtIndex(3);
|
|
|
+ CPDFTextPage pdfTextPage = pdfPage.getTextPage();
|
|
|
+ CPDFTextSelection[] textSelectionArr = pdfTextPage.getSelectionsByTextForRange(textRange);
|
|
|
+ //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 / 2));
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ //Get the position of the keyword `Page` in the page
|
|
|
+ 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();
|
|
|
+ outputListener.println(annotRect.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static List<CPDFTextRange> startSearch(CPDFReaderView readerView, String keywords) {
|
|
|
+ ITextSearcher textSearcher = readerView.getTextSearcher();
|
|
|
+ if (null == textSearcher) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ CPDFDocument document = readerView.getPDFDocument();
|
|
|
+ if (null == document) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ textSearcher.setSearchConfig(keywords, CPDFTextSearcher.PDFSearchOptions.PDFSearchCaseInsensitive);
|
|
|
+ List<CPDFTextRange> searchTextInfoList = new ArrayList<>();
|
|
|
+
|
|
|
+ CPDFPage page = document.pageAtIndex(3);
|
|
|
+ CPDFTextPage textPage = page.getTextPage();
|
|
|
+ if ((null == textPage) || !textPage.isValid()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ final List<CPDFTextRange> searchPageContent = textSearcher.searchKeyword(3);
|
|
|
+ if (searchPageContent.size() > 0) {
|
|
|
+ searchTextInfoList.addAll(searchPageContent);
|
|
|
+ }
|
|
|
+ page.close();
|
|
|
+ return searchTextInfoList;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void addLink(CPDFDocument document) {
|
|
|
+ CPDFPage page = document.pageAtIndex(3);
|
|
|
+ CPDFLinkAnnotation linkAnnotation = (CPDFLinkAnnotation) page.addAnnot(CPDFAnnotation.Type.LINK);
|
|
|
+ RectF pageSize = page.getSize();
|
|
|
+ RectF insertRect = getConvertRect(page, new RectF(50, 50, 150, 150));
|
|
|
+ linkAnnotation.setRect(insertRect);
|
|
|
+
|
|
|
+ float firstPageHeight = document.getPageSize(0).height();
|
|
|
+ CPDFGoToAction goToAction = new CPDFGoToAction();
|
|
|
+ CPDFDestination destination = new CPDFDestination(1, 0, firstPageHeight, 1f);
|
|
|
+ goToAction.setDestination(document, destination);
|
|
|
+ linkAnnotation.setLinkAction(goToAction);
|
|
|
+ linkAnnotation.updateAp();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void addNote(CPDFDocument document) {
|
|
|
+ CPDFPage page = document.pageAtIndex(3);
|
|
|
+ CPDFTextAnnotation textAnnotation = (CPDFTextAnnotation) page.addAnnot(CPDFAnnotation.Type.TEXT);
|
|
|
+ //get the actual size of the page you want to insert
|
|
|
+ RectF insertRect = getConvertRect(page, new RectF(50, 200, 100, 250));
|
|
|
+ textAnnotation.setRect(insertRect);
|
|
|
+ textAnnotation.setContent("ComPDFKit");
|
|
|
+ textAnnotation.updateAp();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void addSound(CPDFDocument document) {
|
|
|
+ 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();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void addStamp(CPDFDocument document) {
|
|
|
+ int yOffset = 50;
|
|
|
+ float lastOffset = 0;
|
|
|
+ for (int i = 0; i < CPDFStampAnnotation.StandardStamp.values().length; i++) {
|
|
|
+ CPDFPage page = document.pageAtIndex(4);
|
|
|
+ CPDFStampAnnotation.StandardStamp standardStamp = CPDFStampAnnotation.StandardStamp.values()[i];
|
|
|
+ if (standardStamp == null || standardStamp == CPDFStampAnnotation.StandardStamp.UNKNOWN) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // add Standard stamp
|
|
|
+ CPDFStampAnnotation standard = (CPDFStampAnnotation) page.addAnnot(CPDFAnnotation.Type.STAMP);
|
|
|
+ if (standard == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ standard.setStandardStamp(standardStamp);
|
|
|
+ RectF pageSize = page.getSize();
|
|
|
+ RectF insertRect = standard.getRect();
|
|
|
+ insertRect.set(page.convertRectFromPage(false, pageSize.width(), pageSize.height(), insertRect));
|
|
|
+ float defaultWidth = 100F;
|
|
|
+ int x = 50;
|
|
|
+ if (i == 10){
|
|
|
+ lastOffset = 50;
|
|
|
+ }
|
|
|
+ if (i >= 10){
|
|
|
+ x = 150;
|
|
|
+ }
|
|
|
+ yOffset = (int) lastOffset + 10;
|
|
|
+ PointF vertex = new PointF(x, yOffset);
|
|
|
+ insertRect.set(vertex.x, vertex.y, vertex.x + defaultWidth, vertex.y + defaultWidth * Math.abs(insertRect.height() / insertRect.width()));
|
|
|
+ lastOffset = insertRect.bottom;
|
|
|
+ standard.setRect(page.convertRectToPage(false, pageSize.width(), pageSize.height(), insertRect));
|
|
|
+ standard.updateAp();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private RectF getConvertRect(CPDFPage page, RectF rectF) {
|
|
|
+ RectF size = page.getSize();
|
|
|
+ return page.convertRectToPage(false, size.width(), size.height(), rectF);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void printAnnotationList(CPDFDocument document){
|
|
|
+ for (int i = 0; i < document.getPageCount(); i++) {
|
|
|
+ CPDFPage page = document.pageAtIndex(i);
|
|
|
+ for (CPDFAnnotation annotation : page.getAnnotations()) {
|
|
|
+ outputListener.println("--------------------------------------------");
|
|
|
+ outputListener.println("Page: " + (i + 1));
|
|
|
+ outputListener.println("Annot Type: " + annotation.getType().name());
|
|
|
+ RectF widgetRect = annotation.getRect();
|
|
|
+ RectF position = page.convertRectFromPage(false, document.getPageSize(i).width(),
|
|
|
+ document.getPageSize(i).height(),widgetRect);
|
|
|
+ outputListener.println(String.format("Position: %d, %d, %d, %d",(int) position.left, (int) position.top,
|
|
|
+ (int) position.right, (int) position.bottom));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ outputListener.println("--------------------------------------------");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void deleteAnnotation(){
|
|
|
+ outputListener.println("--------------------------------------------");
|
|
|
+ outputListener.println("Samples : delete annotation");
|
|
|
+ File sampleFile = new File(SampleApplication.getInstance().getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS),
|
|
|
+ "AnnotationTest/CreateAnnotationTest.pdf");
|
|
|
+ CPDFDocument document = new CPDFDocument(SampleApplication.getInstance());
|
|
|
+ document.open(sampleFile.getAbsolutePath());
|
|
|
+ for (int i = 0; i < document.getPageCount(); i++) {
|
|
|
+ CPDFPage page = document.pageAtIndex(i);
|
|
|
+ List<CPDFAnnotation> annotations = page.getAnnotations();
|
|
|
+ if (annotations != null && annotations.size() >0){
|
|
|
+ page.deleteAnnotation(annotations.get(0));
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ File file = new File(SampleApplication.getInstance().getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS),
|
|
|
+ "AnnotationTest/DeleteAnnotationTest.pdf");
|
|
|
+ saveSamplePDF(document, file, true);
|
|
|
+ outputListener.println("Done. Results saved in DeleteAnnotationTest.pdf");
|
|
|
+ }
|
|
|
+}
|