تحديث روابط PDF في Java

يمكن تعديل الروابط الموجودة عن طريق العثور على تعليق الارتباط في الصفحة وتحديث إما مظهره أو إجراءه.

تحديث لون النص المرتبط

استخدم هذا المثال عندما يجب إعادة تلوين منطقة النص التي يغطيها تعليق الارتباط.

  1. افتح ملف PDF المصدر Document.
  2. ابحث عن تعليقات الروابط وأنشئ مستطيل بحث نصي من كل منطقة تعليقة.
  3. أعد تلوين مقاطع النص المتطابقة واحفظ المستند.
public static void linkAnnotationUpdateTextColor(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Link) {
                TextFragmentAbsorber absorber = new TextFragmentAbsorber();
                Rectangle rect = annotation.getRect();
                rect.setLLX(rect.getLLX() - 2);
                rect.setLLY(rect.getLLY() - 2);
                rect.setURX(rect.getURX() + 2);
                rect.setURY(rect.getURY() + 2);
                absorber.setTextSearchOptions(new TextSearchOptions(rect));
                absorber.visit(document.getPages().get_Item(1));
                for (TextFragment textFragment : absorber.getTextFragments()) {
                    textFragment.getTextState().setForegroundColor(Color.getRed());
                }
            }
        }

        document.save(outputFile.toString());
    }
}

تحديث لون حدود الرابط

استخدم هذا المثال عندما يجب تغيير اللون المرئي لتعليقات الروابط الموجودة.

  1. افتح ملف PDF المصدر Document.
  2. مرّ على تعليقات الصفحة وتصفية لـ الكائنات LinkAnnotation.
  3. حدّث لون التعليق التوضيحي للارتباط واحفظ المستند.
public static void linkAnnotationUpdateBorder(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Link && annotation instanceof LinkAnnotation) {
                LinkAnnotation linkAnnotation = (LinkAnnotation) annotation;
                linkAnnotation.setColor(Color.getRed());
            }
        }

        document.save(outputFile.toString());
    }
}

تحديث وجهة رابط الويب

استخدم هذا المثال عندما ينبغي لرابط ويب موجود أن يشير إلى URI جديد.

  1. افتح ملف PDF المصدر Document.
  2. ابحث عن تعليقات الارتباط التي يكون الإجراء الخاص بها GoToURIAction.
  3. استبدل عنوان URI واحفظ المستند المحدث.
public static void linkAnnotationUpdateWebDestination(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Link && annotation instanceof LinkAnnotation) {
                LinkAnnotation linkAnnotation = (LinkAnnotation) annotation;
                if (linkAnnotation.getAction() instanceof GoToURIAction) {
                    GoToURIAction action = (GoToURIAction) linkAnnotation.getAction();
                    action.setURI("https://www.aspose.com");
                }
            }
        }
        document.save(outputFile.toString());
    }
}