Hello,
In a document (docA) i have Header/Footer section, which can contain text and images and stored in DB. This document is only one in DB.
In my application i have many word documents with merge fields and stored in DB.
Based on requirement in pick a document from DB replace proper data for the merge fields and insert header/footer from docA.
i am through with inserting merge field data but am stuck with inserting the header/footer from docA into this merged document.
Could you please give me an example. I tried many things but none of them worked.
I have attched the souce code (.java as .txt) i tried and the document with sample header/footer.
If i create a new document (new Document()) and pass it to builder, works fine.
but if an existing document is passed to the builder it fails to merge header/footer.
Regards
Kiran
Hi
Thanks for your inquiry. Please see the following thread
I think that it could be useful for you.
Also I tested your code and it works fine, except one thing. Use the following code to remove existing Headers/Footers.
// Remove Headers/Footers from new section
newSection.getHeadersFooters().clear();
Hope this helps.
Best regards.
Hi,
thanks a ton for your reply.
Did you try inserting header/footer to any existing document of yours or any document which is already created.
Document existingDoc = new Document(); //with this it works.
Does it work uncommenting line 28 in my code
// Document existingDoc = new Document("C:/tmp/Test.doc");
Becaude for me it is failing.It does not insert.
I have attached 2 blank document files Input1.doc and Input2.doc
In my code for the line,
Document existingDoc = new Document("C:/tmp/Input1.doc");
header/footer insertion
works,
but for the line Document existingDoc = new Document("C:/tmp/Input2.doc");
does not.
what is the difference in these 2 blank word documents. Please help. I am confused.
Regards
Kiran
Hi
Thanks for your inquiry. I tried both. Your code fails if using input document with Headers/Footers. But if use “newSection.getHeadersFooters().clear()” to remove existing headers/footers all works fine on my side. Could you also attach your document for testing? I will check it and provide you more information.
Best regards.
Hi again.
I checked your document and both document works fine on my side. Most probably you are using old version of Aspose.Words. I use the latest version for testing. You can download it from here:
https://releases.aspose.com/words/net
Hope this helps.
Best regards.
Hi,
we have licensed version of aspose.words and it was taken just few days back.
Anyway i tried downloading the latest version of jar and tried and it is the same, not inserting. Am executing the same code and using same files Input1.doc and Input2.doc, for Input1.doc it works but for Input2.doc it fails. Am little confused.
Hi
Thank you for additional information. It is strange. Here is code I use for testing.
// source document which contains header and footer.
Document originalDoc = new Document("HeaderFooter.doc");
// Create new document for the input template which has to be branded.
Document existingDoc = new Document("Input2.doc");
// Final output file.
String targetFileName = "output.doc";
DocumentBuilder builder = new DocumentBuilder(existingDoc);
builder.getFont().setSize(16);
builder.getFont().setBold(true);
builder.getFont().setColor(Color.BLUE);
builder.getFont().setName("Arial");
builder.setUnderline(Underline.DASH);
builder.write("Sample text.");
NodeImporter importer = new NodeImporter(originalDoc, existingDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
// int sectionIndex = 0;
Sections originalDocSections = originalDoc.getSections();
Section originalDocSection = null;
for (int sectionIndex = 0; sectionIndex < originalDocSections.getCount(); sectionIndex++)
{
originalDocSection = originalDocSections.get(sectionIndex);
// create section in the new document
Section newSection = null;
if (existingDoc.getSections().get(sectionIndex) == null)
{
// Import SectionStart
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 = existingDoc.getSections().get(sectionIndex);
}
// Remove Headers/Footers from new section
newSection.getHeadersFooters().clear();
HeadersFooters headersFooters = originalDocSection.getHeadersFooters();
HeaderFooter headerFooter = null;
////import headers and footers
for (int headersFootersIndex = 0; headersFootersIndex < headersFooters.getCount(); headersFootersIndex++)
{
headerFooter = headersFooters.get(headersFootersIndex);
newSection.getHeadersFooters().add(importer.importNode(headerFooter, true));
}
newSection.getPageSetup().setDifferentFirstPageHeaderFooter(originalDocSection.getPageSetup().getDifferentFirstPageHeaderFooter());
}
existingDoc.save(targetFileName, SaveFormat.DOC);
System.out.println("HeaderFooter Insertion Done...");
As you can see I changed only one line in your code (I highlighted it). Also I attached the output document (input is “input2.doc”). But note that option “Show white space” is disabled in your document. You should enable it to see Headers/Footers. (On the Tools menu, click Options. Click the View tab and then click to select or clear the White space between pages check box.).
Best regards.
Hi,
Thanx for your reply.
Ya i found the same thing.
For me it was inserting the header/footer for Input2.doc also, when i double clicked the header section in output.doc i could clearly view it.
I also selected the option you suggested in View tab for Input2.doc and tried again inserting, now i could see the header/footer clearly in the output.doc.
But in my application we already have thousands of documents in DB for which i need to insert these header/footer when required, but i can’t extract all documents, open it and select that option in View tab. How do i do that programatically when i am inserting the header/footer for that document.
Regards
Kiran
Hi
Thanks for your inquiry. There is option DoNotDisplayPageBoundaries.
existingDoc.getViewOptions().setDoNotDisplayPageBoundaries(false);
But it seems that it does not work. I created new issue #5815 in our defect database. This issue will be fixed in .NET mainstream and then ported to java version. We will notify you as soon as it is fixed.
Best regards.
Hi,
Thank you very much for your support
and please let us know once it is fixed.
Regards
Kiran