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 Aug 13, 2020 7:44 am

Hi Anilkumar,

Thanks for your sharing.
I tested the below code with the latest Spire.PDF Pack(Hot Fix) Version:6.8.1 but did not encounter any issue. If you are using an older version, please try again with the latest version. If the issue still occurs, please provide your OS information (E.g. Windows 7, 64 bit) and region setting (E.g. China, Chinese), and also tell us the text that you want to replace.

Code: Select all
        static void Main(string[] args)
        {
            Spire.Pdf.PdfDocument doc = new PdfDocument(@"result.pdf");
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            dictionary.Add("CERTIFICATE", "1");
            dictionary.Add("OF", "2");
            dictionary.Add("COMPLETION", "3");
            dictionary.Add("This", "4");
            dictionary.Add("certifies", "5");
            dictionary.Add("that", "6");
            dictionary.Add("Fullname", "7");
            FindTextInPDFAndReplaceIt(doc, dictionary);
            doc.SaveToFile("out.pdf", Spire.Pdf.FileFormat.PDF);
        }
        public static void FindTextInPDFAndReplaceIt(PdfDocument documents, Dictionary<string, string> dictionary)
        {
            PdfTextFind[] result = null;
            foreach (PdfPageBase page in documents.Pages)
            {
                foreach (var word in dictionary)
                {
                    result = page.FindText(word.Key, TextFindParameter.IgnoreCase).Finds;
                    foreach (PdfTextFind find in result)
                    {
                        //replace word in pdf
                        find.ApplyRecoverString(word.Value, System.Drawing.Color.White, true);

                    }
                }
            }
        }


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Thu Aug 13, 2020 12:55 pm

Hi Rachel,

I am using FreeSpire.PDF 6.2 Version. Doesn't it have that hotfix.Kindly help with this.


Thanks,
Anilkumar

anil.menta
 
Posts: 8
Joined: Tue Aug 11, 2020 9:34 am

Fri Aug 14, 2020 1:33 am

Hi Anilkumar,

Thanks for your response.
I tested the same code with Free Spire.PDF for .NET Version:6.2, but did not encounter any issue either. If your code is different from mine, please share it with us for further investigation.
Besides, kindly note that we only upgrade our free version irregularly. I suggest you use our commercial version as it is more stable and contains more bug fixes. We are willing to provide a temporary license (one month free) to help you evaluate our commercial version better and remove the warning message. If interested, please contact our sales team (sales@e-iceblue.com) to get it.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Fri Aug 14, 2020 3:04 am

Hi Rachel,

I am getting the same error even though i am using 6.2 Version. Can you please help with this?



Thanks,
Anilkumar

anil.menta
 
Posts: 8
Joined: Tue Aug 11, 2020 9:34 am

Fri Aug 14, 2020 6:34 am

Hi Anilkumar,

I still did not reproduce your issue. Please test your case with the latest commercial version Spire.PDF Pack(Hot Fix) Version:6.8.1. If the issue still occurs after trying, please provide your OS information (E.g. Windows 7, 64 bit) and region setting (E.g. China, Chinese) for further investigation. Thanks in advance.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Fri Aug 14, 2020 10:12 am

Hi Rachel,

How can we adjust the alignment of a replaced text?


Thanks,
Anilkumar

anil.menta
 
Posts: 8
Joined: Tue Aug 11, 2020 9:34 am

Mon Aug 17, 2020 2:13 am

Hi Anilkumar,

Thanks for your reply.
Sorry to tell you that the "ApplyRecoverString" method doesn't support setting the alignment of the text. However, there is an alternative solution. You can first draw a rectangle to cover the original text, and then draw new text on it and set its alignment, as shown below.
Code: Select all
        public static void FindTextInPDFAndReplaceIt(PdfDocument documents, Dictionary<string, string> dictionary)
        {
            PdfTextFind[] result = null;
            foreach (PdfPageBase page in documents.Pages)
            {
                foreach (var word in dictionary)
                {
                    result = page.FindText(word.Key, TextFindParameter.IgnoreCase).Finds;
                    foreach (PdfTextFind find in result)
                    {
                        PdfBrush brush = new PdfSolidBrush(Color.White);
                        RectangleF rect = find.Bounds;
       
                        //Draw a rectangle to cover the original text
                        page.Canvas.DrawRectangle(brush, rect);
                        PdfTrueTypeFont font = new PdfTrueTypeFont(new Font(find.FontName, 20f));
                        PdfBrush brush2 = PdfBrushes.Black;
                        PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);
                        //Draw new text
                        page.Canvas.DrawString(word.Value, font, brush2, find.Bounds, format);

                    }
                }
            }
        }


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Mon Aug 17, 2020 6:39 pm

