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.
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");
}
}
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.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
Enables storage, such as cookies, related to analytics.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.