最近做超链接问题,踩过一些坑,记录一下解决方案。
-
插入外部超链接,获取相对路径方法;
public static String relativePath(String str1, String str2) { String path = null; // File sf1 = new File(String.valueOf(validPath(str1))); // File sf2 = new File(String.valueOf(validPath(str2))); File sf1 = new File(str1); File sf2 = new File(str2); // int i=0; String rela = ""; while (sf1.getParentFile() != null) { if (sf2.getPath().contains(sf1.getParent())) { path = sf1.getParent(); break; } sf1 = sf1.getParentFile(); // i++; rela += "../"; } if (!StringUtils.isEmpty(path)) { if (sf2.getPath().equals(path)) { path = rela; } else { path = rela + sf2.getPath().replace(path, "").substring(1); } } path = null == path ? "" : path.replaceAll("\\\\", "/"); try { path = new String(path.getBytes("GBK"), "ISO-8859-1"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return path; }
-
这块获取相对路径 会出现中文乱码情况,这块对路径进行适当的转码,以什么编码格式解码成bytes,再用什么方法编码成string,这块根据对应的输入输出数据选择对应的编码格式。当时我做的时候是这样的需求,要求使用adobe软件可以执行跳转到外部文件的某一页。首先使用adobe软件做一个外部带有中文的,使用aspose获取到该路径的值,这个值有可能是乱码的,也就是说adobe是使用乱码的文字做跳转的,那么我们获取该乱码的字符集,这块可以这样获取:
public static String getEncoding(String str) { String encode = "GB2312"; try { if (str.equals(new String(str.getBytes(encode), encode))) { //判断是不是GB2312 String s = encode; return s; //是的话,返回“GB2312“,以下代码同理 } } catch (Exception exception) { } encode = "ISO-8859-1"; try { if (str.equals(new String(str.getBytes(encode), encode))) { //判断是不是ISO-8859-1 String s1 = encode; return s1; } } catch (Exception exception1) { } encode = "UTF-8"; try { if (str.equals(new String(str.getBytes(encode), encode))) { //判断是不是UTF-8 String s2 = encode; return s2; } } catch (Exception exception2) { } encode = "GBK"; try { if (str.equals(new String(str.getBytes(encode), encode))) { //判断是不是GBK String s3 = encode; return s3; } } catch (Exception exception3) { } return ""; }
这样获取adobe的使用字体,完后获取当前编译器的编码,同样的方式。最后在创建外部超链接的时候,将相对路径以对应的编码解码进行处理。即可实现在adobe的外部中文相对路径跳转。
-
插入外部指定页数的超链接
static void go() { String sourcePath = "C:/Users/cuigu/Desktop/pdfFormate/456/hello.pdf "; String relativePath = "./你好.pdf"; //在页面的哪里想要创建矩形框,四个值将矩形在页面中确定 Rectangle rectangle = new Rectangle(501, 236, 476, 593); //创建Document对象 Document document = new Document(sourcePath); //想在第几页创建 Page page = document.getPages().get_Item(1); //当前页矩形倾斜度,用户横向页面坐标调整 rectangle.rotate(page.getRotate()); //创建链接 LinkAnnotation link = new LinkAnnotation(page, rectangle); //创建目标信息,4代表了要跳转到第目标文件的第几页,0和0代表了左侧和上部距离,0.0代表了 //缩放比例。 XYZExplicitDestination xyzExplicitDestination = new XYZExplicitDestination(4, 0, 0, 0.0); //创建链接动作行为 GoToRemoteAction action = new GoToRemoteAction(relativePath, xyzExplicitDestination); //设置打开窗口 action.setNewWindow(2); //绑定行为 link.setAction(action); //创建超链接矩形边框 Border border = new Border(link); //边框厚度 border.setWidth(0); link.setBorder(border); //设置颜色 link.setColor(Color.getBlue()); //绑定 page.getAnnotations().add(link); //保存 document.save(); document.close(); }
这个例子介绍了创建外部超链接以及设置超链接矩形框的颜色、厚度。这里有个坑,划重点。就是创建成功后,使用adobe软件进行查看时,默认确实是跳转到第四页,可是我们实际使用时会发现却跳转到了第五页,多跳了一页,这块我截止现在的解决办法是这样的:在创建XYZExplicitDestination对象时,将构造方法内的距离顶部的数值调高,我调到了800,也就是这样XYZExplicitDestination xyzExplicitDestination = new XYZExplicitDestination(4, 0, 800, 0.0);这样即满足了承前缩放,又跳转到了指定页数。