Itext更改书签缩放级别以继承现有pdf的缩放 [英] Itext change bookmarks zoom level to inherit zoom in existing pdf
问题描述
使用 iText5 PDF库,我能够在PDF中读取书签。现在我想使用iText在现有PDF中更改书签的缩放级别
(继承缩放)。
是否可以使用iText PDF库,以及如何使用?
Using the iText5 PDF library, I am able to read bookmarks exist in PDF. Now I want to change the zoom level of bookmarks
(Inherit zoom) in existing PDF using iText.
Is it possible using the iText PDF library, and how?
我附上了截图。
I have attached a screenshot.
这个是我用来更改书签缩放级别的代码(根据@lowagie评论):
This is the code I am using to change the bookmark zoom level (as per @lowagie comment):
public void changeList(List<HashMap<String, Object>> list) {
for (HashMap<String, Object> entry : list) {
for (String key : entry.keySet()) {
System.out.println(key);
if ("Kids".equals(key)) {
Object o = entry.get(key);
changeList((List<HashMap<String, Object>>) o);
} else if ("Page".equals(key)) {
String dest = (String) entry.get(key);
entry.put("Page", dest.replaceAll("Fit", "XYZ 30 100 0"));
}
}
}
}
书签树形结构:
Bookmark Tree Structure :
推荐答案
请看看我对以下问题的回答:将继承缩放(动作属性)设置为pdf文件中的书签
Please take a look at my answer to the following question: Set inherit Zoom(action property) to bookmark in the pdf file
在该答案中,我读了使用 SimpleBookmark
对象的书签。这可能与您阅读书签的方式相同。结果是 List
的 HashMap
对象,它们代表一个大纲树。我使用递归方法查找所有Page
条目。
In that answer, I read bookmarks using the SimpleBookmark
object. That's probably the same way you are reading bookmarks. The result is a List
of HashMap
objects that represents an outline tree. I use a recursive method to find all the "Page"
entries.
这些页面条目包含目的地,实例适合
, FitH
, XYZ
,...你需要将所有这些引用更改为 XYZ
引用,其中您将缩放系数设置为0.缩放系数0相当于继承缩放。
These page entries contain a destination, for instance Fit
, FitH
, XYZ
,... You need to change all these references into XYZ
references of which you set the zoom factor to 0. A zoom factor 0 is the equivalent of 'Inherit Zoom'.
在回答将继承缩放(动作属性)设置为pdf文件中的书签我有以下代码:
public void changeList(List<HashMap<String, Object>> list) {
for (HashMap<String, Object> entry : list) {
for (String key : entry.keySet()) {
if ("Kids".equals(key)) {
Object o = entry.get(key);
changeList((List<HashMap<String, Object>>)o);
}
else if ("Page".equals(key)) {
String dest = (String)entry.get(key);
entry.put("Page", dest.replaceAll("Fit", "FitV 60"));
}
}
}
}
这是很明显,您可以通过全新的操作替换任何现有的页面操作。例如:
It's obvious that you can replace any existing "Page" action by a completely new action. For instance:
entry.put("Page", "XYZ 30 100 0");
现在,您将使用继承的缩放系数跳转到页面上的坐标30 100。如果要放大其他部分,例如与原始位置或多或少对应的位置,则需要检查原始位置中 FitR
之后的坐标PDF。有4:左下x,左下y,右上x和右上y坐标。您可以使用这些值的原始目标解析字符串,并将这些坐标中的两个重新用作 XYZ
目标中的X和Y值。
Now you'll jump to the coordinate 30 100 on the page with an inherited zoom factor. If you want to zoom in on another part, for instance a location that corresponds more or less with the original location, you need to examine the coordinates that followed the FitR
in your original PDF. There are 4: the lower-left x, lower-left y, upper-right x and upper-right y coordinate. You could parse the string with the original destination for those values and reuse two of these coordinates as your X and Y values in the XYZ
destination.
与XYZ目的地一起传递的XY坐标是左上角的坐标。例如,如果你有 FitR -3 234 486 627
,那么你放大一个坐标为llx = -3,lly = 234,urx = 486和ury =的矩形627(ll指的是左下角; ur指的是右上角)。左上角是llx,或者在你的情况下x = -3和y = 627.简而言之:在这种情况下,你需要XYZ -3 627 0。
The X Y coordinate passed with the XYZ destination is the coordinate of the top-left corner. For instance, if you have FitR -3 234 486 627
, then you zoom in on a rectangle with coordinates llx = -3, lly = 234, urx = 486 and ury = 627 (ll refers to lower-left; ur refers to upper-right). The top left corner is llx,ury or in your case x = -3 and y = 627. In short: in this case, you'd need "XYZ -3 627 0".
这篇关于Itext更改书签缩放级别以继承现有pdf的缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!