|
@@ -67,7 +67,6 @@ public class AnnotationTest extends PDFSamples {
|
|
|
document.open(FileUtils.getAssetsTempFile(context, "CommonFivePage.pdf"));
|
|
|
|
|
|
printDividingLine();
|
|
|
- outputListener.println("Samples 1 : create test annotation");
|
|
|
|
|
|
|
|
|
addFreeText(document);
|
|
@@ -110,7 +109,9 @@ public class AnnotationTest extends PDFSamples {
|
|
|
|
|
|
File file = new File(outputDir(), "AnnotationTest/CreateAnnotationTest.pdf");
|
|
|
saveSamplePDF(document, file, true);
|
|
|
+ outputListener.println("Done.");
|
|
|
outputListener.println("\nDone. Result saved in CreateAnnotationTest.pdf");
|
|
|
+ printDividingLine();
|
|
|
|
|
|
|
|
|
|
|
@@ -366,74 +367,23 @@ public class AnnotationTest extends PDFSamples {
|
|
|
* @param document
|
|
|
*/
|
|
|
private void addHighlight(CPDFDocument document) {
|
|
|
- CPDFReaderView readerView = new CPDFReaderView(context);
|
|
|
- readerView.setPDFDocument(document);
|
|
|
- readerView.reloadPages();
|
|
|
|
|
|
- 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);
|
|
|
-
|
|
|
- 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;
|
|
|
- }
|
|
|
-
|
|
|
- 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());
|
|
|
- }
|
|
|
+ CPDFPage pdfPage = document.pageAtIndex(3);
|
|
|
+ CPDFTextPage pdfTextPage = pdfPage.getTextPage();
|
|
|
+
|
|
|
+ RectF annotRect = new RectF(315, 258, 372, 288);
|
|
|
+ annotRect = pdfPage.convertRectToPage(false, pdfPage.getSize().width(), pdfPage.getSize().height(), annotRect);
|
|
|
+ CPDFHighlightAnnotation highlightAnnotation = (CPDFHighlightAnnotation) pdfPage.addAnnot(CPDFAnnotation.Type.HIGHLIGHT);
|
|
|
+ highlightAnnotation.setColor(Color.YELLOW);
|
|
|
+ highlightAnnotation.setAlpha((255 / 2));
|
|
|
+
|
|
|
+ highlightAnnotation.setQuadRects(new RectF[]{annotRect});
|
|
|
+ highlightAnnotation.setMarkedText("Page");
|
|
|
+ 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;
|
|
|
- }
|
|
|
|
|
|
|
|
|
* Samples 7 : add link annotation
|
|
@@ -535,7 +485,7 @@ public class AnnotationTest extends PDFSamples {
|
|
|
RectF insertRect = stampAnnotation.getRect();
|
|
|
insertRect.set(page.convertRectFromPage(false, pageSize.width(), pageSize.height(), insertRect));
|
|
|
float defaultWidth = 150f;
|
|
|
- PointF vertex = new PointF(300,50);
|
|
|
+ PointF vertex = new PointF(300, 50);
|
|
|
insertRect.set(vertex.x, vertex.y, vertex.x + defaultWidth, vertex.y + defaultWidth * Math.abs(insertRect.height() / insertRect.width()));
|
|
|
stampAnnotation.setRect(page.convertRectToPage(false, pageSize.width(), pageSize.height(), insertRect));
|
|
|
stampAnnotation.updateAp();
|
|
@@ -544,7 +494,7 @@ public class AnnotationTest extends PDFSamples {
|
|
|
CPDFStampAnnotation standard = (CPDFStampAnnotation) page.addAnnot(CPDFAnnotation.Type.STAMP);
|
|
|
String imagePath = FileUtils.getAssetsTempFile(context, "ComPDFKit.png");
|
|
|
float imageDefaultWidth = 100F;
|
|
|
- PointF imageVertex = new PointF(300,300);
|
|
|
+ PointF imageVertex = new PointF(300, 300);
|
|
|
BitmapFactory.Options options = new BitmapFactory.Options();
|
|
|
options.inJustDecodeBounds = true;
|
|
|
BitmapFactory.decodeFile(imagePath, options);
|
|
@@ -552,13 +502,13 @@ public class AnnotationTest extends PDFSamples {
|
|
|
RectF pageSize1 = page.getSize();
|
|
|
imageInsertRect.set(page.convertRectToPage(false, pageSize1.width(), pageSize1.height(), imageInsertRect));
|
|
|
standard.setRect(imageInsertRect);
|
|
|
- if (imagePath.endsWith("png")){
|
|
|
+ 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 {
|
|
|
+ } else {
|
|
|
standard.setImageStamp(imagePath);
|
|
|
standard.updateAp();
|
|
|
}
|
|
@@ -574,7 +524,6 @@ public class AnnotationTest extends PDFSamples {
|
|
|
*/
|
|
|
private void printAnnotationList() {
|
|
|
printDividingLine();
|
|
|
- outputListener.println("Samples 2 : print annotation list");
|
|
|
File sampleFile = new File(outputDir(), "AnnotationTest/CreateAnnotationTest.pdf");
|
|
|
CPDFDocument document = new CPDFDocument(context);
|
|
|
document.open(sampleFile.getAbsolutePath());
|
|
@@ -588,7 +537,7 @@ public class AnnotationTest extends PDFSamples {
|
|
|
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("--------------------------------------------");
|
|
|
+ printDividingLine();
|
|
|
}
|
|
|
}
|
|
|
document.close();
|
|
@@ -598,7 +547,6 @@ public class AnnotationTest extends PDFSamples {
|
|
|
* Samples 11 : delete annotation
|
|
|
*/
|
|
|
private void deleteAnnotation() {
|
|
|
- outputListener.println("Samples 3: delete annotation");
|
|
|
File sampleFile = new File(outputDir(), "AnnotationTest/CreateAnnotationTest.pdf");
|
|
|
CPDFDocument document = new CPDFDocument(context);
|
|
|
document.open(sampleFile.getAbsolutePath());
|
|
@@ -613,6 +561,7 @@ public class AnnotationTest extends PDFSamples {
|
|
|
|
|
|
File file = new File(outputDir(), "AnnotationTest/DeleteAnnotationTest.pdf");
|
|
|
saveSamplePDF(document, file, true);
|
|
|
+ outputListener.println("Done.");
|
|
|
outputListener.println("Done. Results saved in DeleteAnnotationTest.pdf");
|
|
|
}
|
|
|
}
|