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.

Thu Sep 09, 2021 3:20 pm

Hello Everybody!
I'm using Spire.PDF free version with Visual Studio 2019 (C#) and I have this code:
Code: Select all
results = page.FindText("my text", TextFindParameter.WholeWord).Finds;


So, How can I replace "my text" in this code by a string variable? For example

Code: Select all
string name;
name = "new text";
results = page.FindText(name, TextFindParameter.WholeWord).Finds;


When I use this code, there is a erro message: Use of unassigned local variable 'name' (Error code: CS0165)

Help me, please!

Thank you!

hoangesc
 
Posts: 1
Joined: Thu Sep 09, 2021 3:07 pm

Fri Sep 10, 2021 3:55 am

Hello,

Thanks for your inquiry.
Please refer to the following code to replace the text in the PDF file.
Code: Select all
            String newText = "E-iceblue Spire.PDF";

            PdfBrush brush = new PdfSolidBrush(Color.DarkBlue);
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 12f, FontStyle.Regular));

            RectangleF rec;
            foreach (PdfTextFind find in results)
            {
                rec = find.TextBounds[0];
                page.Canvas.DrawRectangle(PdfBrushes.White, rec);
                page.Canvas.DrawString(newText, font, brush, rec.X,rec.Y);
            }

As for the error message, I simulated a PDF file and tested your code, but I did not reproduce your issue. Please provide your input file for further investigation. You can upload it here or send it to us ([email protected]) via email. Thanks in advance.

Sincerely,
Brian
E-iceblue support team
User avatar

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

Thu Sep 16, 2021 10:38 am

Hello,

Greetings from E-iceblue!
How is your issue now? 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