Package fr.becpg.repo.report.pdf
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 ClassesModifier and TypeClassDescription(package private) static final recordA 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
FieldsModifier and TypeFieldDescriptionprivate PdfTextEraser.ShownStringprivate intprivate org.apache.pdfbox.text.TextPositionprivate booleanprivate List<PdfTextEraser.Region> private final List<PdfTextEraser.ShownString> private org.apache.pdfbox.pdfwriter.ContentStreamWriterFields inherited from class org.apache.pdfbox.text.PDFTextStripper
charactersByArticle, document, LINE_SEPARATOR, output -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static floatadvance(PdfTextEraser.ShownString shown, int index) Gives the displacement, in text space, the glyph at the given index added to the text matrix.private booleanappendWithoutErasedGlyphs(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 floatcomputeFontHeight(org.apache.pdfbox.pdmodel.font.PDFont arg0) private booleandrawsOnlyErasedText(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) booleanerase(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 floatflushAdvance(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 voidflushKept(ByteArrayOutputStream kept, org.apache.pdfbox.cos.COSArray target) Moves the pending kept bytes to the array as one string.private booleanisInRegion(org.apache.pdfbox.text.TextPosition position) Tells whether a glyph lies in one of the regions to clear.protected voidprocessOperator(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 voidprocessTextPosition(org.apache.pdfbox.text.TextPosition text) Keeps the position of the glyph being shown instead of collecting the page text.Gives the number of bytes of each character code of a shown string.private org.apache.pdfbox.cos.COSArrayrewriteShowText(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 voidshowGlyph(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 voidshowText(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, writeWordSeparatorMethods 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
-
Field Details
-
shownStrings
-
regions
-
writer
private org.apache.pdfbox.pdfwriter.ContentStreamWriter writer -
currentString
-
lastPosition
private org.apache.pdfbox.text.TextPosition lastPosition -
depth
private int depth -
modified
private boolean modified
-
-
Constructor Details
-
PdfTextEraser
PdfTextEraser() throws IOException- Throws:
IOException
-
-
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 pagepageIndex- the zero-based index of the pagepageRegions- 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:
processOperatorin classorg.apache.pdfbox.contentstream.PDFStreamEngine- Parameters:
operator- the operator read from the content streamoperands- 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
Records the string shown and the text matrix once it is drawn.- Overrides:
showTextin classorg.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 glyphfont- the current fontcode- the character code of the glyphdisplacement- 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:
processTextPositionin classorg.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 processedoperands- 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 showntarget- 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
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
Moves the pending kept bytes to the array as one string.- Parameters:
kept- the pending bytes, emptied by the calltarget- 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 spaceshown- the string the glyphs belong totarget- the array being built- Returns:
- the pending advance once written, always zero
-
advance
Gives the displacement, in text space, the glyph at the given index added to the text matrix.- Parameters:
shown- the string as it was shownindex- the index of the glyph in the string- Returns:
- the advance of the glyph
-
computeFontHeight
- Throws:
IOException
-