Table of Contents

Method EditText

Namespace
BitMiracle.Docotic.Pdf
Assembly
BitMiracle.Docotic.Pdf.dll

EditText(Regex, PdfTextMatchHandler)

Edits text that matches the specified regular expression in the PDF document.

public void EditText(Regex regex, PdfTextMatchHandler matchHandler)

Parameters

regex Regex

The regular expression used to find text to edit.

matchHandler PdfTextMatchHandler

The callback invoked for each match of the specified regular expression in the PDF document. The callback receives information about the match and specifies how the match should be processed.

Remarks

Editing a text fragment does not affect the positions of the remaining text.

If the regular expression contains no capturing groups, the entire match is returned for editing. If the regular expression contains one or more capturing groups, only the text captured by those groups is returned for editing. If a capturing group occurs multiple times during a match, all of its captured occurrences are returned.

Use non-capturing groups (?:...) or the ExplicitCapture flag to prevent groups from being captured.

The text search follows the order of the text elements in the PDF content stream. This order may differ from the logical reading order of the document.

Neighboring text elements can be combined with or without whitespace when searching depending on their positions.

Text elements separated by a significant horizontal or vertical distance are combined with a line feed (\n). For example, "a" on one line followed by "b" on the next line is searched as "a\nb".

Text on different pages is searched independently. For example, if one page ends with "hello" and the next page starts with "world", searching for "hello world" does not produce a match across the page boundary.

The matchHandler callback specifies the action for each match and controls how the match is processed. Return:

  • new PdfTextEditResult() to leave the current match unchanged and continue processing.
  • new PdfTextEditResult().Remove(..) to remove spans from the current match and continue processing.
  • new PdfTextEditResult().Replace(..) to replace spans from the current match and continue processing.
  • new PdfTextEditResult().Stop() to leave the current match unchanged and stop processing.
  • new PdfTextEditResult().Remove(..).Stop() to remove the current match and stop processing.
  • new PdfTextEditResult().Replace(..).Stop() to replace the current match and stop processing.

Do not modify the PDF document from within the matchHandler callback, for example, by drawing on page canvases. Doing so may lead to unpredictable results.

All invocations of matchHandler complete before any text is replaced. If the callback throws an exception, no text is replaced by this method.

Exceptions

ArgumentNullException

regex or matchHandler is null.

CannotShowTextException

The replacement text cannot be displayed.

PdfException

matchHandler returns the result that uses text span from another PdfDocument.

InvalidOperationException

matchHandler returns null.