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.

Tue Aug 24, 2021 7:10 am

I want to find a text and change the fore color of that text.

I have been trying the below code but it doesnot change.


Spire.Pdf.General.Find.PdfTextFind[] text2 = page2.FindText("<PLACE1>", TextFindParameter.WholeWord).Finds;

PdfBrush brush = new PdfSolidBrush(Color.DarkBlue);

RectangleF rec;

PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 12f, FontStyle.Regular));

foreach (Spire.Pdf.General.Find.PdfTextFind seletion in text2)
{

rec = seletion.Bounds;

page2.Canvas.DrawRectangle(PdfBrushes.White, rec);

page2.Canvas.DrawString("<PLACE1>", font, brush, rec);
}

I have tried to apply highlight and that also doesnot work.
Please help.

Thank you

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

Tue Aug 24, 2021 9:53 am

Hello,

Thanks for your inquiry.
I simulated a pdf file and tested your code with the latest Spire.PDF Pack(Hot Fix) Version:7.8.8, but I did not reproduce your issue. If you are using an older version, we recommend you upgrade to the latest version and try again.
If the issue persists, please provide your input file for further investigation. Thanks in advance for your assistance.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Tue Aug 24, 2021 10:39 am

Hi,

What I am trying to achieve is I have a pdf file with <TEST> which is set to fore color white.

I want to set <TEST> to fore color black and replace the text to todays date. But it always returns a empty pdf.

The test pdf file attached.

Code below and file attached.

Spire.Pdf.General.Find.PdfTextFind[] text2 = page1.FindText("<TEST>", TextFindParameter.WholeWord).Finds;

PdfBrush brush = new PdfSolidBrush(Color.Black);

RectangleF rec;

PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Regular));
foreach (Spire.Pdf.General.Find.PdfTextFind seletion in text2)
{

rec = seletion.TextBounds[0];

page1.Canvas.DrawRectangle(PdfBrushes.Black, rec);

page1.Canvas.DrawString("<TEST>", font, brush, rec);
}

ptr1.ReplaceText("<TEST>", "08/24/2021");

doc.SaveToFile(@"C:\Test1.pdf");

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

Wed Aug 25, 2021 9:35 am

Hello,

Thanks for your response.
Please refer to the following modified code.
Code: Select all
            PdfDocument doc = new PdfDocument("test.pdf");
            PdfPageBase page1 = doc.Pages[0];

            Spire.Pdf.General.Find.PdfTextFind[] text2 = page1.FindText("<TEST>", TextFindParameter.WholeWord).Finds;

            PdfBrush brush = new PdfSolidBrush(Color.Black);

            RectangleF rec;

            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Regular));
            foreach (Spire.Pdf.General.Find.PdfTextFind seletion in text2)
            {
                List<RectangleF> textBounds = seletion.TextBounds;

                rec = seletion.TextBounds[0];
                page1.Canvas.DrawRectangle(PdfBrushes.White, rec);

                page1.Canvas.DrawString("08/24/2021", font, brush, rec.X, rec.Y);
            }

            doc.SaveToFile(@"Test1.pdf");


Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Wed Sep 01, 2021 10:22 am

Hello,

Greetings from E-iceblue!
Did the code we provided work for you? Could you please give us some feedback at your convenience?

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Return to Spire.PDF

cron