All,
I am trying to find and replace text in a PDF. However, when I do so, my PDF format changes completely. Here is my code:
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(GetStreamFromUrl(inputFile));
logger.Info($"Successfully got PDF Loaded Document from {inputFile}.");
PdfLoadedPageCollection loadedPages = loadedDocument.Pages;
PdfDocument newPdfDocument = new PdfDocument();
foreach (PdfPageBase page in loadedPages)
{
string extractedText = page.ExtractText();
foreach (var item in bindVarDictionary)
{
extractedText = extractedText.Replace(item.Key, item.Value);
}
PdfPage newPage = newPdfDocument.Pages.Add();
//Create PDF graphics for the page
PdfGraphics graphics = newPage.Graphics;
//Set the standard font.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);
//Draw the text.
graphics.DrawString(extractedText, font, PdfBrushes.Black, new PointF(0, 0));
}
Does anyone know how I can do a simple find/replace of text in a PDF without totally changing my formatting?
Thanks,
Philip