Добавление всплывающих подсказок к тексту PDF в Java
Contents
[
Hide
]
Aspose.PDF for Java позволяет добавлять интерактивную справку, размещая поля формы над текстовыми фрагментами.
Добавление всплывающих подсказок к найденному тексту
Используйте этот пример, когда существующий текст в PDF должен отображать всплывающую подсказку при наведении.
- Создайте образец PDF и откройте его снова для редактирования.
- Найдите целевые фрагменты текста с помощью
TextFragmentAbsorber. - Разместите поля
ButtonFieldповерх найденного текста и задайте текст подсказки. - Сохраните обновлённый документ.
public static void addToolTipToSearchedText(Path outputFile) {
Document document = new Document();
document.getPages().add().getParagraphs()
.add(new TextFragment("Move the mouse cursor here to display a tooltip"));
document.getPages().get_Item(1).getParagraphs()
.add(new TextFragment("Move the mouse cursor here to display a very long tooltip"));
document.save(outputFile.toString());
document.close();
document = new Document(outputFile.toString());
TextFragmentAbsorber absorber = new TextFragmentAbsorber(
"Move the mouse cursor here to display a tooltip");
document.getPages().accept(absorber);
for (TextFragment fragment : absorber.getTextFragments()) {
ButtonField field = new ButtonField(fragment.getPage(), fragment.getRectangle());
field.setAlternateName("Tooltip for text.");
document.getForm().add(field);
}
absorber = new TextFragmentAbsorber("Move the mouse cursor here to display a very long tooltip");
document.getPages().accept(absorber);
for (TextFragment fragment : absorber.getTextFragments()) {
ButtonField field = new ButtonField(fragment.getPage(), fragment.getRectangle());
field.setAlternateName("Lorem ipsum dolor sit amet, consectetur adipiscing elit,"
+ " sed do eiusmod tempor incididunt ut labore et dolore magna"
+ " aliqua. Ut enim ad minim veniam, quis nostrud exercitation"
+ " ullamco laboris nisi ut aliquip ex ea commodo consequat."
+ " Duis aute irure dolor in reprehenderit in voluptate velit"
+ " esse cillum dolore eu fugiat nulla pariatur. Excepteur sint"
+ " occaecat cupidatat non proident, sunt in culpa qui officia"
+ " deserunt mollit anim id est laborum.");
document.getForm().add(field);
}
document.save(outputFile.toString());
document.close();
}
Отображение плавающего текстового блока при наведении
Используйте этот пример, когда при наведении курсора на текстовую область должно отображаться скрытое текстовое поле.
- Создайте образец PDF и откройте его снова для редактирования.
- Найдите триггерный текстовый фрагмент с
TextFragmentAbsorber. - Создайте скрытый
TextBoxFieldиButtonFieldс действиями входа и выхода. - Сохраните окончательный PDF.
public static void createHiddenTextBlock(Path outputFile) {
Document document = new Document();
document.getPages().add().getParagraphs()
.add(new TextFragment("Move the mouse cursor here to display floating text"));
document.save(outputFile.toString());
document.close();
document = new Document(outputFile.toString());
TextFragmentAbsorber absorber = new TextFragmentAbsorber(
"Move the mouse cursor here to display floating text");
document.getPages().accept(absorber);
TextFragment fragment = absorber.getTextFragments().get_Item(1);
TextBoxField floatingField = new TextBoxField(
fragment.getPage(), new Rectangle(100.0, 700.0, 220.0, 740.0, false));
floatingField.setValue("This is the \"floating text field\".");
floatingField.setReadOnly(true);
floatingField.setFlags(floatingField.getFlags() | AnnotationFlags.Hidden);
floatingField.setPartialName("FloatingField_1");
floatingField.setDefaultAppearance(new DefaultAppearance("Helv", 10, java.awt.Color.BLUE));
floatingField.getCharacteristics().setBackground(java.awt.Color.CYAN);
floatingField.getCharacteristics().setBorder(java.awt.Color.BLUE);
floatingField.setBorder(new Border(floatingField));
floatingField.getBorder().setWidth(1);
floatingField.setMultiline(true);
document.getForm().add(floatingField);
ButtonField buttonField = new ButtonField(fragment.getPage(), fragment.getRectangle());
buttonField.getAnnotationActions().setOnEnter(new HideAction(floatingField, false));
buttonField.getAnnotationActions().setOnExit(new HideAction(floatingField));
document.getForm().add(buttonField);
document.save(outputFile.toString());
document.close();
}