التعليقات التوضيحية الأمنية باستخدام Java
Contents
[
Hide
]
سير عمل تعليقات الأمان في هذا القسم يركز على إعداد وتطبيق الحذف على محتوى PDF الحساس.
تمييز النص باستخدام تعليقات الحذف
استخدم هذا المثال عندما يجب تغطية النص المطابق بتعليقات الحذف قبل تطبيق الحذف بشكل دائم.
- افتح ملف PDF المصدر Document.
- ابحث عن النص المستهدف وأنشئ كائنًا من الفئة RedactionAnnotation لكل تطابق.
- اضبط مظهر الحجب واحفظ المستند.
public static void markTextRedaction(Path inputFile, Path outputFile, String searchTerm) {
try (Document document = new Document(inputFile.toString())) {
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(searchTerm);
TextSearchOptions textSearchOptions = new TextSearchOptions(true);
textFragmentAbsorber.setTextSearchOptions(textSearchOptions);
document.getPages().accept(textFragmentAbsorber);
for (var textFragment : textFragmentAbsorber.getTextFragments()) {
Page page = textFragment.getPage();
RedactionAnnotation redactionAnnotation = new RedactionAnnotation(page, textFragment.getRectangle());
redactionAnnotation.setFillColor(Color.getGray());
redactionAnnotation.setBorderColor(Color.getRed());
redactionAnnotation.setColor(Color.getWhite());
redactionAnnotation.setOverlayText("REDACTED");
redactionAnnotation.setTextAlignment(HorizontalAlignment.Center);
redactionAnnotation.setRepeat(true);
page.getAnnotations().add(redactionAnnotation, true);
}
document.save(outputFile.toString());
}
}
تطبيق الحجب الموجودة
هذا المثال يطبق بشكل دائم تعليقات الحجب التي موجودة بالفعل على الصفحة.
- افتح ملف PDF المصدر Document.
- اجمع التعليقات من النوع AnnotationType.
Redaction. - استدعِ
redact()على كل ملاحظة تم جمعها واحفظ الملف المحدث.
public static void applyRedaction(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
List<RedactionAnnotation> redactionAnnotations = new ArrayList<>();
for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
if (annotation.getAnnotationType() == AnnotationType.Redaction) {
redactionAnnotations.add((RedactionAnnotation) annotation);
}
}
for (RedactionAnnotation redactionAnnotation : redactionAnnotations) {
redactionAnnotation.redact();
}
document.save(outputFile.toString());
}
}
إخفاء منطقة مختارة من الصفحة
استخدم هذا النهج عندما يتم تحديد المحتوى المستهدف حسب الموقع بدلاً من مطابقة النص.
- افتح ملف PDF المصدر Document.
- اكتشف المستطيل المستهدف على الصفحة، على سبيل المثال من موضع صورة.
- أنشئ كائنًا من الفئة RedactionAnnotation لتلك المنطقة واحفظ المستند.
public static void redactArea(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
ImagePlacementAbsorber imagePlacementAbsorber = new ImagePlacementAbsorber();
Page page = document.getPages().get_Item(1);
page.accept(imagePlacementAbsorber);
com.aspose.pdf.Rectangle targetRect = imagePlacementAbsorber.getImagePlacements().get_Item(2).getRectangle();
RedactionAnnotation redactionAnnotation = new RedactionAnnotation(page, targetRect);
redactionAnnotation.setFillColor(Color.getGray());
redactionAnnotation.setBorderColor(Color.getRed());
redactionAnnotation.setColor(Color.getWhite());
redactionAnnotation.setOverlayText("REDACTED");
redactionAnnotation.setTextAlignment(HorizontalAlignment.Center);
redactionAnnotation.setRepeat(true);
page.getAnnotations().add(redactionAnnotation, true);
document.save(outputFile.toString());
}
}