【问题标题】:Add Tooltip using Aspose API for a PDF使用 Aspose API 为 PDF 添加工具提示
【发布时间】:2017-06-02 19:36:10
【问题描述】:

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

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

当我将鼠标悬停在“Hi How are you..!”文字上时我想添加描述。有人可以帮忙吗?

提前致谢

【问题讨论】:

    标签: aspose aspose-cells aspose.pdf


    【解决方案1】:

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

    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 担任支持开发人员。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-08
      相关资源
      最近更新 更多