Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Wed Jun 23, 2021 4:12 pm

Hi

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!

Ash_Precision2
 
Posts: 9
Joined: Mon Jun 14, 2021 9:17 am

Thu Jun 24, 2021 7:10 am

Hello,

Thank you for your inquiry.
Based on your description, I thought the issue you faced should be that the rectangle height is small, which cannot accommodate the font height(12.0F) of replacement text. In order to avoid this behavior, you can set a smaller font size of replacement text, or you can set the LineLimit as false to force the display of text. If the problem still exists, please provide your PDF file for further investigation. You could attach it here or send to us via email (support@e-iceblue.com). Thanks in advance.
Code: Select all
 
        Private Shared Sub stamp_pdf_with_Spire(ByVal sInFilePath As String, ByVal sOutFilePath As String)
        Dim doc As PdfDocument = 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"
        Dim brush As PdfBrush = New PdfSolidBrush(Color.DarkBlue)
        ' Defines a font
        Dim font As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("Arial", 12!, FontStyle.Regular))
        Dim format As PdfStringFormat = New PdfStringFormat
        format.LineLimit = false
        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, format)
        Next
        doc.SaveToFile(sOutFilePath)
    End Sub


Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1643
Joined: Wed Apr 07, 2021 2:50 am

Thu Jun 24, 2021 7:58 am

Thanks - reducing the font size to test this worked just fine.

Can I suggest you add some text to this effect on the 'Find and replace text on PDF document in C#' sample code page to help anyone else who comes across this?

Cheers!

Ash_Precision2
 
Posts: 9
Joined: Mon Jun 14, 2021 9:17 am

Thu Jun 24, 2021 8:29 am

Hi Annika

Just to follow this up, I've replaced the text by using the Linelimit=False option, so I need to expand the rectangle to allow for the font descender (and the rest of the text) to show.

What's the best way to work out what the size should be to ensure the text is on the same level as other text on the same line that is not replaced?

I imagine it must relate to the font.descent value?

I've tried variations on:

Code: Select all
rec.Height = rec.Height - font.Descent
rec.Y = rec.Y + (font.Descent / 2)


This gets it pretty close, but is there a more exact method?

I've attached a couple of before and after pdf files - you can see the text on page 2 in each case.

Thanks for your help with this.

Ash_Precision2
 
Posts: 9
Joined: Mon Jun 14, 2021 9:17 am

Thu Jun 24, 2021 10:40 am

Hello,

Thank you for your feedback.
1) We will consider your suggestion. Thanks again for your kind advice.
2) I suggest that you use the following code to set the rectangle, which will produce a better effect.
Code: Select all
 float sub = (font.Height- rec.Height) / 2;
 rec.Height = font.Height;
 rec.Y = rec.Y - sub;

If there is any question, please feel free to write back.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1643
Joined: Wed Apr 07, 2021 2:50 am

Thu Jun 24, 2021 12:49 pm

Thanks - I'll give this a go.

Ash_Precision2
 
Posts: 9
Joined: Mon Jun 14, 2021 9:17 am

Fri Jun 25, 2021 2:27 am

Hello,

You're welcome.
Looking forward to your feedback after the test.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1643
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.PDF