使用Aspose API为PDF添加工具提示

时间:2017-06-02 02:03:30

标签: aspose aspose-cells aspose.pdf

我正在尝试在FTL模板文件中添加工具提示文本以生成PDF。谁能帮助我如何添加它? 这是一个示例:

<Table ColumnWidth="100%">
   <row>
       <cell>
           <segment> Hi How are you..!  </segment>
       </cell>
   </row>
</Table>

当我将鼠标悬停在文字上时“你好,你好吗......!”我想添加一个描述。有人可以帮忙。

先谢谢

1 个答案:

答案 0 :(得分:1)

您可以在生成后在PDF中添加注释,并通过为鼠标输入/输出添加JavaScript操作来控制其可见性。这样,它看起来就像一个工具提示。请检查以下代码段,以满足您的要求。

Document doc = new Document(dataDir + "Tooltip.pdf");

// search target text
TextFragmentAbsorber tfa = new TextFragmentAbsorber("Hello World!");
doc.getPages().get_Item(1).accept(tfa);
Page page = doc.getPages().get_Item(1);
String name = "TXTANNOT";
String title = "Tooltip";
String comment = "Hi How Are You?";

// add a text annotation
TextAnnotation text = new TextAnnotation(page, tfa.getTextFragments().get_Item(1).getRectangle());
text.setName(name);
text.setTitle(title);
text.setContents(comment);

// These flags must be raised to suppress showing of annotation icon
text.setFlags(AnnotationFlags.NoView | AnnotationFlags.ReadOnly);
page.getAnnotations().add(text);
com.aspose.pdf.Rectangle popupRect = new com.aspose.pdf.Rectangle(90, 610, 235, 710);

// Add popup annotation
PopupAnnotation popup = new PopupAnnotation(page, popupRect);
page.getAnnotations().add(popup);
text.setPopup(popup);
popup.setParent(text);

Field field = new ButtonField(page, tfa.getTextFragments().get_Item(1).getRectangle());
doc.getForm().add(field);

String fieldName = field.getPartialName();
String openScript = "var t = this.getAnnot(this.pageNum, '" + name + "'); t.popupOpen = true; var w = this.getField('" + fieldName + "'); w.setFocus();";
String closeScript = "var t = this.getAnnot(this.pageNum, '" + name + "'); t.popupOpen = false;";

PdfAction openaction = new JavascriptAction(openScript);
PdfAction closeaction = new JavascriptAction(closeScript);

// set button actions
field.getActions().setOnEnter(openaction);
field.getActions().setOnExit(closeaction);
doc.save(dataDir + "ToolTip_out.pdf");

请按以上代码检查生成的output,以供参考。

PS:我在Aspose担任支持开发人员。