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.

Wed Oct 02, 2024 8:23 am

Hello,

My c# code tries to replace in a PDF file the string "TEST POR MARCA RETOÑO" by "TEST POR MARCA ENERO". But after the string is replaced, the accent of the letter "Ñ" is maintained as shown in the image. How can I fix this problem?

Here adding in the dictionary the strings and calling the replace function:

Code: Select all
dictionary.Add("TEST POR MARCA RETOÑO", "TEST POR MARCA ENERO");
SpireFintTextInPDFAndReplaceItNombreProducto(doc, dictionary);


Here the replace function:

Code: Select all
public static void SpireFintTextInPDFAndReplaceItNombreProducto(PdfDocument documents, Dictionary<string, string> dictionary)
        {
            PdfTextFind[] result = null;
            foreach (var word in dictionary)
            {
                foreach (PdfPageBase page in documents.Pages)
                {

                    try
                    {
                        result = page.FindText(word.Key, TextFindParameter.WholeWord).Finds;

                        int i = 0;

                        foreach (PdfTextFind find in result)
                        {

                            if (i == 0)
                                find.ApplyRecoverString(word.Value, SystemColors.ControlLight, true);
                            else find.ApplyRecoverString(word.Value, System.Drawing.Color.White, true);
                            i++;
                        }

                    }
                    catch (Exception ex)
                    {
                        continue;
                    }


                }
            }
        }



Please, see the attached file where you can see the ressult. As you can check, the accent of "Ñ" remains.

Thanks in advance for your help.

francescbir@live.com
 
Posts: 4
Joined: Thu Mar 26, 2015 9:27 am

Wed Oct 02, 2024 9:19 am

Hello,

Thanks for your inquiry.
I created a PDF document containing 'TEST POR MARCA RETOÑO' and replaced it with 'TEST POR MARCA ENERO' using the latest Spire.PDF Pack(Hot Fix) Version:10.9.0, but I did not reproduce the issue you mentioned. For your case, I suggest you update the latest version for testing. The new version has adjusted the code for finding and replacing. Please refer to the code below for testing. If it still doesn't work, please provide us with your input file and we will investigate further. Thank you in advance.
Code: Select all
 PdfDocument doc = new PdfDocument();
 doc.LoadFromFile("test_加水印.pdf");
 PdfTextReplaceOptions textReplaceOptions = new PdfTextReplaceOptions();
 textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.WholeWord;
 textReplaceOptions.ReplaceType = PdfTextReplaceOptions.ReplaceActionType.AutofitWidth;
 for (int i = 0; i < doc.Pages.Count; i++)
 {
     PdfPageBase page = doc.Pages[i];
     PdfTextReplacer textReplacer = new PdfTextReplacer(page);
     textReplacer.Options = textReplaceOptions;
     textReplacer.ReplaceAllText("TEST POR MARCA RETOÑO", "TEST POR MARCA ENERO");
 }
 doc.SaveToFile("out.pdf");
 doc.Dispose();


Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 454
Joined: Mon Dec 27, 2021 2:23 am

Wed Oct 02, 2024 10:32 am

Hello William,

Actually I have installed Spire.PDF v. 8.4.15.1046. I have tried, as you suggest, with fix v. 10.9.0 (in my case with NET 4.6), but I got problems with my code because, I think, I'm using libraries and functions that are not avaiables with fix v. 10.9.0 such the following: "using Spire.Pdf.General.Find;", "PdfTextFind[] result = null;", "result = page.FindText(word.Key, TextFindParameter.WholeWord).Finds;", etc.

Thanks for your help,

Xavier

PD "I can't use PdfTextReplaceOptions". See attached file.

I set:

Code: Select all
using Spire.Pdf;
using Spire.Pdf.Graphics;
using PdfDocument = Spire.Pdf.PdfDocument;
using PdfImage = Spire.Pdf.Graphics.PdfImage;
using PdfFont = Spire.Pdf.Graphics.PdfFont;
using Spire.Pdf.General.Find;

francescbir@live.com
 
Posts: 4
Joined: Thu Mar 26, 2015 9:27 am

Thu Oct 03, 2024 7:44 am

Hi,

Thank you for your reply.
The PdfTextReplacer is a new method introduced in our recent versions, and in the new versions, we have updated the interface for the Find method and deprecated the previous one. For the usage of these two interfaces, please refer to the tutorial below. If you have any further questions, feel free to write back.

Find and Highlight Text in PDF: https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Text/Find-and-Highlight-Selected-Text-in-PDF-in-C-VB.NET.html
Replace Text in a PDF Document: https://www.e-iceblue.com/Tutorials/Spire.PDF/Program-Guide/Text/Find-and-replace-text-on-PDF-document-in-C.html
Sincerely,
Doris
E-iceblue support team
User avatar

Doris.Liu
 
Posts: 409
Joined: Mon Nov 07, 2022 8:10 am

Thu Oct 03, 2024 8:17 am

Thanks for your help.

francescbir@live.com
 
Posts: 4
Joined: Thu Mar 26, 2015 9:27 am

Thu Oct 10, 2024 9:42 am

Hello, after updating the DLL from Spire.PDF v. 8.4.15.1046 to v. 10.9.0, and after changing obsolete code, the changed texte is now correct, but I have lost my license. I still have a copy of the obsolete version, and the license is working fine in this case. I have attached the sample PDF with the obsolete version that is working fine with the license and the new one with the problem license. ¿What have I need to do now?

I have tried it with my Spire.PDF license:

string fileLicense = Directory.GetCurrentDirectory() + "\\license.elic.xml";
Spire.Pdf.License.LicenseProvider.SetLicenseKey(fileLicense);

But it doesn't work.

Thanks in advance,

Xavier

francescbir@live.com
 
Posts: 4
Joined: Thu Mar 26, 2015 9:27 am

Fri Oct 11, 2024 2:23 am

Hello,

Thanks for your inquiry. The argument to the SetLicenseKey method is the value of the element Key attribute of the xml license file, not the path to the license file, and you can refer to this article(https://www.e-iceblue.com/Tutorials/Lic ... nsing.html) to apply the license. Additionally, based on your email address, I have not checked your purchase history, so I cannot confirm if your license can use version 10.9.0. If your purchase date is before September 14th, 2023, then your license will not be able to apply version 10.9.0. Please note that you can receive free hot fixes and new versions within one year of purchase, if you want to update and use the version released after one year, you need to renew your license. If there is still any doubts, you can write email to our sales@e-iceblue.com.

Sincerely,
Amin
E-iceblue support team
User avatar

Amin.Gan
 
Posts: 283
Joined: Mon Jul 15, 2024 5:40 am

Return to Spire.PDF