Class PdfTextEraser

java.lang.Object
org.apache.pdfbox.contentstream.PDFStreamEngine
org.apache.pdfbox.text.PDFTextStripper
fr.becpg.repo.report.pdf.PdfTextEraser

final class PdfTextEraser extends org.apache.pdfbox.text.PDFTextStripper
Removes the glyphs drawn inside given regions from the content of a page, so that the text is gone from the page itself and not only hidden under a mask: it no longer comes out in a search or a copy. The glyphs removed are replaced by an equal advance, which leaves the text that follows them in the same operator where it was. A form XObject is removed from the page when all its text lies in the regions, and kept whole otherwise; text drawn by the quote operators is left untouched. Not thread-safe: use one instance per document.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    (package private) static final record 
    A text line segment, in the coordinates of the text stripper: horizontal bounds and the baseline distance from the top of the page.
    private static final class 
     
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
     
    private int
     
    private org.apache.pdfbox.text.TextPosition
     
    private boolean
     
     
     
    private org.apache.pdfbox.pdfwriter.ContentStreamWriter
     

    Fields inherited from class org.apache.pdfbox.text.PDFTextStripper

    charactersByArticle, document, LINE_SEPARATOR, output
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    private static float
    Gives the displacement, in text space, the glyph at the given index added to the text matrix.
    private boolean
    appendWithoutErasedGlyphs(PdfTextEraser.ShownString shown, org.apache.pdfbox.cos.COSArray target)
    Appends a shown string to a show-text array, each run of erased glyphs being replaced by the advance it took.
    protected float
    computeFontHeight(org.apache.pdfbox.pdmodel.font.PDFont arg0)
     
    private boolean
    drawsOnlyErasedText(org.apache.pdfbox.contentstream.operator.Operator operator)
    Tells whether an operator draws a form XObject whose whole text lies in the regions, such as the page total a report engine keeps in a template of its own.
    (package private) boolean
    erase(org.apache.pdfbox.pdmodel.PDDocument document, int pageIndex, List<PdfTextEraser.Region> pageRegions)
    Rewrites the content of a page without the glyphs lying in the regions.
    private static float
    flushAdvance(float erasedAdvance, PdfTextEraser.ShownString shown, org.apache.pdfbox.cos.COSArray target)
    Writes the pending advance of erased glyphs to the array as a text adjustment.
    private static void
    flushKept(ByteArrayOutputStream kept, org.apache.pdfbox.cos.COSArray target)
    Moves the pending kept bytes to the array as one string.
    private boolean
    isInRegion(org.apache.pdfbox.text.TextPosition position)
    Tells whether a glyph lies in one of the regions to clear.
    protected void
    processOperator(org.apache.pdfbox.contentstream.operator.Operator operator, List<org.apache.pdfbox.cos.COSBase> operands)
    Copies each operator of the page to the rewritten content, a show-text operator being replaced when it draws glyphs to erase.
    protected void
    processTextPosition(org.apache.pdfbox.text.TextPosition text)
    Keeps the position of the glyph being shown instead of collecting the page text.
    private static List<Integer>
    Gives the number of bytes of each character code of a shown string.
    private org.apache.pdfbox.cos.COSArray
    rewriteShowText(org.apache.pdfbox.contentstream.operator.Operator operator, List<org.apache.pdfbox.cos.COSBase> operands)
    Builds the operand of the show-text operator that replaces the given one, or returns null when the operator draws nothing to erase and must be written as it is.
    protected void
    showGlyph(org.apache.pdfbox.util.Matrix textRenderingMatrix, org.apache.pdfbox.pdmodel.font.PDFont font, int code, org.apache.pdfbox.util.Vector displacement)
    Records where each glyph of the current string starts and whether it lies in a region.
    protected void
    showText(byte[] string)
    Records the string shown and the text matrix once it is drawn.

    Methods inherited from class org.apache.pdfbox.text.PDFTextStripper

    beginMarkedContentSequence, endArticle, endDocument, endMarkedContentSequence, endPage, getAddMoreFormatting, getArticleEnd, getArticleStart, getAverageCharTolerance, getCharactersByArticle, getCurrentPageNo, getDropThreshold, getEndBookmark, getEndPage, getIgnoreContentStreamSpaceGlyphs, getIndentThreshold, getLineSeparator, getListItemPatterns, getOutput, getPageEnd, getPageStart, getParagraphEnd, getParagraphStart, getSeparateByBeads, getSortByPosition, getSpacingTolerance, getStartBookmark, getStartPage, getSuppressDuplicateOverlappingText, getText, getWordSeparator, matchPattern, processPage, processPages, setAddMoreFormatting, setArticleEnd, setArticleStart, setAverageCharTolerance, setDropThreshold, setEndBookmark, setEndPage, setIgnoreContentStreamSpaceGlyphs, setIndentThreshold, setLineSeparator, setListItemPatterns, setPageEnd, setPageStart, setParagraphEnd, setParagraphStart, setShouldSeparateByBeads, setSortByPosition, setSpacingTolerance, setStartBookmark, setStartPage, setSuppressDuplicateOverlappingText, setWordSeparator, startArticle, startArticle, startDocument, startPage, writeCharacters, writeLineSeparator, writePage, writePageEnd, writePageStart, writeParagraphEnd, writeParagraphSeparator, writeParagraphStart, writeString, writeString, writeText, writeWordSeparator

    Methods inherited from class org.apache.pdfbox.contentstream.PDFStreamEngine

    addOperator, applyTextAdjustment, beginText, decreaseLevel, endText, getAppearance, getCurrentPage, getGraphicsStackSize, getGraphicsState, getInitialMatrix, getLevel, getResources, getTextLineMatrix, getTextMatrix, increaseLevel, isShouldProcessColorOperators, markedContentPoint, operatorException, processAnnotation, processChildStream, processOperator, processSoftMask, processTilingPattern, processTilingPattern, processTransparencyGroup, processType3Stream, restoreGraphicsStack, restoreGraphicsState, saveGraphicsStack, saveGraphicsState, setLineDashPattern, setTextLineMatrix, setTextMatrix, showAnnotation, showFontGlyph, showForm, showTextString, showTextStrings, showTransparencyGroup, showType3Glyph, transformedPoint, transformWidth, unsupportedOperator

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • shownStrings

      private final List<PdfTextEraser.ShownString> shownStrings
    • regions

      private List<PdfTextEraser.Region> regions
    • writer

      private org.apache.pdfbox.pdfwriter.ContentStreamWriter writer
    • currentString

      private PdfTextEraser.ShownString currentString
    • lastPosition

      private org.apache.pdfbox.text.TextPosition lastPosition
    • depth

      private int depth
    • modified

      private boolean modified
  • Constructor Details

  • Method Details

    • erase

      boolean erase(org.apache.pdfbox.pdmodel.PDDocument document, int pageIndex, List<PdfTextEraser.Region> pageRegions) throws IOException
      Rewrites the content of a page without the glyphs lying in the regions.
      Parameters:
      document - the document holding the page
      pageIndex - the zero-based index of the page
      pageRegions - the regions to clear on that page
      Returns:
      true if the page content was changed
      Throws:
      IOException - if the page content cannot be read or written
    • processOperator

      protected void processOperator(org.apache.pdfbox.contentstream.operator.Operator operator, List<org.apache.pdfbox.cos.COSBase> operands) throws IOException
      Copies each operator of the page to the rewritten content, a show-text operator being replaced when it draws glyphs to erase. Operators run on behalf of another one, such as those of a form XObject, are processed but not copied.
      Overrides:
      processOperator in class org.apache.pdfbox.contentstream.PDFStreamEngine
      Parameters:
      operator - the operator read from the content stream
      operands - its operands
      Throws:
      IOException - if the operator cannot be processed or written
    • drawsOnlyErasedText

      private boolean drawsOnlyErasedText(org.apache.pdfbox.contentstream.operator.Operator operator)
      Tells whether an operator draws a form XObject whose whole text lies in the regions, such as the page total a report engine keeps in a template of its own. Such a form is left out of the page; a form holding other text, or no text at all like an image, is kept.
      Parameters:
      operator - the operator just processed
      Returns:
      true if the operator must not be copied to the page
    • showText

      protected void showText(byte[] string) throws IOException
      Records the string shown and the text matrix once it is drawn.
      Overrides:
      showText in class org.apache.pdfbox.contentstream.PDFStreamEngine
      Parameters:
      string - the encoded string
      Throws:
      IOException - if the string cannot be shown
    • showGlyph

      protected void showGlyph(org.apache.pdfbox.util.Matrix textRenderingMatrix, org.apache.pdfbox.pdmodel.font.PDFont font, int code, org.apache.pdfbox.util.Vector displacement) throws IOException
      Records where each glyph of the current string starts and whether it lies in a region.
      Parameters:
      textRenderingMatrix - the text rendering matrix of the glyph
      font - the current font
      code - the character code of the glyph
      displacement - the displacement of the glyph
      Throws:
      IOException - if the glyph cannot be shown
    • processTextPosition

      protected void processTextPosition(org.apache.pdfbox.text.TextPosition text)
      Keeps the position of the glyph being shown instead of collecting the page text.
      Overrides:
      processTextPosition in class org.apache.pdfbox.text.PDFTextStripper
      Parameters:
      text - the position of the glyph
    • isInRegion

      private boolean isInRegion(org.apache.pdfbox.text.TextPosition position)
      Tells whether a glyph lies in one of the regions to clear.
      Parameters:
      position - the position of the glyph
      Returns:
      true if the glyph must be erased
    • rewriteShowText

      private org.apache.pdfbox.cos.COSArray rewriteShowText(org.apache.pdfbox.contentstream.operator.Operator operator, List<org.apache.pdfbox.cos.COSBase> operands) throws IOException
      Builds the operand of the show-text operator that replaces the given one, or returns null when the operator draws nothing to erase and must be written as it is.
      Parameters:
      operator - the operator just processed
      operands - its operands
      Returns:
      the array to show with the adjusted show-text operator, or null to keep the operator
      Throws:
      IOException - if a string cannot be split into character codes
    • appendWithoutErasedGlyphs

      private boolean appendWithoutErasedGlyphs(PdfTextEraser.ShownString shown, org.apache.pdfbox.cos.COSArray target) throws IOException
      Appends a shown string to a show-text array, each run of erased glyphs being replaced by the advance it took.
      Parameters:
      shown - the string as it was shown
      target - the array receiving the kept runs and the advances
      Returns:
      false when the string cannot be split safely
      Throws:
      IOException - if the string cannot be split into character codes
    • readCodeLengths

      private static List<Integer> readCodeLengths(PdfTextEraser.ShownString shown) throws IOException
      Gives the number of bytes of each character code of a shown string.
      Parameters:
      shown - the string as it was shown
      Returns:
      the byte length of each code, in order
      Throws:
      IOException - if a code cannot be read
    • flushKept

      private static void flushKept(ByteArrayOutputStream kept, org.apache.pdfbox.cos.COSArray target)
      Moves the pending kept bytes to the array as one string.
      Parameters:
      kept - the pending bytes, emptied by the call
      target - the array being built
    • flushAdvance

      private static float flushAdvance(float erasedAdvance, PdfTextEraser.ShownString shown, org.apache.pdfbox.cos.COSArray target)
      Writes the pending advance of erased glyphs to the array as a text adjustment.
      Parameters:
      erasedAdvance - the pending advance, in text space
      shown - the string the glyphs belong to
      target - the array being built
      Returns:
      the pending advance once written, always zero
    • advance

      private static float advance(PdfTextEraser.ShownString shown, int index)
      Gives the displacement, in text space, the glyph at the given index added to the text matrix.
      Parameters:
      shown - the string as it was shown
      index - the index of the glyph in the string
      Returns:
      the advance of the glyph
    • computeFontHeight

      protected float computeFontHeight(org.apache.pdfbox.pdmodel.font.PDFont arg0) throws IOException
      Throws:
      IOException