当您使用SDK搜索关键字的功能后,想要对关键字再做一些处理,比如:加上高亮。使得关键字在文档中更加容易被识别。示例代码如下:
PDFPage page = doc.getPage(8);
// Parse PDF page.
page.startParse(PDFPage.e_ParsePageNormal, null, false);
TextPage text_page = new TextPage(page, TextPage.e_ParseTextNormal);
TextSearch text_search = new TextSearch(text_page);
//setSearchFlags
text_search.setPattern("关键字");
RectFArray matched_rect_array = new RectFArray();
while (text_search.findNext()) {
RectFArray temp_rect_array = text_search.getMatchRects();
for (int z = 0; z < temp_rect_array.getSize(); z++) {
Highlight highlight = new Highlight(page.addAnnot(Annot.e_Highlight, temp_rect_array.getAt(z)));
highlight.setFlags(e_FlagPrint);
QuadPoints quad_points = new QuadPoints();
quad_points.setFirst(new PointF(temp_rect_array.getAt(z).getLeft(), temp_rect_array.getAt(z).getTop()));
quad_points.setSecond(new PointF(temp_rect_array.getAt(z).getRight(), temp_rect_array.getAt(z).getTop()));
quad_points.setThird(new PointF(temp_rect_array.getAt(z).getLeft(), temp_rect_array.getAt(z).getBottom()));
quad_points.setFourth(new PointF(temp_rect_array.getAt(z).getRight(), temp_rect_array.getAt(z).getBottom()));
QuadPointsArray quad_points_array = new QuadPointsArray();
quad_points_array.add(quad_points);
highlight.setQuadPoints(quad_points_array);
highlight.setContent("Highlight");
highlight.setSubject("Highlight");
highlight.setTitle("Foxit SDK");
highlight.setBorderColor(0x00FF00);
// Appearance should be reset.
highlight.resetAppearanceStream();
}
}
doc.saveAs( "KeysHighlight.pdf", PDFDoc.e_SaveFlagNormal);