Hi all,
In .Net, after using TextFragmentAbsorber to replace text in a PDF file, the page Content was not adjusted.
Please look at attached pdf: beforeReplace.pdf (126.0 KB)
All my Texts are within a table and what I want to do is to replace this text with another text, that I’m getting from another Source (SharePoint Rest Statment to get metadata from a SharePoint List Item).
So: “<%Field.Title%>” should be replaced to for example: “This is my Title”.
Well, that’s not really the Problem as you can see (Please look at attached file afterReplace.pdf (207.1 KB)
) that my Code is working fine (Except for some replacements, as I don’t implemented this step yet)
But there are Texts, which are longer than the original Text and my question is:
Is there a way to re-arrange the Content within a table?
Below my Code for replacement:
Document pdfDocument = new Document(@“C:\temp\beforeReplace.pdf”);
// Create TextFragmentAbsorber object to find all text occurrences between “<%” and “%>”
TextFragmentAbsorber absorber = new TextFragmentAbsorber(@"<%.*%>", new TextSearchOptions(true));
// Accept the absorber for all pages
pdfDocument.Pages.Accept(absorber);
TextFragmentCollection textFragmentCollection = absorber.TextFragments;
foreach(TextFragment fragment in textFragmentCollection)
{
if(!fragment.Text.Contains(“Field.”)) continue;
string internalName = fragment.Text.Split(new string[] {“Field.”}, StringSplitOptions.RemoveEmptyEntries)[1];
internalName = internalName.Substring(0, internalName.LastIndexOf("%"));
fragment.TextState.Font = FontRepository.FindFont(“Arial”);
fragment.TextState.FontSize = 8;
fragment.TextState.ForegroundColor = Color.Navy;
if(item[internalName] != null)
{
fragment.Text = item[internalName].Value?.ToString() ?? “”;
}
}
pdfDocument.Save(@“C:\temp\afterReplace.pdf”);
Thanks!