在 PDFBox 中,如何使用“翻转"创建链接注释/“鼠标悬停"效果? [英] In PDFBox, how to create a link annotation with "rollover" / "mouse over" effects?

查看:73
本文介绍了在 PDFBox 中,如何使用“翻转"创建链接注释/“鼠标悬停"效果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

使用 PDFBox,如何创建具有鼠标悬停"颜色效果(又名鼠标悬停/鼠标悬停)的链接注释?

这意味着当我将鼠标光标悬停在 PDF 文件中的链接上(不单击它)时,该链接会更改为不同的颜色.如果我将光标移开,链接就会变回原来的颜色.

例如:

我正在寻找的效果类似于stackoverflow网站上的链接.当您将鼠标光标悬停在(不单击)提问"按钮上时,链接会从灰色变为橙色.当您将光标移开时,颜色会变回灰色.例如,请参见下图:我想在 PDF 文件中实现完全相同的效果.

我尝试过的:

在PDF Reference第六版中,是这样描述的:

<块引用>

当用户不按鼠标按钮将光标移动到注释的活动区域时使用翻转外观"

<块引用>

[rollover appearance]定义在一个外观字典中,它又是AP条目在注解字典中的值

还有,

在PDFBox中,有一个PDAppearanceDictionary类,它有一个setRolloverAppearance()方法.

这是我能得到的最远的距离.我不知道如何将 PDAppearanceDictionary 类(如果这确实是要使用的正确类)与 PDAnnotationLink 类结合使用,以达到我想要的结果.

我曾尝试在 Google 上寻找示例,但徒劳无功.

解决方案

简而言之

对于这种翻转效应是否可能存在一些不确定性.使用当前的 Adob​​e PDF 查看器(Reader XI 和 Acrobat 9.5)进行显示时,链接注释没有出现所需的翻转效果.不过,对于按钮小部件(带有相同的 URL 操作),效果确实出现了.

详细

测试代码 将自定义为按钮的 PDAnnotationLinkPDAnnotationWidget 提供给将相应注释嵌入文档并添加正常和翻转外观:

void createRollover(PDAnnotation annotation, String filename) throws IOException, COSVisitorException{PDDocument 文档 = 新的 PDDocument();PDPage 页面 = 新的 PDPage();document.addPage(page);列表<PDAnnotation>注释 = page.getAnnotations();浮动 x = 100;浮动 y = 500;String text = "PDFBox";PDFont 字体 = PDType1Font.HELVETICA_BOLD;float textWidth = font.getStringWidth(text)/1000 * 18;PDPageContentStream 内容 = 新的 PDPageContentStream(document, page);内容.beginText();内容.setFont(字体,18);content.moveTextPositionByAmount(x, y);内容.drawString(文本);内容.endText();内容.关闭();PDAppearanceDictionary appearanceDictionary = new PDAppearanceDictionary();PDAppearanceStream normal = createAppearanceStream(document, textWidth, font, "0.5 0.5 0.5 rg");PDAppearanceStream rollover = createAppearanceStream(document, textWidth, font, "1 0.7 0.5 rg");PDAppearanceStream down = createAppearanceStream(document, textWidth, font, "0 0 0 rg");AppearanceDictionary.setNormalAppearance(normal);AppearanceDictionary.setRolloverAppearance(rollover);AppearanceDictionary.setDownAppearance(down);annotation.setAppearance(appearanceDictionary);PDRectangle 位置 = 新 PDRectangle();position.setLowerLeftX(x);position.setLowerLeftY(y - 5);position.setUpperRightX(x + textWidth);position.setUpperRightY(y + 20);annotation.setRectangle(position);annotations.add(annotation);文件.保存(新文件(RESULT_FOLDER,文件名));文档.close();}

如果是 PDAnnotationLink:

如果是按钮PDAnnotationWidget:

背景:

他的问题中的 OP 和评论中的 @Tilman 提到了 PDF 规范:

<块引用><块引用>

一个注解可以定义多达三种不同的外观:[...]

• 当用户将光标移动到注释的活动区域而不按下鼠标按钮时,应使用 rollover 外观.[...]

正常、翻转和向下的外观应在外观字典中定义,这又是注释字典中AP条目的值

因此,认为:

<块引用>

所以应该可以

不过,他们没有考虑到规范将外观字典介绍为:

<块引用><块引用>

