public void highlight(
      final Pattern searchText, final Pattern markingPattern, Color color, int pageNr) {
    if (textCache == null || document == null) {
      throw new IllegalArgumentException("TextCache was not initialized");
    }

    final List<PDPage> pages = document.getDocumentCatalog().getAllPages();

    try {
      boolean found = false;

      final PDPage page = pages.get(pageNr - 1);
      PDPageContentStream contentStream = new PDPageContentStream(document, page, true, true);

      PDExtendedGraphicsState graphicsState = new PDExtendedGraphicsState();
      graphicsState.setNonStrokingAlphaConstant(0.5f);
      PDResources resources = page.findResources();
      Map graphicsStateDictionary = resources.getGraphicsStates();
      if (graphicsStateDictionary == null) {
        // There is no graphics state dictionary in the resources dictionary, create one.
        graphicsStateDictionary = new TreeMap();
      }
      graphicsStateDictionary.put("highlights", graphicsState);
      resources.setGraphicsStates(graphicsStateDictionary);

      for (Match searchMatch : textCache.match(pageNr, searchText)) {
        if (textCache.match(searchMatch.positions, markingPattern).size() > 0) {
          for (Match markingMatch : textCache.match(searchMatch.positions, markingPattern)) {
            if (markupMatch(color, contentStream, markingMatch)) {
              found = true;
            }
          }
        } else {
          System.out.println(
              "Cannot highlight: " + markingPattern.pattern() + " on page " + (pageNr - 1));
        }
        if (found) {
          break;
        }
      }
      contentStream.close();
    } catch (Exception e) {
      e.printStackTrace();
    } catch (Error e1) {
      e1.printStackTrace();
      throw e1;
    }
  }
Exemple #2
0
 public static void assumeNotHeadless() {
   boolean headless = true;
   try {
     GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
     headless = ge.isHeadless();
   } catch (Exception e) {
     e.printStackTrace();
   } catch (Error e) {
     // Really not sure why this ever happens, maybe just jenkins issues
     e.printStackTrace();
   }
   if (headless) {
     System.out.println("You are trying to start a GUI in a headless environment. Aborting test");
   }
   org.junit.Assume.assumeTrue(!headless);
 }
  /*
   * The following methods are overwritten from the PDTextStripper
   */
  public void initialize(final PDDocument pdf) throws IOException {
    try {
      resetEngine();
      document = pdf;
      textCache = new TextCache();

      if (getAddMoreFormatting()) {
        setParagraphEnd(getLineSeparator());
        setPageStart(getLineSeparator());
        setArticleStart(getLineSeparator());
        setArticleEnd(getLineSeparator());
      }
      startDocument(pdf);
      processPages(pdf.getDocumentCatalog().getAllPages());
      endDocument(pdf);
    } catch (Exception e) {
      e.printStackTrace();
    } catch (Error e1) {
      e1.printStackTrace();
    }
  }
    public List<Match> match(List<TextPosition> textPositions, String text, final Pattern pattern) {
      try {
        final Matcher matcher = pattern.matcher(text);
        final List<Match> matches = new ArrayList<Match>();

        while (matcher.find()) {
          final List<TextPosition> elements = textPositions.subList(matcher.start(), matcher.end());
          matches.add(new Match(matcher.group(), elements));
        }
        return matches;
      } catch (Error e) {
        System.out.println("An error occurred while searching for: " + pattern.toString());
        e.printStackTrace();
        final List<Match> emptyList = new ArrayList<Match>();
        return emptyList;
      } catch (Exception e1) {
        System.out.println("An exception occurred while seraching for: " + pattern.toString());
        e1.printStackTrace();
        final List<Match> emptyList = new ArrayList<Match>();
        return emptyList;
      }
    }