Class SelectionOutputTarget


  • public class SelectionOutputTarget
    extends OutputTarget

    An OutputTarget derivative that restricts the content added to the given StringBuffer to that within the starting and ending selection points specified in the constructor. This implementation would most commonly be paired with an interactive UI, where a user would be able to specify a range of content to be selected.

    Example:

     float x1 = 0, y1 = 450, x2 = 390, y2 = 72;
     
     Document pdf = PDF.open(pdfFile);
     StringBuffer sb = new StringBuffer();
     SelectionOutputTarget tgt = new SelectionOutputTarget(sb, x1, y1, x2, y2);
     
     pdf.getPage(0).pipe(tgt);
     p.pipe(tgt);
     pdf.close();
     
     String selectedText = sb.toString();
     
    Note that this OutputHandler retains OutputTarget's read-ordering and segmentation semantics, so the selection's start and end points are interpreted within that context; they are not the corners of a bounding or crop box as with RegionOutputTarget. That is, a start point that is within the first column of a page and an end point in the second column will result in all of the intervening text being extracted.

    Version:
    ©2004-2024 Snowtide
    • Constructor Detail

      • SelectionOutputTarget

        public SelectionOutputTarget​(StringBuffer sb,
                                     float x1,
                                     float y1,
                                     float x2,
                                     float y2)
    • Method Detail

      • startPage

        public void startPage​(Page p)
        Description copied from class: OutputHandler
        Invoked when a page is about to be processed.
        Overrides:
        startPage in class OutputTarget
        Parameters:
        p - a reference to the Page that is about to be processed
      • endPage

        public void endPage​(Page p)
        Description copied from class: OutputHandler
        Invoked when PDFxStream has finished processing a page
        Overrides:
        endPage in class OutputHandler
        Parameters:
        p - a reference to the Page that has been processed
      • textUnit

        public void textUnit​(TextUnit ch)
        Description copied from class: OutputTarget

        Default implementation that writes the character run specified by the given TextUnit instance to the Appendable held by this OutputTarget.

        This implementation is very straightforward; it is provided here for illustrative purposes only:

         if (tu.getCharacterSequence() == null) {
             // no mapped sequence, append direct character code conversion
             int cc = tu.getCharCode();
             if (cc >= 32) write((char)cc);
         } else {
             write(tu.getCharacterSequence());
         }
         

        Overrides:
        textUnit in class OutputTarget