AP 字典 (可选;PDF 1.2) 一个外观字典,指定注释在页面上的视觉呈现方式(参见 12.5.5,外观流").个别注释处理程序可能会忽略此条目并提供自己的外观.

因此,如果 PDF 查看器中的注释处理程序具有自己的想法.

tl;dr:完全由相关 PDF 查看器决定它使用哪些外观流以及它以自己的方式忽略和替换哪些外观流.

如果使用注释外观流,则应始终确保在忽略给定外观的情况下也提供最有可能使用的信息,例如在链接注释下方有常规页面内容.

Question:

With PDFBox, how can I create a link annotation with "mouse over" color effect (aka rollover / mouse hover)?

It means that when I hover my mouse cursor over a link in a PDF file (without clicking it), the link changes to a different color. And if I mouse the cursor away, the link changes backs to the original color.

For example:

The effect that I am looking for is similar to the links at stackoverflow website. When you hover the mouse cursor over (without clicking) the "Ask Question" button, the link changes from grey to orange. When you move the cursor away, the color changes back to grey. See following picture for example: I want to achieve exactly the same effect in a PDF file.

What I have tried:

In PDF Reference Sixth Edition, it is described that:

the rollover appearance is used when the user moves the cursor into the annotation’s active area without pressing the mouse button"

and

[rollover appearance] are defined in an appearance dictionary, which in turn is the value of the AP entry in the annotation dictionary

Also,

In the PDFBox, there is a PDAppearanceDictionary class, which has a setRolloverAppearance() method.

This is the farthest I can get. I don't know how to use PDAppearanceDictionary class (if this is indeed the right class to use) in conjunction with a PDAnnotationLink class, in order to achieve my desired result.

I have tried finding examples on Google in vain.

解决方案

In short

There was some uncertainty about whether or not such a rollover effect is possible. Using fairly current Adobe PDF viewers (Reader XI and Acrobat 9.5) for displaying, the desired rollover effect did not turn up for a link annotation. The effect did turn up, though, for a button widget (carrying the same URL action).

In detail

The test code feeds either a PDAnnotationLink or a PDAnnotationWidget customized as a pushbutton to a method which embeds the respective annotation in a document and adds normal and rollover appearances to it:

void createRollover(PDAnnotation annotation, String filename) throws IOException, COSVisitorException
{
    PDDocument document = new PDDocument();
    PDPage page = new PDPage();
    document.addPage(page);
    List<PDAnnotation> annotations = page.getAnnotations();

    float x = 100;
    float y = 500;
    String text = "PDFBox";
    PDFont font = PDType1Font.HELVETICA_BOLD;
    float textWidth = font.getStringWidth(text) / 1000 * 18;

    PDPageContentStream contents = new PDPageContentStream(document, page);
    contents.beginText();
    contents.setFont(font, 18);
    contents.moveTextPositionByAmount(x, y);
    contents.drawString(text);
    contents.endText();
    contents.close();

    PDAppearanceDictionary appearanceDictionary = new PDAppearanceDictionary();
    PDAppearanceStream normal = createAppearanceStream(document, textWidth, font, "0.5 0.5 0.5 rg");
    PDAppearanceStream rollover = createAppearanceStream(document, textWidth, font, "1 0.7 0.5 rg");
    PDAppearanceStream down = createAppearanceStream(document, textWidth, font, "0 0 0 rg");
    appearanceDictionary.setNormalAppearance(normal);
    appearanceDictionary.setRolloverAppearance(rollover);
    appearanceDictionary.setDownAppearance(down);
    annotation.setAppearance(appearanceDictionary);

    PDRectangle position = new PDRectangle();
    position.setLowerLeftX(x);
    position.setLowerLeftY(y - 5);
    position.setUpperRightX(x + textWidth);
    position.setUpperRightY(y + 20);
    annotation.setRectangle(position);

    annotations.add(annotation);
    document.save(new File(RESULT_FOLDER, filename));
    document.close();
}

In case of the PDAnnotationLink:

In case of the pushbutton PDAnnotationWidget:

Backgrounds:

The OP in his question and @Tilman in a comment referred to the PDF specification:

An annotation may define as many as three separate appearances: [...]

• The rollover appearance shall be used when the user moves the cursor into the annotation’s active area without pressing the mouse button. [...]

The normal, rollover, and down appearances shall be defined in an appearance dictionary, which in turn is the value of the AP entry in the annotation dictionary

and, therefore, thought:

So it should be possible

They did not consider, though, that the specification introduces the appearance dictionary as:

AP dictionary (Optional; PDF 1.2) An appearance dictionary specifying how the annotation shall be presented visually on the page (see 12.5.5, "Appearance Streams"). Individual annotation handlers may ignore this entry and provide their own appearances.

Thus, what at first glance seemed to be an unconditional requirement ("The rollover appearance shall be used when...") turns out to be ignorable if the annotation handler in a PDF viewer has its own ideas.

tl;dr: it is completely up to the PDF viewer in question to decide which appearance streams it uses and which it ignores and replaces in its own ways.

If making use of annotation appearance streams, one should always make sure that one also supplies the information most plausibly used if the given appearances are ignored, e.g. having regular page content beneath a link annotation.

这篇关于在 PDFBox 中,如何使用“翻转"创建链接注释/“鼠标悬停"效果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