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.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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.examples.hslf;
  16. import java.io.FileInputStream;
  17. import java.util.List;
  18. import java.util.Locale;
  19. import org.apache.poi.hslf.usermodel.HSLFHyperlink;
  20. import org.apache.poi.hslf.usermodel.HSLFSimpleShape;
  21. import org.apache.poi.hslf.usermodel.HSLFSlide;
  22. import org.apache.poi.hslf.usermodel.HSLFSlideShow;
  23. import org.apache.poi.hslf.usermodel.HSLFTextParagraph;
  24. import org.apache.poi.hslf.usermodel.HSLFTextRun;
  25. /**
  26. * Demonstrates how to read hyperlinks from a presentation
  27. */
  28. @SuppressWarnings({"java:S106", "java:S4823"})
  29. public final class Hyperlinks {
  30. private Hyperlinks() {}
  31. public static void main(String[] args) throws Exception {
  32. for (String arg : args) {
  33. try (FileInputStream is = new FileInputStream(arg);
  34. HSLFSlideShow ppt = new HSLFSlideShow(is)) {
  35. for (HSLFSlide slide : ppt.getSlides()) {
  36. System.out.println("\nslide " + slide.getSlideNumber());
  37. // read hyperlinks from the slide's text runs
  38. System.out.println("- reading hyperlinks from the text runs");
  39. slide.getTextParagraphs().stream().
  40. flatMap(List::stream).
  41. map(HSLFTextParagraph::getTextRuns).
  42. flatMap(List::stream).
  43. forEach(run -> out(run.getHyperlink(), run));
  44. // in PowerPoint you can assign a hyperlink to a shape without text,
  45. // for example to a Line object. The code below demonstrates how to
  46. // read such hyperlinks
  47. System.out.println("- reading hyperlinks from the slide's shapes");
  48. slide.getShapes().stream().
  49. filter(sh -> sh instanceof HSLFSimpleShape).
  50. forEach(sh -> out(((HSLFSimpleShape) sh).getHyperlink(), null));
  51. }
  52. }
  53. }
  54. }
  55. private static void out(HSLFHyperlink link, HSLFTextRun run) {
  56. if (link == null) {
  57. return;
  58. }
  59. String rawText = run == null ? null : run.getRawText();
  60. //in ppt end index is inclusive
  61. String formatStr = "title: %1$s, address: %2$s" + (rawText == null ? "" : ", start: %3$s, end: %4$s, substring: %5$s");
  62. String line = String.format(Locale.ROOT, formatStr, link.getLabel(), link.getAddress(), link.getStartIndex(), link.getEndIndex(), rawText);
  63. System.out.println(line);
  64. }
  65. }