insertDocument results 1 one blank line before

Hi there,
I am using the examples found on your forums to insert a document (nodetype = paragraph) into a main document. I do this via insertAfter. But everytime insertDocument is executed then the insertion starts with a blank line. I tried to delete these blank lines, but unfortunate until know all our attempts did not succeed.
For example: We have tried to setup parentPara.getParagraphFormat().setSpaceBefore(0); This did not work.

part of our code
public void fieldMerging(FieldMergingArgs e) throws Exception {
if (this.documentBuilder == null)
this.documentBuilder = new DocumentBuilder(e.getDocument());
if (this.macroButton.getSoort().equals(“B”)) {
if (log.isDebugEnabled()) {
log.debug("Behandelen Bouwsteen: " + e.getFieldValue());
}
Document subDoc = new Document((String) e.getFieldValue());
execute(this.opdrachtbestandWrapper, subDoc, null);
insertDocument(getParagraaf(e.getDocumentFieldName()), subDoc);
// Met log.debug(subDoc.toString(SaveFormat.TEXT)); kan de inhoud van een document eenvoudig zichtbaar
// gemaakt worden.
}
if (this.macroButton.getSoort().equals(“P”)) {
String bodyBlok = (String) e.getFieldValue();
if (log.isDebugEnabled()) {
log.debug("Behandelen Paragraaf: " + e.getFieldValue());
}
Node insertBodyblok = getParagraaf(e.getDocumentFieldName());
Document joinDoc = new Document();
for (String blok = this.opdrachtbestandWrapper.getFirstBlok(bodyBlok); blok != null; blok =
this.opdrachtbestandWrapper.getNextBlok(bodyBlok)) {
Document subDoc = createDocument(blok);
execute(this.opdrachtbestandWrapper, subDoc, this.opdrachtbestandWrapper.getBodyBlok());
joinDoc.appendDocument(subDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
}
insertDocument(insertBodyblok, joinDoc);

		if (e.getFieldValue() == null) {
			this.documentBuilder.moveToMergeField(e.getFieldName());
			// Nothing to do for this field.
		}
	}

}

protected void insertDocument(Node aNode, Document srcDoc) throws Exception {
Node insertAfterNode = aNode;
if ((insertAfterNode.getNodeType() != NodeType.PARAGRAPH) && (insertAfterNode.getNodeType() != NodeType.TABLE))
throw new IllegalArgumentException(“The destination node should be either a paragraph or table.”);

	// We will be inserting into the parent of the destination paragraph.
	CompositeNode dstStory = insertAfterNode.getParentNode();

	// This object will be translating styles and lists during the import.
	NodeImporter importer =
			new NodeImporter(srcDoc, insertAfterNode.getDocument(), ImportFormatMode.KEEP_SOURCE_FORMATTING);

	// Loop through all secintions in the source document.
	for (Section srcSection : srcDoc.getSections()) {
		// Loop through all block level nodes (paragraphs and tables) in the
		// body of the section.
		for (Node srcNode : (Iterable<Node>) srcSection.getBody()) {
			// Let's skip the node if it is a last empty paragraph in a
			// section.
			if (srcNode.getNodeType() == (NodeType.PARAGRAPH)) {
				Paragraph paragraph = (Paragraph) srcNode;
				if (paragraph.isEndOfSection() && !paragraph.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;
		}
	}
}

private Paragraph getParagraaf(String name) {
	NodeCollection starts = this.sjabloon.getChildNodes(NodeType.FIELD_START, true);
	for (FieldStart start : (Iterable<FieldStart>) starts) {
		if (start.getFieldType() == FieldType.FIELD_MACRO_BUTTON) {
			Paragraph parentPara = (Paragraph) start.getAncestor(NodeType.PARAGRAPH);
			parentPara.getParagraphFormat().setSpaceBefore(0);
			String testP = parentPara.getText();
			if (testP.indexOf(name) > 0) {
				try {
					start.getField().remove();
				} catch (Exception e) {
					// ARGUMENTATIE: onderliggend wordt door Aspose een @link(Exception} gegooid. Hier kunnen
					// we niets aan veranderen, maar we willen in dit geval wel verder gaan.
					log.warn("Fout bij ophalen paragraaf", e);
				}
				return parentPara;
			}
		}
	}
	return null;
}

Hopefully you can help me with this problem

kind regards
Marcel van Olffen

@maoynwa,

Thanks for your inquiry. In your case, we suggest you please move the cursor to the desired location and use DocumentBuilder.InsertDocument method to insert content of the document. Hope this helps you.

If you still face problem, please ZIP and attach following resources here for testing.

  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please create a simple Java application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

Hi Tahir,

thanks for your response.

I have tried your solution in our software however that did not the trick.

Therefor I have included the zip with a test.

there is a main.doc (this is a template)

and a sub.doc, which I have tried to insert with mailmerge.

If you run this test, you will see sub.doc is twice inserted, but before the insertion you’'ll see an empty line.

Those empty lines have to deleted in the resulting document

kind regards

Marcel van Olffen
Anva bv
Netherlands

asposetest.zip (69.5 KB)

@maoynwa,

Thanks for sharing the detail. We have tested the scenario using following code example and have not found the shared issue.

Document doc = new Document(MyDir + "MAIN.DOC");

Document subdoc1 = new Document(MyDir + "Sub.DOC");
Document subdoc2 = new Document(MyDir + "Sub.DOC");

DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToDocumentEnd();
builder.insertDocument(subdoc1, ImportFormatMode.KEEP_SOURCE_FORMATTING);

builder.moveToDocumentEnd();
builder.insertDocument(subdoc2, ImportFormatMode.KEEP_SOURCE_FORMATTING);

doc.save(MyDir + "output.doc");

Please share the following documents here for our reference. We will then provide you more information about your query along with code.

  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.

@tahir.manzoor
Thanks for your response
I have included three files: asposetest.zip with a test program Document2PdfServiceTest
After unpacking and build you can run this test in debug, please set a breakpoint at line 59 otherwise the resulting main.doc is deleted.
asposetest.zip (82.4 KB)

I have also included the wrong main.doc as a result and a expected main-ok.doc wthin the zip-file in folder test-results

kind regards
Marcel van Olffen
ANVA bv

@maoynwa,

Please use the DocumentBuilder.insertDocument method as shown in my previous post to get the desired output. If you want to use the insertDocument method shared in your first post, please remove the empty paragraph after inserting the document.

@tahir.manzoor
Thanks for your solution.
We still using documentinsert after this we remove the paragraph.
The reason is, our documents to insert are not in a sequence

This works now.

kind regards
Marcel van Olffen
Anva bv
the Netherlands

@maoynwa,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.