To command line remove existing text in a PDF you MUST attempt to replace characters with nulls or spaces, so as to keep text XY placements in their self contained lines (there is generally no wraps in a PDF).
If the number of bytes is changed the file will most likely be compromised as it depends on an index of byte offsets. In addition many texts are compressed so not easy to find as plain text.
For all the above reasons, decompression of fonts, is needed with an application that can "fix" the number of bytes after edit.
Thus qpdf with QDF mode and QDF fix is the frequently mentioned "go to" answer.
For adding text, there are other problems. Like the already included font characters may not have every sub-set of characters in its fonts. Thus the new text will need to ensure it is using its own supply of fonts. This is most easily done by writing new text on a blank page in the desired XY location and then OVERSTAMPING the original page with the new font inclusive characters.
This too is possible with qpdf AFTER the existing text is replaced with blank space characters.
If the qpdf method does not work well you would need to move up to a fully command driven editor such as PyMuPDF or Java based scripted editors like Apache PDFBox.