I believe I have the same problems as the person in the recent post called: Find and replace text on PDF document-Replaced text is blank (I can't post the url directly), but as this post hasn't been followed up for a day or two, I thought I'd start a new one.
The problem is that I've also found that the sample code 'Find-and-replace-text-on-PDF-document-in-C' finds the text size/width and creates a white rectangle over it, but doesn't seem to want to add the replacement text.
Here's my version of the code - NB I've converted it from c# to vb, so the problem may well be one of translation, but if it is, I can't see what needs to change:
- Code: Select all
Shared Sub stamp_pdf_with_Spire(sInFilePath As String, sOutFilePath As String)
Dim doc As New PdfDocument()
doc.LoadFromFile(sInFilePath)
Dim page As PdfPageBase = doc.Pages(1)
Dim collection As PdfTextFindCollection = page.FindText("%Forename%", TextFindParameter.IgnoreCase)
Dim newText As String = "Replacement text"
'Creates a brush
Dim brush As PdfBrush = New PdfSolidBrush(Color.DarkBlue)
'Defines a font
Dim font As New PdfTrueTypeFont(New Font("Arial", 12.0F, FontStyle.Regular))
Dim rec As RectangleF
For Each find As PdfTextFind In collection.Finds
' Gets the bound of the found text in page
rec = find.Bounds
page.Canvas.DrawRectangle(PdfBrushes.White, rec)
page.Canvas.DrawString(newText, font, brush, rec)
Next find
doc.SaveToFile(sOutFilePath)
End Sub
Perhaps seeing the vb will help resolve this?
Thanks for reading!