Package com.snowtide.pdf
Class SelectionOutputTarget
- java.lang.Object
-
- com.snowtide.pdf.OutputHandler
-
- com.snowtide.pdf.OutputTarget
-
- com.snowtide.pdf.SelectionOutputTarget
-
public class SelectionOutputTarget extends OutputTarget
An
OutputTarget
derivative that restricts the content added to the givenStringBuffer
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 thisOutputHandler
retainsOutputTarget
'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 withRegionOutputTarget
. 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
-
-
Field Summary
-
Fields inherited from class com.snowtide.pdf.OutputTarget
sink
-
-
Constructor Summary
Constructors Constructor Description SelectionOutputTarget(StringBuffer sb, float x1, float y1, float x2, float y2)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
endPage(Page p)
Invoked when PDFxStream has finished processing a pagevoid
startPage(Page p)
Invoked when a page is about to be processed.void
textUnit(TextUnit ch)
Default implementation that writes the character run specified by the givenTextUnit
instance to theAppendable
held by thisOutputTarget
.-
Methods inherited from class com.snowtide.pdf.OutputTarget
forBuffer, forWriter, getConfig, getObject, linebreaks, setConfig, spaces, startSpan, write, write, write, write
-
Methods inherited from class com.snowtide.pdf.OutputHandler
endBlock, endLine, endPDF, endSpan, startBlock, startLine, startPDF
-
-
-
-
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 classOutputTarget
- Parameters:
p
- a reference to thePage
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 classOutputHandler
- Parameters:
p
- a reference to thePage
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 theAppendable
held by thisOutputTarget
.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 classOutputTarget
-
-