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