Aspose.pdf替换超长字符没自动换行

image.png (42.4 KB)
将这一段字符替换成 “222222222222222222222222222222222222222222” 怎么自动换到第二行,而不是超出右侧边界

@zhangsanYaxin
You need to find the fragment to be replaced using TextFragmentAbsorber and execute
absorber.TextFragments[0].Text=“new text”.
And before set option

var tfa = new TextFragmentAbsorber("{Variable}");
tfa.TextReplaceOptions.ReplaceAdjustmentAction = TextReplaceOptions.ReplaceAdjustment.WholeWordsHyphenation;

还是没有解决问题,麻烦再帮指点一下:
1.测试文件:
copy_pdf_test.pdf (78.7 KB)
2.再次叙述下问题
将"fan" 替换成 “321312222222222222222222222222” 后 右侧文本被挤出
image.png (104.1 KB)
3.使用的代码
public class Test3 {

public static final Set<String> set = new HashSet();

public static void main(String[] args) {
    // Open document
    Document pdfDocument = new Document("D:\\word_test\\copy_pdf_test.pdf");

    replace(pdfDocument,"fan","321312222222222222222222222222");

    pdfDocument.save("D:\\word_test\\qq.pdf");

}

public static void replace( Document pdfDocument,String source,String target){
    TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(source);

    TextReplaceOptions textReplaceOptions = new TextReplaceOptions();
    textReplaceOptions.setReplaceAdjustmentAction(TextReplaceOptions.ReplaceAdjustment.WholeWordsHyphenation);
    textFragmentAbsorber.setTextReplaceOptions(textReplaceOptions);

    // Accept the absorber for all pages of document
    pdfDocument.getPages().accept(textFragmentAbsorber);
    // Get the extracted text fragments into collection
    TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();

    // Loop through the fragments
    for (TextFragment textFragment :  textFragmentCollection) {
        Position baselinePosition = textFragment.getBaselinePosition();
        double xIndent = baselinePosition.getXIndent();
        double yIndent = baselinePosition.getYIndent();
        if(!set.contains(baselinePosition.getXIndent() + "-" + baselinePosition.getYIndent())){
            set.add(xIndent + "-" + yIndent);
            // Update text and other properties
            textFragment.setText(target);
        }
    }
}

}

2.测试代码在附件中

@zhangsanYaxin
should use code like this (written in C#)

    {
        // Open document
        Document pdfDocument = new Document(myDir + "copy2_pdf_test.pdf");

        replace(pdfDocument, "fan", "321312222222222222222222222222");

        pdfDocument.Save(myDir + "qq2.pdf");
    }

    public static void replace(Document pdfDocument, string source, string target)
    {
        var textFragmentAbsorber = new TextFragmentAbsorber(source);
        textFragmentAbsorber.TextReplaceOptions.ReplaceAdjustmentAction = TextReplaceOptions.ReplaceAdjustment.WholeWordsHyphenation;
        pdfDocument.Pages[1].Accept(textFragmentAbsorber);

        textFragmentAbsorber.TextFragments[1].Text = target;
    }

However, it doesn’t work for the attached document because the paragraph is continuous text, with no spaces.
I will create a task for the development team regarding this issue.
To test, I used a document with a space added - with it, as you can check, the code works correctly.
copy2_pdf_test.pdf (135.1 KB)

@zhangsanYaxin
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-55571

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.