I' ve been searching the Internet for 2 Weeks and found some interesting solutions for my Problem, but nothing seems to give me the answer.
My goal is to do the folowing:
I want to find a Text in a static PDF-File and replace this text with another text. I would like to keep the design of the content. Is it really that hard?
I found a way but I lost the whole information:
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(txt_SuchenNach.Text, txt_ErsetzenMit.Text);
}
return text.ToString();
}
The second try I had was way better, but needs fields where I can change the text inside:
string fileNameExisting =path;
string fileNameNew = @"C:\TEST.pdf";
using (FileStream existingFileStream = new FileStream(fileNameExisting, FileMode.Open))
using (FileStream newFileStream = new FileStream(fileNameNew, FileMode.Create))
{
// PDF öffnen
PdfReader pdfReader = new PdfReader(existingFileStream);
PdfStamper stamper = new PdfStamper(pdfReader, newFileStream);
var form = stamper.AcroFields;
var fieldKeys = form.Fields.Keys;
foreach (string fieldKey in fieldKeys)
{
var value = pdfReader.AcroFields.GetField(fieldKey);
form.SetField(fieldKey, value.Replace(txt_SuchenNach.Text, txt_ErsetzenMit.Text));
}
// Textfeld unbearbeitbar machen (sieht aus wie normaler text)
stamper.FormFlattening = true;
stamper.Close();
pdfReader.Close();
}
This keeps the formatation of the rest of text and does only change my searched text. I need a solution for text which is NOT in a Textfield.
thanks for all your answers and your help.
The general issue is that text objects may use embedded fonts with specific glyphs assigned to specific letters. I.e. if you have a text object with some text like "abcdef" then the embedded font may contain glyphs for these ("abcdef" letters) only but not for other letters. So if you replace "abcdef" with "xyz" then the PDF will not display these "xyz" as no glyphs are available for these letters to be displayed.
So I would consider the following workflow:
I have worked on the same requirement and I am able to achieve this by the following steps.
Step1: Locating Source Pdf File and Destination file Path
Step2: Read Source Pdf file and Searching for the location of string that we want to replace
Step3: Replacing the string with new one.