Hello,
Thank you for your feedback.
Based on your requirements, we have prepared the following demo for your reference. Please note that it is not possible to remove the original text from the PDF content directly, instead, a white rectangular area has been used to cover the target text, achieving a visual effect of making it invisible.
- Code: Select all
PdfDocument doc = new PdfDocument();
// Read a pdf file
doc.LoadFromFile(path+"1.pdf");
// Get the first page of pdf file
PdfPageBase page = doc.Pages[0];
// Create a PdfTextFinder object for searching text within the first page
PdfTextFinder finder = new PdfTextFinder(page);
finder.Options.Parameter = Spire.Pdf.Texts.TextFindParameter.Regex;
String regex = "BMI\\s*:\\s*\\d+(?:\\.\\d+)?\\s*-\\s*\\d+(?:\\.\\d+)?\\s*,\\s*adult";
// Find occurrences of the specified text within the first page
List<PdfTextFragment> finds = finder.Find(regex);
// Creates a brush
PdfBrush brush = new PdfSolidBrush(Color.DarkBlue);
// Defines a font
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 7f, FontStyle.Bold));
// Defines text horizontal/vertical center format
PdfStringFormat centerAlign = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
RectangleF rec;
// Iterate through each found text fragment
foreach (PdfTextFragment find in finds)
{
// Gets the bound of the found text in page
rec = find.Bounds[0];
page.Canvas.DrawRectangle(PdfBrushes.White, rec);
// Draws new text as defined font and color
page.Canvas.DrawString("", font, brush, rec);
}
doc.SaveToFile(path+"result.pdf");
doc.Close();
doc.Dispose();
If the above example does not meet your needs, please provide us with further feedback.
Sincerely,
Amy
E-iceblue support team