You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Hyperlinks.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hslf.examples;
  16. import java.io.FileInputStream;
  17. import java.util.List;
  18. import org.apache.poi.hslf.usermodel.HSLFHyperlink;
  19. import org.apache.poi.hslf.usermodel.HSLFShape;
  20. import org.apache.poi.hslf.usermodel.HSLFSlide;
  21. import org.apache.poi.hslf.usermodel.HSLFSlideShow;
  22. import org.apache.poi.hslf.usermodel.HSLFTextParagraph;
  23. /**
  24. * Demonstrates how to read hyperlinks from a presentation
  25. *
  26. * @author Yegor Kozlov
  27. */
  28. public final class Hyperlinks {
  29. public static void main(String[] args) throws Exception {
  30. for (int i = 0; i < args.length; i++) {
  31. FileInputStream is = new FileInputStream(args[i]);
  32. HSLFSlideShow ppt = new HSLFSlideShow(is);
  33. is.close();
  34. for (HSLFSlide slide : ppt.getSlides()) {
  35. System.out.println("\nslide " + slide.getSlideNumber());
  36. // read hyperlinks from the slide's text runs
  37. System.out.println("- reading hyperlinks from the text runs");
  38. for (List<HSLFTextParagraph> txtParas : slide.getTextParagraphs()) {
  39. List<HSLFHyperlink> links = HSLFHyperlink.find(txtParas);
  40. String text = HSLFTextParagraph.getRawText(txtParas);
  41. for (HSLFHyperlink link : links) {
  42. System.out.println(toStr(link, text));
  43. }
  44. }
  45. // in PowerPoint you can assign a hyperlink to a shape without text,
  46. // for example to a Line object. The code below demonstrates how to
  47. // read such hyperlinks
  48. System.out.println("- reading hyperlinks from the slide's shapes");
  49. for (HSLFShape sh : slide.getShapes()) {
  50. HSLFHyperlink link = HSLFHyperlink.find(sh);
  51. if (link == null) continue;
  52. System.out.println(toStr(link, null));
  53. }
  54. }
  55. }
  56. }
  57. static String toStr(HSLFHyperlink link, String rawText) {
  58. //in ppt end index is inclusive
  59. String formatStr = "title: %1$s, address: %2$s" + (rawText == null ? "" : ", start: %3$s, end: %4$s, substring: %5$s");
  60. String substring = (rawText == null) ? "" : rawText.substring(link.getStartIndex(), link.getEndIndex()-1);
  61. return String.format(formatStr, link.getLabel(), link.getAddress(), link.getStartIndex(), link.getEndIndex(), substring);
  62. }
  63. }