Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Sun Sep 19, 2021 2:27 am

Hi,

Can you send me a sample code where I can search for text and then replace that text with a strikeout text.

Thank you

AmisHealth
 
Posts: 8
Joined: Fri Apr 21, 2017 5:59 am

Tue Sep 21, 2021 2:48 am

Hi,

Thanks for your inquiry and sorry for the late reply as weekend.
Please refer to the code below to achieve your demand. If there is any question, please feel free to write back.
Code: Select all
//Load Pdf document
PdfDocument doc = new PdfDocument("test.pdf");
//Define a brush
PdfBrush brush = new PdfSolidBrush(Color.Black);
//Define a font with Strikeout style
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Strikeout));
String text = "TEST";
//Loop through pages
foreach (PdfPageBase page in doc.Pages)
{
    //Find text
    Spire.Pdf.General.Find.PdfTextFind[] finds = page.FindText(text, TextFindParameter.WholeWord).Finds;
    //Loop through found texts
    foreach (Spire.Pdf.General.Find.PdfTextFind seletion in finds)
    {
        List<RectangleF> textBounds = seletion.TextBounds;
        RectangleF rec = seletion.TextBounds[0];
        page.Canvas.DrawRectangle(PdfBrushes.White, rec);
        //Draw the text with Strikeout style
        page.Canvas.DrawString(text, font, brush, rec.X, rec.Y);
    }
}
//Save the document
doc.SaveToFile("result.pdf",FileFormat.PDF);

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1385
Joined: Tue Sep 27, 2016 1:06 am

Tue Sep 21, 2021 7:23 am

Thank you

AmisHealth
 
Posts: 8
Joined: Fri Apr 21, 2017 5:59 am

Tue Sep 21, 2021 8:10 am

Hi,

You are welcome.
If you have other questions, please feel free to contact us.
Wish you all the best :D

Best Regards,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1385
Joined: Tue Sep 27, 2016 1:06 am

Return to Spire.PDF