Hi, I use the latest version 21.9.0 and I need to replace text phrases in a PDF document. To accomplish this task I have used sample code from aspose.com:
My code sample below searches for the phrase “FrauHerrn” and replaces this phrase with “This works as it should!”. Actually, this works as it should.
Subsequently my code sample searches for the phrase ““VornameNachname”” and is supposed to replace this phrase with “Uwe”. However, in the output-PDF “VornameNachname” is replaced with “we” and the “U” is missing.
If I change the code from “Uwe” to “Owe” the phrase “VornameNachname” is replaced with “Owe” - as it should.
The attached link allows the download of the VS Windows Forms sample project, the PDF-file required to replicate the issue and the corresponding word document.
https://drive.google.com/drive/folders/19b0ub6t2rN6G2VxqwzeUjBkqSYeWO7Bg?usp=sharing
Many thanks in advance for any help.
Stefan
Document pdfDocument = new Document("c:\\BKAbrNachzahlung.pdf");
TextFragmentAbsorber textFragmentAbsorber1 = new TextFragmentAbsorber("FrauHerrn");
pdfDocument.Pages.Accept(textFragmentAbsorber1);
TextFragmentCollection textFragmentCollection1 = textFragmentAbsorber1.TextFragments;
foreach (TextFragment textFragment in textFragmentCollection1)
{
textFragment.Text = "This works as it should!";
textFragment.TextState.Font = FontRepository.FindFont("Arial");
textFragment.TextState.FontSize = 11;
textFragment.TextState.FontStyle = FontStyles.Regular;
}
TextFragmentAbsorber textFragmentAbsorber2 = new TextFragmentAbsorber("VornameNachname");
pdfDocument.Pages.Accept(textFragmentAbsorber2);
TextFragmentCollection textFragmentCollection2 = textFragmentAbsorber2.TextFragments;
foreach (TextFragment textFragment in textFragmentCollection2)
{
textFragment.Text = "Uwe";
textFragment.TextState.Font = FontRepository.FindFont("Arial");
textFragment.TextState.FontSize = 11;
textFragment.TextState.FontStyle = FontStyles.Regular;
}
pdfDocument.Save("c:\\BKAbrNachzahlung_out.pdf");