|
| 1 | +import type { RedactionAnnotation, TextLine } from "@nutrient-sdk/viewer"; |
| 2 | +import { baseOptions } from "../../shared/base-options"; |
| 3 | + |
| 4 | +const REDACTION_CONFIG = { |
| 5 | + color: window.NutrientViewer.Color.RED, |
| 6 | + overlayText: "REDACTED", |
| 7 | +}; |
| 8 | + |
| 9 | +function createRedactionFromTextLine(textLine: TextLine) { |
| 10 | + const { boundingBox, pageIndex } = textLine; |
| 11 | + |
| 12 | + return new window.NutrientViewer.Annotations.RedactionAnnotation({ |
| 13 | + id: window.NutrientViewer.generateInstantId(), |
| 14 | + pageIndex, |
| 15 | + boundingBox, |
| 16 | + rects: window.NutrientViewer.Immutable.List([boundingBox]), |
| 17 | + ...REDACTION_CONFIG, |
| 18 | + }); |
| 19 | +} |
| 20 | + |
| 21 | +window.NutrientViewer.load({ |
| 22 | + ...baseOptions, |
| 23 | + theme: window.NutrientViewer.Theme.DARK, |
| 24 | +}).then(async (instance) => { |
| 25 | + const pageIndices = Array.from({ length: instance.totalPageCount }); |
| 26 | + |
| 27 | + const allPagesTextLines = await Promise.all( |
| 28 | + pageIndices.map((_, index) => instance.textLinesForPageIndex(index)), |
| 29 | + ); |
| 30 | + |
| 31 | + const redactionAnnotations = allPagesTextLines.flatMap((pageTextLines) => |
| 32 | + pageTextLines.reduce<RedactionAnnotation[]>( |
| 33 | + (annotations, textLine) => [ |
| 34 | + ...annotations, |
| 35 | + createRedactionFromTextLine(textLine), |
| 36 | + ], |
| 37 | + [], |
| 38 | + ), |
| 39 | + ); |
| 40 | + |
| 41 | + await instance.create(redactionAnnotations); |
| 42 | + await instance.applyRedactions(); |
| 43 | +}); |
0 commit comments