Hi Support,
I am working on creating a word document using the Aspose Java API.While generating the document, I analyzed that an empty paragraph is inserted every time at the beginning of the document. Please find the below code snippet which I am using for creating a wrod document.
public AsposeWordDocumentBuilderImpl() throws MyException
{
try
{
Document document = new Document();
DocumentBuilder builder = new DocumentBuilder(document);
builder.getPageSetup().clearFormatting();
// move to first section
builder.moveToSection(0);
String paraText = builder.getCurrentParagraph().toTxt();
}
catch (Exception e)
{
throw new MyException(e);
}
}
In the above code I am getting the “paraText” as “\r\n” and this results as an empty paragraph in word document. Is there any way by which I can restrict creating this paragraph.
I have also attached the manual and Aspose generated document. By comparing these documents you can see an empty paragraph in the Aspose.doc in the very beginning.
Thanks,
Hi
Thanks for your request. Aspose.Words does not add any empty paragraphs at the beginning of document. But you should note that when you create a new MS Word document it already contains one section with one empty paragraph. So in case if you append your newly create paragraph to the end of this section the first empty paragraph remains. I think, this exactly what occurs on your side.
I think, you can use DocumentBuilder to insert content in the document. For example the following code will produce the document without empty paragraph at the beginning:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("This is Aspose generated document.");
doc.Save(@"Test001\out.doc");
Best regards,
Hi,
Thanks for your suggestions. But this solution will not work in my case because we are getting text in terms of Html String and Byte Stream, which we are providing to document builder.
So instead of using builder.Write(String) API we are using builder.insertHtml(String) in case of html and in case of ByteStream, we are using the code which is shown in below snippet
// This I am getting as an input from our application.
ByteArrayInputStream bais = new ByteArrayInputStream();
Document SrcDoc = new Document(bais);
Paragraph p = builder.insertParagraph();
Paragraph insertAfterNode = (Paragraph) p.getPreviousSibling();
if ((insertAfterNode.getNodeType() != NodeType.PARAGRAPH) &
(insertAfterNode.getNodeType() != NodeType.TABLE))
throw new Exception("The destination node should be either a paragraph or table.");
CompositeNode dstStory = insertAfterNode.getParentNode();
NodeImporter importer = new NodeImporter(srcDoc, insertAfterNode.getDocument(), ImportFormatMode.KEEP_SOURCE_FORMATTING);
for (Section srcSection: srcDoc.getSections())
{
/* Loop through all block level nodes (paragraphs and tables) in the body of the section.*/
for (Node srcNode: srcSection.getBody())
{
/* Let's skip the node if it is a last empty paragarph in a section.*/
if (srcNode.getNodeType() == NodeType.PARAGRAPH)
{
Paragraph para = (Paragraph) srcNode;
if (para.isEndOfSection() && !para.hasChildNodes())
continue;
}
/* This creates a clone of the node, suitable for insertion into the destination document.*/
Node newNode = importer.importNode(srcNode, true);
// Insert new node after the reference node.
dstStory.insertAfter(newNode, insertAfterNode);
insertAfterNode = newNode;
}
}
Please suggest me if I am doing any thing wrong or I need to do something else what I am doing here. I will really appreciate your quick help as this issue is very urgent.
Thanks for your support.
Hi
Thanks for your request. In your code you insert content after a paragraph. In case of empty document, this is the empty paragraph you see before your content.
In case when you insert a document, you can just check whether the first paragraph of the generated document is empty and remove it if so. Code will look like this:
while (!doc.FirstSection.Body.FirstParagraph.HasChildNodes)
{
doc.FirstSection.Body.FirstParagraph.Remove();
}
Hope this helps.
Best regards,
Hi Alexey,
I tried this but only one empty paragraph is got removed from the top of the Document. Actually after every Page Break, I am getting an empty paragraph. So I need something which can identify every page break and remove the first empty paragraph followed by the same.
Thanks
Hi
Thanks for your inquiry. Actually, there is no an empty paragraph after page break. Page Break is just a character so it is a part of paragraph text. So if you imagine a paragraph with some text and page break at the end of the paragraph, you will see a paragraph mark on the next page.
I think, in your case, you can use section breaks instead of page breaks. In this case, you will be able to remove empty paragraphs at the beginning of each section in your document.
Best regards,