Layout problem in Aspose.Words

Hi all,

I’m experiencing some problems with the layout after loading a Word document into Aspose.Words (Java), editing it and saving it back to PDF. My source document basically consists of just two bookmarks which are placed right after one another in separate paragraphs. Now when I read the document with Aspose and add a list (ol or ul) into the first bookmark (via insertHtml) the resulting PDF shows an unexpected large space between the list and the next paragraph (see Result_List.pdf). This large space is not there when I just add text into the first bookmark (see Result.pdf). Where does this space (seems to be one “additional” empty line) come from? Is this expected behavior for lists?

Regards,

Kevin

Hi Kevin,

Thanks for your inquiry. Yes, this is expected behavior. Please note that Aspose.Words mimics the same behavior as MS Word do. If you perform the same operation by using MS Word, you will get the same output.

Please save your final document into Docx and check the output in MS Word. There is an empty Paragraph after bookmark. Please see the attached image for detail. I have attached output Docx with this post. In your case, I suggest you please remove the empty Paragraph before second bookmark. Please let us know if you have any more queries.

Hi Tahir,

thanks for your quick response. Much appreciated!

Yet I’m still a bit puzzled as to where this empty paragraph comes from since it is not in my source document (Source.docx). I also don’t add it in my code since the only two things I’m actually doing is inserting some html for bookmark 1 and some for bookmark 2 (Code_sample.txt).

I’d gladly remove the empty paragraph (before writing the output) if I’d know where it is (or where it comes from). Any advice?

Regards,

Kevin

Hi Kevin,

Thanks for your inquiry. Your input document (Source.docx) has two Paragraphs. The first Paragraph has nodes BookmarkStart, Run, BookmarkEnd and Paragraph break. Please see the attached ‘firstParagraph.png’.

When you insert following html at the location of first bookmark (Test1), this html ends with Paragraph break and first Paragraph comes after inserted html as shown in ‘after insert html.png’.

builder.insertHtml("<ol><li>This is entry 1</li><li>This is entry 2</li>");

You can also check this behavior in MS Word by inserting ‘Enter’ inside a Paragraph, this splits the Paragraph into two Paragraphs. You are getting empty Paragraph because you are setting bookmark’s text to empty string. Please do not set bookmark’s text to empty string while inserting html and check the output.

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

Hi Tahir,

thanks again for your answer. I see what you mean and understand why the paragraph is there. If I don’t set the bookmarks text to an empty string I can see the paragraph with the bookmarks text (“Test1”) right after my inserted list.

Let me just rephrase my question one last time. Is it possible to use my Source.docx to get an output like in DesiredResult.docx by using insertHtml? I’m basically looking for some functionality to replace the two bookmarks (or their contents) with custom HTML (the first with a list, the other with some text). Is this possible?

Thanks for your time,

Kevin

Hi Kevin,

Thanks for your inquiry. In your case, I suggest you please use DocumentBuilder.write method to insert a string into the document after inserting html. Please use the following code snippet to achieve your requirements. Hope this helps you.

Document doc = new Document(MyDir + "source.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToBookmark("Test1");
doc.getRange().getBookmarks().get("Test1").setText("");
builder.insertHtml("<ol><li>This is entry 1</li><li>This is entry 2</li>");
builder.write("This is another paragraph");
doc.save(MyDir + "Out.docx");