Hi,
I am facing some problems while extracting header/footer from one document and appending it in another document.
- After running the code given below the values of the header/footer are not appearing in the appended document if it is saved in docx format. But it will work if it is saved in doc format.
- Even with the doc format the values are again not appearing and the formatting of the header/footer gets changed(i.e. the color of the header changes) if the server is running in an linux environment.
Here is my code:-
public static void main(String[] args) {
try
{
String dir = "E:\\mergedfiles\\";
String headerFooterWatermarkDocumentFilePath = dir + "Doc1.docx";
Document doc = new Document(dir + "testHF1.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
getAppendedHeaderFooterWatermark(builder, headerFooterWatermarkDocumentFilePath);
builder.getDocument().save(dir + "document1.doc");
builder.getDocument().save(dir + "document2.docx");
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void getAppendedHeaderFooterWatermark(DocumentBuilder builder,
String headerFooterWatermarkDocumentFilePath) throws Exception {
Document document = builder.getDocument();
Document headerFooterWatermarkDocument = new Document(headerFooterWatermarkDocumentFilePath);
NodeImporter importer = new NodeImporter(headerFooterWatermarkDocument, document,
ImportFormatMode.KEEP_SOURCE_FORMATTING);
SectionCollection originalDocSections = headerFooterWatermarkDocument.getSections();
Section originalDocSection = null;
for (int sectionIndex = 0; sectionIndex < originalDocSections.getCount(); sectionIndex++)
{
originalDocSection = originalDocSections.get(sectionIndex);
Section newSection = null;
if (document.getSections().get(sectionIndex) == null)
{
builder.moveToDocumentEnd();
int sectionStart = originalDocSection.getPageSetup().getSectionStart();
switch (sectionStart) {
case SectionStart.CONTINUOUS:
builder.insertBreak(BreakType.SECTION_BREAK_CONTINUOUS);
break;
case SectionStart.EVEN_PAGE:
builder.insertBreak(BreakType.SECTION_BREAK_EVEN_PAGE);
break;
case SectionStart.NEW_COLUMN:
builder.insertBreak(BreakType.SECTION_BREAK_NEW_COLUMN);
break;
case SectionStart.NEW_PAGE:
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
break;
case SectionStart.ODD_PAGE:
builder.insertBreak(BreakType.SECTION_BREAK_ODD_PAGE);
break;
}
newSection = builder.getCurrentSection();
}
else
{
newSection = document.getSections().get(sectionIndex);
}
newSection.getHeadersFooters().clear();
HeaderFooterCollection headersFooters = originalDocSection.getHeadersFooters();
HeaderFooter headerFooter = null;
// //import headers and footers
newSection.getPageSetup().setDifferentFirstPageHeaderFooter(
originalDocSection.getPageSetup().getDifferentFirstPageHeaderFooter());
newSection.getPageSetup().setOddAndEvenPagesHeaderFooter(
originalDocSection.getPageSetup().getOddAndEvenPagesHeaderFooter());
for (int headersFootersIndex = 0; headersFootersIndex < headersFooters.getCount(); headersFootersIndex++)
{
headerFooter = headersFooters.get(headersFootersIndex);
newSection.getHeadersFooters().add(importer.importNode(headerFooter, true));
}
}
//document.save(dir + "document.doc");
}