Header & Footers Not Included When INCLUDETEXT Field in used in WORD

Hi Team,

We are using INCLUDETEXT field in Document.
We have two documents: one document having Headers and Footers & some text whereas another document has only some text[~ no headers and footers].

We are adding INCLUDETEXT fields in Final Document. Final document doesn’t contain Header and footers. It only has Text of both the documents.

Please check below files to check what kind of file it is and what is expected behaviour.

FinalDocument_backup.docx (14.9 KB)
Multipart1.docx (16.4 KB)
Multipart2.docx (12.9 KB)

Code Snippet :

	private void insertIncludeTextField(String path)
	{
		String field = "INCLUDETEXT  \"<file>\"  \\* MERGEFORMAT"; //$NON-NLS-1$

		try
		{
			if (FileUtils.fileExists(path))
			{
				File f = new File(path);
				path = URIUtils.decodeURI(f.toURI());
			}

			field = field.replace("<file>", path); //$NON-NLS-1$
			Field fld = docBuilder.insertField(field, ""); //$NON-NLS-1$

			if (updateFields == UpdateFieldsMethod.progressive)
			{
				fld.update();
				docBuilder.moveTo(fld.getEnd());
			}
		}
		catch (Exception e)
		{
			RPELog.getInstance().logMessage(MessagesMapping.RPE_3285, new String[]
			{ path }, e);
		}
	}

        Then we are doing  - doc.updateFields();

Thanks

@Prem_Parmar

When using the INCLUDETEXT field in Word documents, it is important to understand that the headers and footers from the source document are not included in the final document. This behavior occurs because the INCLUDETEXT field only merges the main body text of the referenced document, excluding any headers and footers.

To include headers and footers in your final document, you will need to manually add them after inserting the INCLUDETEXT fields. You can achieve this by using the DocumentBuilder class to create headers and footers in your final document.

Here’s a simple example of how to add headers and footers after inserting the INCLUDETEXT fields:

// Assuming 'doc' is your final document
DocumentBuilder builder = new DocumentBuilder(doc);

// Add Header
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.Write("Your Header Text Here");

// Add Footer
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
builder.Write("Your Footer Text Here");

// Update fields if necessary
doc.UpdateFields();

By following this approach, you can ensure that your final document contains the desired headers and footers alongside the text from the included documents.

I think Header and footer should be automatically added in Final Document if First File has Header/Footers and We are adding that file using INCLUDETEXT field same as we do manually in Word.

@Prem_Parmar The behavior is expected. You can observe the same behavior in MS Word when insert INCLUDETEXT field.

If you need to insert the document into the destination document including headers/footers, you can use Document.appendDocument method.

Hi @alexey.noskov, If We use INCLUDETEXT Field for the file having Headers/Footers in empty file, Headers and footers are getting added in Final File. I have checked in MS Word.

@Prem_Parmar
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): WORDSNET-28683

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.