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 Mar 14, 2019 2:32 am

Hi Team,

Is there any way to edit or replace text in pdf ?

Best regards,
wcenjan

404room
 
Posts: 1
Joined: Wed Feb 20, 2019 11:10 am

Thu Mar 14, 2019 7:41 am

Hello,

Thanks for your post.
Sorry that there is no direct way to edit or replace text in PDF. However, there is a workaround, that is covering the text and then drawing the new string like the following sample code. Please have a try. If there is any question, welcome to get it back to us.
Code: Select all
   static void Main(string[] args)
   {
       Spire.Pdf.PdfDocument doc = new PdfDocument(@"test.pdf");
       Dictionary<string, string> dictionary = new Dictionary<string, string>();
       dictionary.Add("searchedText","replacedText");
       FindTextInPDFAndReplaceIt(doc, dictionary);
       doc.SaveToFile("result.pdf", Spire.Pdf.FileFormat.PDF);
   }
   public static void FindTextInPDFAndReplaceIt(PdfDocument documents, Dictionary<string, string> dictionary)
           {
              PdfTextFind[] result = null;
              foreach (var word in dictionary)
              {
                 foreach (PdfPageBase page in documents.Pages)
                 {
                   result = page.FindText(word.Key,false,true).Finds;
                  foreach (PdfTextFind find in result)
                    {
                  //replace word in pdf                   
                   find.ApplyRecoverString(word.Value, System.Drawing.Color.Red, true);
                    }
                 }
              }

Sincerely,
Lisa
E-iceblue support team
User avatar

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

Sun Jul 26, 2020 4:06 am

Hi this was very helpful but there is small issue that the font style and font size of replaced text is not matched with the text that was to find, can you help me regarding this?
Thanks
Regards
Afzal Ali

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

Mon Jul 27, 2020 5:46 am

Hi Afzal,

Thanks for your inquiry.
I tested the code above with the latest Spire.PDF Pack(Hot Fix) Version:6.7.8 but found that the font style and font size of replaced text are consistent with the text that was to find. If you using an older version, please download the latest version and try again.
If the issue still occurs after trying, please provide your input file and tell us the text that you want to replace for further investigation. You could send them to us(support@e-iceblue.com) via email. Thanks in advance.

Sincerely,
Rachel
E-iceblue support team
User avatar

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

Wed Jul 29, 2020 2:16 am

Hi Rachel sir,

16.zip


Thanks for your quick reply i have installed the latest version via NuGet package manager in visual studio but still same issue i am attaching the pdf file 16.pdf please have a look i want to replace the text 'MASTER OF THE WATERS' with 'MASTER OF THE FIRES', and also there is another issue (i want to replace word 'happy' with 'sad' so there are two occurrence of that word but the code is just replacing the first one not second one and same font issue ) you can check with the same file. Please give me some solution, looking forward for your reply..

Thanks in advance

Regards
Afzal Ali

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

Wed Jul 29, 2020 11:10 am

Hi Afzal,

Thanks for your inquiry.
Regarding your first issue, please make sure you have installed the corresponding fonts in your computer. If the issue still happens, please provide your output file, you OS information (E.g. Windows 7, 64 bit) and region setting (E.g. China, Chinese).
As for your second issue, I tested the following code but got the error "Parameter is not valid". This issue will be passed to our Dev team for further investigation. If there is any update, we will let you know.
Code: Select all
           static void Main(string[] args)
           {
               Spire.Pdf.PdfDocument doc = new PdfDocument(@"16.pdf");
               Dictionary<string, string> dictionary = new Dictionary<string, string>();
               dictionary.Add("happy", "sad");
               FindTextInPDFAndReplaceIt(doc, dictionary);
               doc.SaveToFile("result.pdf", Spire.Pdf.FileFormat.PDF);
           }
           public static void FindTextInPDFAndReplaceIt(PdfDocument documents, Dictionary<string, string> dictionary)
           {
               PdfTextFind[] result = null;
               foreach (var word in dictionary)
               {
                   foreach (PdfPageBase page in documents.Pages)
                   {
                       //!!!This method is obsolete and may be removed in the future
                       //result = page.FindText(word.Key, false, false).Finds;
                       //New method
                       result = page.FindText(word.Key, TextFindParameter.None).Finds;

                       foreach (PdfTextFind find in result)
                       {
                           //replace word in pdf                   
                           find.ApplyRecoverString(word.Value, System.Drawing.Color.Red, true);//Parameter is not valid
                       }
                   }
               }
           }


Sincerely,
Rachel
E-iceblue support team
User avatar

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

Wed Jul 29, 2020 3:51 pm

Hi richel sir,

Thanks for having look, I am waiting for update from your side regarding second issue.

Regards
Afzal Ali

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

Thu Jul 30, 2020 2:32 am

Hi Afzal,

Thanks for your response.
Once there is any progress, I will notify you immediately. Have a nice day!

Sincerely,
Rachel
E-iceblue support team
User avatar

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

Sun Aug 09, 2020 3:38 am

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

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

Mon Aug 10, 2020 3:37 am

Hi Afzal,

Thanks for your following up.
I just checked the status of your issue and found that it has been fixed. Now it is under the testing phase. If it passes the test, we will prepare a hotfix for you.
As for your another issue, the implementation logic of the ApplyRecoverString method is to draw a rectangle to cover the original text, and then draw new text on it. The original text actually still exists on the PDF page, so when you copy the text and paste it, it shows the original text. Regarding this issue, we are very sorry that there is currently no solution available.

Sincerely,
Rachel
E-iceblue support team
User avatar

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

Mon Aug 10, 2020 7:51 am

Hi sir,

Thanks for your help. Is there any other way to replace text in this API.

Regards
Afzal Ali

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

Mon Aug 10, 2020 8:58 am

Hi Afzal,

Thanks for your reply.
Unfortunately there is no any other way to replace text at present. Sorry for the inconvenience caused.

Sincerely,
Rachel
E-iceblue support team
User avatar

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

Wed Aug 12, 2020 1:32 pm

Hi Rachel,

I am also facing the error Parameter is not valid while implementing replace text. Is there any update on this.


Thanks
Anilkumar

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

Thu Aug 13, 2020 3:18 am

Hi Anilkumar,

Thanks for your post.
Considering that the internal structure of each document is different, the hotfix for the issue above may not apply to your document. Please provide your test code and your input file, then we will verify it further. You could attach them here or send them to us (support@e-iceblue.com) via email. Thanks in advance.

Sincerely,
Rachel
E-iceblue support team
User avatar

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

Thu Aug 13, 2020 5:07 am

Hi Rachel,

This is my code.

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); // getting error here for the second replacement

}
}
}

I have the attached the sample pdf file. Can you please help with this issue?

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

Return to Spire.PDF