We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Page break doesn't consider next line as new Paragraph

Hi Team,

When I write “line 1”, “insert page break”, “apply style” and then write “line 2”, the style is being applied to “line 1” as well. The page break is not being considered as end of the paragraph. “Line 1” should be one paragraph and “Line 2” should be a new paragraph.

As a workaround, insert blank line before applying style will fix the issue. However, the extra line is seen in the second page.

Please find the code below and other necessary attachment along with output.

This issue is seen in Aspose Words for Java 11.3.0.0 as well as in 11.10.

Thanks,
Kumar

import java.io.FileInputStream;
import java.io.InputStream;

import com.aspose.words.BreakType;
import com.aspose.words.BuildVersionInfo;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.License;

public class PreviousParagraph
{

    public static void main(String[] args) throws Exception
    {
        printAsposeVersion();
        setupLicense();
        Document doc = new Document("c:\abc.dot");
        DocumentBuilder builder = new DocumentBuilder(doc);
        builder.write("Aspose product and version is = "\ + BuildVersionInfo.getProduct() + " : "\ + BuildVersionInfo.getVersion());
        builder.writeln();
        paragraphStyleIndent(builder, doc);
        save(doc);
        System.out.println("done");
    }

    private static void save(Document doc) throws Exception
    {
        doc.save("C:\test_old.doc");
        doc.save("C:\test_old.pdf");
    }

    private static void printAsposeVersion()
    {
        System.out.println("Aspose product and version is = "\ + BuildVersionInfo.getProduct() + " : "\ + BuildVersionInfo.getVersion());
    }

    private static void setupLicense() throws Exception
    {
        InputStream licstream = null;

        try
        {
            License license = new License();
            licstream = new FileInputStream("src/Aspose.Words.lic"); //$NON-NLS-1$
            license.setLicense(licstream);
        }
        finally
        {
            if (licstream != null)
            {
                licstream.close();
            }
        }
    }

    private static void paragraphStyleIndent(DocumentBuilder docBuilder,
        Document doc) throws Exception
    {

        docBuilder.write("This is line 1");
        docBuilder.insertBreak(BreakType.PAGE_BREAK);
        docBuilder.getParagraphFormat().setStyleName("Heading 1");
        docBuilder.write("This is line 2");
    }

}

Hi Kumaraswamy,

Thanks for your inquiry. Please note that Aspose.Words tries to mimic the same behavior as MS Word do. If you insert page break in a Paragraph by using MS Word, the paragraph formatting will be same.

In your case, please use the DocumentBuilder.writeln method instead of write method. The DocumentBuilder.writeln method inserts a string and a paragraph break into the document.

You may also use BreakType.SECTION_BREAK_NEW_PAGE to achieve your requirement as shown in following code snippet.

builder.writeln("This is line 1");

System.out.println(builder.getParagraphFormat().getStyleName());
builder.insertBreak(BreakType.PAGE_BREAK);
builder.getParagraphFormat().setStyleName("Heading 1");
builder.writeln("This is line 2");
builder.write("This is line 1");
System.out.println(builder.getParagraphFormat().getStyleName());
builder.insertBreak(BreakType.SECTION_BREAK_EVEN_PAGE);
builder.getParagraphFormat().setStyleName("Heading 1");
builder.write("This is line 2");

Hi Tahir,

Thanks for your response. I understood that any SECTION_BREAK creates a new section.

However, when I try the same in MS Word

  1. Open a MS word doc
  2. Set style to “Normal”
  3. Insert line 1
  4. Insert page break
  5. Set style to “Heading 2”
  6. Insert line 2

The out of the document is “line 1” is written in style “Normal” and “line 2” is written using style “Heading 1”.
In a document that is generated using Aspose, both “line 1” and “line 2” are written using style “Heading 1”, which seems to be a bug.

Thanks,
Kumar

Hi Kumaraswamy,

Thanks for your inquiry. There is difference between DocumentBuilder.Write and DocumentBuilder.Writeln.

DocumentBuilder.Write : Inserts a string into the document at the current insert position.
DocumentBuilder.Writeln : Inserts a string and a paragraph break into the document.

If you use the write method as shown in following code snippet, both lines will be in one Paragraph node. Please see the attached dom-write.png for detail.

builder.write("This is line 1");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.getParagraphFormat().setStyleName("Heading 1");
builder.write("This is line 2");

If you use the writeln method as shown in following code snippet, both lines will be in two Paragraph nodes as this method inserts a string and a paragraph break into the document. Please see the attached dom-writeln.png for detail. This is the same scenario which you described in your last post.

The output file generated with following code snippet will have text formatting as ‘Normal’ for first line and ‘Heading 1’ for line 2.

builder.writeln("This is line 1");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.getParagraphFormat().setStyleName("Heading 1");
builder.writeln("This is line 2");

Hope this answers your query. Please let us know if you have any more queries.

Hi Kumar,

Further to what Tahir has stated, if you run those steps in Microsoft Word you will see that it actually create a new paragraph after the page break (MS Word does this for you automatically) which is why you can get two different paragraph styles for each line. Enable formatting marks to see this.

If you remove the paragraph break on the first page (click onto the end of the paragraph and press delete) you will see there is only one paragraph and the behavior matches Aspose.Words.

Thanks,

Thanks Tahir and Adam. It works as you mentioned and I tried it in MS Word and the behavior is same. Yes, one cannot have 2 styles in the same line of text (within the same paragraph) unless one selects few characters manually and apply a different style.

One last question. Is there any way to insert paragraph break without creating a new line?

Hi Kumar,

Thanks for your inquiry. There is no way to add paragraph break without creating new line in both Aspose.Words and MS Word. When you press ENTER in the MS Word, you get a paragraph break. By pressing SHIFT+ENTER, you get a line break.

The ParagraphBreak breaks between paragraphs which means that the text insertion after paragraph break will not be on the same line.

Moreover, a valid paragraph in Microsoft Word always ends with a paragraph break character and a minimal valid paragraph consists just of a paragraph break. The Paragraph class automatically appends the appropriate paragraph break character at the end and this character is not part of the child nodes of the Paragraph, therefore a Paragraph can be empty.

Thanks Tahir.