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.

Sat May 13, 2023 1:42 pm

I'm encountering a problem with the code I'm using to replace text in a PDF file. Although the code is able to identify the text to be replaced, it fails to replace it with the new text and only places a white object over the original text.

Code: Select all
        public void ReplaceText(PdfDocument doc, string posField, string newText, string nameFile)
        {
            try
            {
                doc.LoadFromFile(formCOPTemplatePath);
                PdfBrush brush = new PdfSolidBrush(Color.Black);

                for (int i = 0; i < doc.Pages.Count; i++)
                {
                    PdfPageBase page = doc.Pages[i];
                    PdfTextFindCollection collection = page.FindText(posField, TextFindParameter.IgnoreCase);

                    if (posField == "%name%")
                    {
                        foreach (PdfTextFind find in collection.Finds)
                        {

                            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Calibri (Body)", 10f, FontStyle.Regular));
                            RectangleF rec;
                            // Gets the bound of the found text in page
                            rec = find.Bounds;
                            page.Canvas.DrawRectangle(PdfBrushes.White, rec);
                            page.Canvas.DrawString(newText, font, brush, rec);
                        }
                    }
                }
                doc.SaveToFile(formCOPDownloadPath + nameFile);
            }


I have noticed that only the line "page.Canvas.DrawRectangle(PdfBrushes.White, rec);" (which overlays the text with a white object) seems to be working, while the line "page.Canvas.DrawString(newText, font, brush, rec);" (which is intended to replace the text) does not appear to be working. This issue occurs in most of the PDF files I've tested.

I'm using Windows 11, Free Spire PDF 8.6.0, and ASP.NET Framework 4.8.

Can anyone provide suggestions or solutions to this issue, and explain why the line of code to replace the text is not working correctly? Thank you.

CANbus
 
Posts: 1
Joined: Sat May 13, 2023 1:08 pm

Mon May 15, 2023 2:35 am

Hi,

Thanks for your inquiry.
Kindly note that if the new text font size is larger than the text which will be replaced, this will cause the new text to not be displayed. Here are two solutions.
1) Adjust the font size of the new text to match the original.
2) Remove the line limit, see the following code snippet for reference(this may cause incomplete text display)
Code: Select all
PdfStringFormat format = new PdfStringFormat();
format.LineLimit = false;
…
page.Canvas.DrawString(newText, font, brush, rec, format);


best regards,
triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Return to Spire.PDF

cron