Hi Team,
Is there any way to edit or replace text in pdf ?
Best regards,
wcenjan
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);
}
}
}
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
}
}
}
}