How can i replace a Section_Break with a Paragraph_Break

in my document, it has a SECTION_BREAK_CONTINUOUS, how can i replace it with a Paragraph_Break ?
here’s my code which doesn’t work :<<<<<

for (Paragraph para : paragraphs) {
    // 获取段落的文本
    String text = para.getText();
    if (text.endsWith(ControlChar.SECTION_BREAK)){
        text = text.replace(ControlChar.SECTION_BREAK, ControlChar.PARAGRAPH_BREAK);
        para.getRuns().clear();
        para.getRuns().add(new Run(doc, text));
    }
}


the output remains the SECTION_Break

btw, can you tell me how to upload and show the picture directly , cause I always see you editing the ‘image.png’ to be seen , I dont want to bother you with such trifle. @alexey.noskov

@Madecho Section break is not a simple character in the document structure. To remove section break you should concatenate sections’ content and then remove the second section. You can use the following code to achieve this:

Document doc = new Document("C:\\Temp\\in.docx");

Iterable<Section> sections = doc.getSections();
for (Section s : sections)
{
    // Skip the first section.
    if (s == doc.getFirstSection())
        continue;

    // Remove section break Continuous
    if (s.getPageSetup().getSectionStart() == SectionStart.CONTINUOUS)
    {
        ((Section)s.getPreviousSibling()).appendContent(s);
        s.remove();
    }
}
doc.save("C:\\Temp\\out.docx");

You can use standard markdown syntax, i.e. ![](/uploads/default/97615) instead of <a class="attachment" href="/uploads/default/97615">image.png</a>. Alternative you can use HTML img tag.

1 Like

@alexey.noskov hello alexey, after using your code, there’s another problem occurs, cross references can’t display as usual

, why? I searched and used ‘updateFields()’, but that didn’t work

@Madecho Could you please attach your input and output documents here for testing? We will check the issue and provide you more information. I am afraid, it is impossible to analyze the problem by screenshots.