Hi Rachel,

Thank you for the response. The one you have provided is not working Rachel. It is just replacing with white brush. Text is not showing.
Can you please help with this?



Thanks,
Anilkumar

anil.menta
 
Posts: 8
Joined: Tue Aug 11, 2020 9:34 am

Tue Aug 18, 2020 10:36 am

Hi Anilkumar,

Thanks for your feedback.
This may be related to the font size you set, you can set it smaller as shown below. In addition, our Spire.PDF doesn't support getting the font size of the found text yet. We have added it as a new feature with the ticket SPIREPDF-3010. If this can be achieved in the future, I will let you know.
Code: Select all
     PdfTrueTypeFont font = new PdfTrueTypeFont(new Font(find.FontName, 12f));


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Tue Aug 18, 2020 3:44 pm

Hi Rachel,


The whole text is not showing even though i reduced font size. Can we increase the width of drawing text?
Can you please help me with proper solution?


Thanks,
Anilkumar

anil.menta
 
Posts: 8
Joined: Tue Aug 11, 2020 9:34 am

Wed Aug 19, 2020 1:53 am

Hi Anilkumar,

Thanks for your feedback.
Below is the code snippet for your reference. Feel free to contact us if you have further questions.
Code: Select all
        PdfBrush brush = new PdfSolidBrush(Color.White);
        RectangleF rect = find.Bounds;
        //Draw a rectangle to cover the original text
        page.Canvas.DrawRectangle(brush, rect);                       
        PdfTrueTypeFont font = new PdfTrueTypeFont(new Font(find.FontName, 12f));
        PdfBrush brush2 = PdfBrushes.Red;
        SizeF size = font.MeasureString(word.Value);
        float width = size.Width > rect.Width? size.Width : rect.Width;
        RectangleF textRect = new RectangleF(rect.X, rect.Y, width, rect.Height);
        PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);
        //Draw new text
        page.Canvas.DrawString(word.Value, font, brush2, textRect, format);


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Wed Aug 19, 2020 2:42 am

afzal_ali27 wrote:Hi again sir!

Is there any update? and also i have one more issue i.e. when the program replaced the text in pdf and create updated pdf file there is issue that the replaced text is not able to copy from pdf for example if we copy text from pdf and paste it into an editor it will show paste the original text instead of replaced, please help me regard this. (this issue is more important for me). Thanks

Regards
Afzal Ali


Hi Afzal,

Regarding the issue that the application threw ArgumentException when using the method ApplyRecoverString, it is fixed in the newly released Spire.PDF Pack(Hot Fix) Version:6.8.5, welcome to download it from the following links.
Our website: https://www.e-iceblue.com/Download/down ... t-now.html
Nuget: https://www.nuget.org/packages/Spire.PDF/6.8.5

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Thu Sep 10, 2020 9:03 am

Thanks Rachel sir! :)

afzal_ali27
 
Posts: 6
Joined: Sun Jul 26, 2020 3:55 am

Thu Sep 10, 2020 10:02 am

afzal_ali27 wrote:Thanks Rachel sir! :)


Hi Afzal,

You are welcome.
Feel free to contact us if you need further assistance.
Have a great day!

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Mon Nov 18, 2024 2:05 am

Hello anil.menta,

Thank you for your patience.
I'm glad to inform you that our latest Spire.PDF has supported retrieving font formatting of the found text. It supports retrieving bold, faux bold (font style set to fill and stroke), italic, and color. Welcome to download and test.

Website link:https://www.e-iceblue.com/Download/download-pdf-for-net-now.html
NuGet link: https://www.nuget.org/packages/Spire.PDF

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1271
Joined: Wed Apr 25, 2018 3:20 am

Return to Spire.PDF