itext更改所有超链接的缩放级别以继承现有pdf的缩放 [英] itext change zoom level of all hyper link to inherit zoom in existing pdf

查看:385
本文介绍了itext更改所有超链接的缩放级别以继承现有pdf的缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码将所有超链接的缩放级别更改为继承缩放,但无法更改。

I am using following code to change zoom level of all hyperlinks to inherit zoom, but unable to change.

可能是我有在PdfName.DEST和条件中犯了一些错误,因为第一页的pdf中没有DEST数组(查看屏幕截图)。

may be i have made some mistake in PdfName.DEST and condition because there is no array of DEST in pdf for first page( check out screen shot).

for (int count = 0; count < reader.getNumberOfPages(); count++) {
        PdfDictionary page = reader.getPageN(count+1);
        PdfArray annots = page.getAsArray(PdfName.ANNOTS);
        if (annots != null) {
            for (int i = 0; i < annots.size(); i++) {
                PdfDictionary annotation = annots.getAsDict(i);
                if (PdfName.LINK.equals(annotation.getAsName(PdfName.SUBTYPE))) {
                     if(annotation.get(PdfName.A)==null){
                          continue;
                     }
                    PdfArray d = annotation.getAsArray(PdfName.DEST);
                    if (d != null && d.size() == 5 && PdfName.XYZ.equals(d.getAsName(1)))
                        d.set(4, new PdfNumber(0));
                }
            }
        }
    }

I在第一页到第二页创建了链接,查看图像中的结构。

I have created link in first page to second page, check out structure in image.

我还尝试使用以下代码...我有调试并检查 d的值每次为空

I have also try to use following code... i have debug and check value of d is null every time

for (int count = 0; count < reader.getNumberOfPages(); count++) {
            PdfDictionary page = reader.getPageN(count+1);
            PdfArray annots = page.getAsArray(PdfName.ANNOTS);
            if (annots != null) {
                for (int i = 0; i < annots.size(); i++) {
                    PdfDictionary annotation = annots.getAsDict(i);
                    if (PdfName.LINK.equals(annotation.getAsName(PdfName.SUBTYPE))) {

                        PdfArray d = annotation.getAsArray(PdfName.DEST);
                        // d is null every time
                    }
                }
            }
        }


推荐答案

添加一个屏幕截图,可以查看PDF内部。在此屏幕截图中,您清楚地看到目的地是 [5 0 R,/ FitH,795] 。但是,您只更改包含5个值并且类型为 / XYZ 的目标的缩放系数。

You add a screen shot that allows you to look inside the PDF. In this screen shot, you clearly see that the destination is [5 0 R, /FitH, 795]. However, you only change the zoom factor of destinations that consist of 5 values and that are of type /XYZ.

 if (d != null && d.size() == 5 && PdfName.XYZ.equals(d.getAsName(1)))
      d.set(4, new PdfNumber(0));

[5 0 R,/ FitH,795] 有3个元素(换句话说 d.size()== 5 false )和第一个条目在数组中是 PdfName.FITH (换句话说, PdfName.XYZ.equals(d.getAsName(1)是虚假)。

[5 0 R, /FitH, 795] has 3 elements (in other words d.size() == 5 is false) and the first entry in the array is PdfName.FITH (in other words PdfName.XYZ.equals(d.getAsName(1) is false).

你的问题应该被低估,因为你展示了一件事,然后做了别的事情,然后问我们为什么你没有得到预期的结果。好像有人为你提供汤,你在桌子上看到一把勺子和一把叉子。虽然你知道汤应该用勺子吃,你拿叉子然后抱怨你不能吃汤用叉子。那是......很奇怪。

Your question should be down-voted because you show one thing, then do something else, and then ask us why you don't get the expected result. It's as if somebody serves you soup and you see a spoon and a fork on the table. In spite of the fact that you know that soup should be eaten with a spoon, you take the fork and then complain that you can't eat soup with a fork. That's... odd.

更新在评论中,你抱怨 d null ,但是看着你的屏幕截图,你已经知道 d 是空的。

Update In the comments, you complain that d is null, but looking at your screen shot, you already knew that d is null.

你遍历页面,你得到第1页(对象4)的页面字典。你检查是否这是这个页面字典中的 / Annots 条目。有(对象40)。你循环遍历annots并遇到一个注释字典(对象30或39,屏幕截图的分辨率对我来说太糟糕了)。您检查 / Subtype 是否链接。它是。您检查注释是否具有 / A 条目。它有。然后你得到 / Dest 条目。没有这样的条目。因此,你得到 null 。目的地可以在这里找到:

You loop over the pages and you get the page dictionary for page 1 (Object 4). You check if there's an /Annots entry in this page dictionary. There is (object 40). You loop over the annots and you encounter an annotation dictionary (object 30 or 39, the resolution of the screen shot is too bad for me to see). You check if the /Subtype is Link. It is. You check if the annotation has an /A entry. It has. Then you get the /Dest entry. There is no such entry. Hence you get null. The destination is to be found here:

PdfDictionary action = annotation.get(PdfName.A);
if (action != null) {
    PdfArray d = action.getAsArray(PdfName.D);
    // now examine and change D
}

请注意,你在问SO社区在您的位置编写您的应用程序。您获得此解决方案的报酬,但您希望其他人在您的位置上完成您的工作。此外:您可能没有iText的商业许可......

Note that you are asking the SO community to write your application in your place. You are paid for this solution, but you expect that other people do your work in your place. Moreover: you probably don't have a commercial license for iText...

这篇关于itext更改所有超链接的缩放级别以继承现有pdf的缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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