Find and Replace specific word in pdf.
Hi all,
I have written a C# console program to replace a specific word with new word in pdf.My program doing this but my new pdf does does not look like orginal pdf. My basic purpose to replace some words without changing the look and feel of the orginal pdf.Can u pls help me out regard this, your help is heartily appreciated.Here is my code:
static void Main(string[] args)
{
var editedText = ExtractTextFromPdf("@"C:\temp\MyPdf_Orginal.pdf"");
using (var fileStream = new FileStream(@"C:\temp\MyPdf_New.pdf", FileMode.Create, FileAccess.Write)
{
Document document = new Document(PageSize.A4, 25, 25, 30, 30);
PdfWriter writer = PdfWriter.GetInstance(document, fileStream);
document.Open();
document.Open();
document.Add(new Paragraph(editedText));
Close the document
document.Close();
writer.Close();
fileStream .Close();
}
}
public static string ExtractTextFromPdf(string path)
{
using (PdfReader reader = new PdfReader(path))
{
StringBuilder text = new StringBuilder();
for (int i = 1; i <= reader.NumberOfPages; i++)
{
text.Append(PdfTextExtractor.GetTextFromPage(reader, i));
text.Replace("Delhi", "Mumbai");
}
return text.ToString();
}
}
}
Any new approach is most welcome.