Insert New Lines after Some Keywords or Spaces in Word Document using Java | Paragraph Formatting

Trying to add text in builder

Firstline…secondline …thridline of paragraph in builder.writeln()
Expected alignment should be
Firstline…
secondline…
thridline…

but currently displaying as
Firstline…
Space Secondline …
Space thridline…

@saranyasrinivasan92,

Please try the following code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Firstline…" + ControlChar.LINE_BREAK +
                    "secondline…" + ControlChar.LINE_BREAK +
                    "thridline");
doc.save("E:\\temp\\awjava-20.6.docx");

Hardcoding is not possible.

But in paragraph we dont know each line right… just based on words space it will go to next line

@saranyasrinivasan92,

You can replace any string of text (dots, spaces etc) with Line Breaks. Please try running the following code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Firstline...secondline...thridline");

doc.getRange().replace("...", ControlChar.LINE_BREAK);
doc.save("E:\\temp\\awjava-20.6.docx");

… is just sample text lets consider paragaraph with more than 3 lines

@saranyasrinivasan92,

To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:

  • Your simplified input Word document (if any)
  • Aspose.Words for Java 20.6 generated output DOCX file showing the undesired behavior
  • Your expected DOCX file showing the desired output. You can create this document by using MS Word.
  • Please also create a standalone simple Java application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing. Please do not include Aspose.Words JAR files in it to reduce the file size.

As soon as you get these pieces of information ready, we will start further investigation into your scenario and provide you more information. Thanks for your cooperation.