When previewing a PDF document, you can highlight the rectangular area or text on the page and customize the highlight color.
You can use setHighlightText to highlight multiple different text blocks at the same time.

| API Name | Description |
|---|---|
| setHighlightText(pageIndex: number, textArray: string[], color: number): void | Highlights the specified text. |
Loads a PDF document.
Call the PdfView preview component to render the display.
Call the setHighlightText method via the setHighlightText button, to set one or more text blocks to be highlighted.
import { pdfService, PdfView, pdfViewManager } from '@kit.PDFKit';
// ...
@Entry
@Component
struct HighlightPage {
private controller: pdfViewManager.PdfController = new pdfViewManager.PdfController();
private context = this.getUIContext().getHostContext() as Context;
private loadResult: pdfService.ParseResult = pdfService.ParseResult.PARSE_ERROR_FORMAT;
aboutToAppear(): void {
// Ensure that the input.pdf document exists in the src/main/resources/resfile project directory.
let filePath = this.context.resourceDir + '/input.pdf';
(async () => {
this.loadResult = await this.controller.loadDocument(filePath);
})()
}
build() {
// ...
Column() {
Row() {
// Set the highlight style of the text.
Button('setHighlightText').onClick(async () => {
if (this.loadResult === pdfService.ParseResult.PARSE_SUCCESS) {
this.controller.setHighlightText(0, ['PDF'], 0xAAF9CC00);
}
})
}
// Load the PdfView component for preview.
PdfView({
controller: this.controller,
pageFit: pdfService.PageFit.FIT_WIDTH,
showScroll: true
})
.id('pdfview_app_view')
.layoutWeight(1);
}
// ...
.width('100%').height('100%')
}
}