Вставить текст в Word без преобразования в абзац

#java #ms-word #apache-poi

#java #ms-word #apache-poi

Вопрос:

Ниже приведена строка, которая у меня есть

     String def = "R. (12) Blessed the people the Lord has chosen to be his own.rn"
              "Blessed the nation whose God is the LORD,rn"
              "the people he has chosen for his own inheritance.rn"   "From heaven the LORD looks down;rn"
              "he sees all mankind.rn"   "R. Blessed the people the Lord has chosen to be his own.rn"
              "From his fixed throne he beholdsrn"   "all who dwell on the earth,rn"
              "He who fashioned the heart of each,rn"   "he who knows all their works.rn"
              "R. Blessed the people the Lord has chosen to be his own.rn"   "Our soul waits for the LORD,rn"
              "who is our help and our shield,rn"   "For in him our hearts rejoice;rn"
              "in his holy name we trust.rn"   "R. Blessed the people the Lord has chosen to be his own.";
  

Я использую библиотеку Apache POI для вставки этой строки в документ Word

 XWPFParagraph paragraph8 = document.createParagraph();
XWPFRun run8 = paragraph8.createRun();
run8.setText(def);
  

Я хочу текст в word в формате ниже

 R. (12) Blessed the people the Lord has chosen to be his own.
Blessed the nation whose God is the LORD,
the people he has chosen for his own inheritance.
From heaven the LORD looks down;
he sees all mankind.
R. Blessed the people the Lord has chosen to be his own.
From his fixed throne he beholds
all who dwell on the earth,
He who fashioned the heart of each,
he who knows all their works.
R. Blessed the people the Lord has chosen to be his own.
Our soul waits for the LORD,
who is our help and our shield,
For in him our hearts rejoice;
in his holy name we trust.
R. Blessed the people the Lord has chosen to be his own.
  

Но я получаю его как абзац

 R. (12) Blessed the people the Lord has chosen to be his own. Blessed the nation whose God is the LORD, the people he has chosen for his own inheritance. From heaven the LORD looks down; he sees all mankind. R. Blessed the people the Lord has chosen to be his own. From his fixed throne he beholds all who dwell on the earth, He who fashioned the heart of each, he who knows all their works. R. Blessed the people the Lord has chosen to be his own. Our soul waits for the LORD, who is our help and our shield, For in him our hearts rejoice; in his holy name we trust. R. Blessed the people the Lord has chosen to be his own.   
  

введите описание изображения здесь

Ответ №1:

r n не является синтаксисом Word для разрыва строки. Может быть полезно установить несколько строк в Word, а затем изучить XML. Вы пытались установить весь отрывок как 1 прогон в 1 абзаце. Если каждая строка заканчивается мягким возвратом, каждая строка будет выполняться отдельно:

 <w:p w14:paraId="52A70B3B" w14:textId="21AEAB8E" w:rsidR="00A934F5" w:rsidRDefault="00BD29B4" w:rsidP="00BD29B4">
    <w:r>
        <w:t>R. (12) Blessed the people the Lord has chosen to be his own.</w:t>
    </w:r>
    <w:r>
        <w:br/>
    </w:r>
    <w:r>
        <w:t>Blessed the nation whose God is the LORD,</w:t>
    </w:r>
    <w:r>
        <w:br/>
    </w:r>
  

Если каждая строка разделена жестким возвратом, каждая строка будет отдельным абзацем:

 <w:p w14:paraId="21B22AC3" w14:textId="77777777" w:rsidR="009023FF" w:rsidRDefault="009023FF" w:rsidP="009023FF">
    <w:r>
        <w:t>Blessed the nation whose God is the LORD,</w:t>
    </w:r>
</w:p>
<w:p w14:paraId="44B230A2" w14:textId="09D2C1FA" w:rsidR="009023FF" w:rsidRDefault="009023FF" w:rsidP="009023FF">
    <w:r>
        <w:t>the people he has chosen for his own inheritance.</w:t>
    </w:r>
</w:p>