Find an replace with multiple line

Hi

I need to replace a text in pdf with multiple lines , example

test1 => test1
test2
test3.

i tried to add \n but it does not work. It adds space like test1 test2 test3.

we are using TextFragmentAbsorber to find the text and replacing with textFragment.Text.
I would really appreciate any help here.

Thanks
Chandra

@aroc

To replace text in a PDF with multiple lines using Aspose.PDF, you can utilize the TextFragmentAbsorber to find the text and then set the new text with line breaks. However, simply using \n may not work as expected in the PDF context, as it might not render the line breaks correctly.

Instead, you can use the TextFragment’s setText method to include the new lines. Here’s an example of how you can achieve this:

import com.aspose.pdf.Color;
import com.aspose.pdf.Document;
import com.aspose.pdf.FontRepository;
import com.aspose.pdf.TextFragment;
import com.aspose.pdf.TextFragmentAbsorber;
import com.aspose.pdf.TextFragmentCollection;

public class ReplaceTextWithMultipleLines {
    public static void main(String[] args) {
        // Open the PDF document
        Document pdfDocument = new Document("input.pdf");
        
        // Create a TextFragmentAbsorber to find the text to replace
        TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("test1");
        
        // Accept the absorber for the first page of the document
        pdfDocument.getPages().get_Item(1).accept(textFragmentAbsorber);
        
        // Get the extracted text fragments into a collection
        TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();
        
        // Get the first occurrence of the text and replace it
        TextFragment textFragment = textFragmentCollection.get_Item(1);
        
        // Update text with multiple lines
        textFragment.setText("test1\n\ntest2\n\ntest3");
        textFragment.getTextState().setFont(FontRepository.findFont("Verdana"));
        textFragment.getTextState().setFontSize(12);
        textFragment.getTextState().setForegroundColor(Color.getBlack());
        
        // Save the updated PDF file
        pdfDocument.save("Text_Updated.pdf");
    }
}

In this example, the setText method is used to replace “test1” with “test1”, “test2”, and “test3” on separate lines. Make sure to adjust the font size and other properties as needed to fit your layout requirements.

If you encounter any issues with the line breaks not appearing as expected, you may need to adjust the positioning or formatting of the TextFragment to ensure it displays correctly in the PDF.

Thanks yor your replay. I need same in .net and could you please give me sample for c#

@aroc

Below is the sample C# code equivalent to the above Java code:

using Aspose.Pdf;
using Aspose.Pdf.Text;

class ReplaceTextWithMultipleLines
{
    static void Main()
    {
        // Open the PDF document
        Document pdfDocument = new Document("input.pdf");

        // Create a TextFragmentAbsorber to find the text to replace
        TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("test1");

        // Accept the absorber for the first page of the document
        pdfDocument.Pages[1].Accept(textFragmentAbsorber);

        // Get the extracted text fragments into a collection
        TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;

        // Get the first occurrence of the text and replace it
        TextFragment textFragment = textFragmentCollection[1];

        // Update text with multiple lines
        textFragment.Text = "test1\n\ntest2\n\ntest3";
        textFragment.TextState.Font = FontRepository.FindFont("Verdana");
        textFragment.TextState.FontSize = 12;
        textFragment.TextState.ForegroundColor = Color.Black;

        // Save the updated PDF file
        pdfDocument.Save("Text_Updated.pdf");
    }
}

AsposeFindReplaceConsole (1).zip (79.2 KB)

Hi

Please see attached sample and unfortunetly it did not work. Could you please check.

Thanks
Chandra

@aroc

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-59933

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.