Hi,
I’m trying to replace text in a pdf. It needs to keep the font used on the text, especially if it’s an embedded font.
If I simply replace only the text, it switches the font to Times New Roman. If I try forcing it by using a FontAbsorber
to find the font in the document, it replaces the text with blue boxes.
Am I doing something wrong or are there issues with embedded fonts?
Using the latest Aspose.Pdf package for .Net, this is a .Net6 project.
This is my class:
using Aspose.Pdf;
using Aspose.Pdf.Text;
namespace PDFTest
{
internal class AsposeTest
{
private static Dictionary<string, string> details1 = new Dictionary<string, string>()
{
{ "{{CONSUMER}}", "Joe Bloggs" },
{ "{{TEACHER}}", "Jane Doe"},
{ "{{DATE}}", DateTime.Now.ToString() }
};
public static void Main()
{
SetLicense();
var inputFolder = @"C:\PDFTest\Original PDFs";
var outputFolder = @"C:\PDFTest\Output PDFs";
foreach (var fileInfo in new DirectoryInfo(inputFolder).GetFiles())
{
// Open document
Document pdfDocument = new Document(fileInfo.FullName);
foreach (var detail in details1)
{
// Create TextAbsorber object to find all instances of the input search phrase
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(detail.Key);
// Accept the absorber for all the pages
pdfDocument.Pages.Accept(textFragmentAbsorber);
// Get the extracted text fragments
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
// Loop through the fragments
foreach (TextFragment textFragment in textFragmentCollection)
{
var fontName = textFragment.TextState.Font.FontName;
var tempFontSize = textFragment.TextState.FontSize;
var tempForegroundColour = textFragment.TextState.ForegroundColor;
var tempBackgroundColour = textFragment.TextState.BackgroundColor;
var textState = textFragment.TextState;
// Update text and other properties
var fontabsorber = new FontAbsorber();
fontabsorber.Visit(pdfDocument);
//***Test 1, by itself, replaces text with system default Times New Roman
textFragment.Text = detail.Value;
//***Test 2, replaces the text with boxes.
textFragment.TextState.Font = fontabsorber.Fonts.First(x => x.FontName == fontName);
textFragment.TextState.FontSize = tempFontSize;
if (tempForegroundColour != null) textFragment.TextState.ForegroundColor = tempForegroundColour;
if (tempBackgroundColour != null) textFragment.TextState.BackgroundColor = tempBackgroundColour;
}
}
// Save resulting PDF document.
pdfDocument.Save(Path.Combine(outputFolder, fileInfo.Name));
}
}
public static void SetLicense()
{
// Initialize license object
Aspose.Pdf.License license = new Aspose.Pdf.License();
try
{
// Set license
license.SetLicense("Aspose.Pdf.NET.lic");
}
catch (Exception)
{
// something went wrong
throw;
}
Console.WriteLine("License set successfully.");
}
}
}
Thanks,
Mike