Could I please know is there a way to add header and footer to a template word document using separate header.docx and footer.docx?
@HASUADDOVATION Sure, you can copy headers/footers from one document to another. You should simply import the appropriate headers/footers node. For example see the following code:
// The document that contains Header definitions
Document header = new Document("C:\\Temp\\header.docx");
// The document that contains footer definitions
Document footer = new Document("C:\\Temp\\footer.docx");
// The target document
Document mainBody = new Document("C:\\Temp\\dst.docx");
foreach (Section s in mainBody.Sections)
{
CopyHeaderFooter(s, header.FirstSection, HeaderFooterType.HeaderPrimary);
CopyHeaderFooter(s, footer.FirstSection, HeaderFooterType.FooterPrimary);
}
mainBody.Save("C:\\Temp\\out.docx");
private static void CopyHeaderFooter(Section dstSection, Section srcSection, HeaderFooterType hfType)
{
// Specify options to display different header/footer for first and odd/even pages.
if (hfType == HeaderFooterType.HeaderFirst || hfType == HeaderFooterType.FooterFirst)
dstSection.PageSetup.DifferentFirstPageHeaderFooter = srcSection.PageSetup.DifferentFirstPageHeaderFooter;
if (hfType == HeaderFooterType.HeaderEven || hfType == HeaderFooterType.HeaderEven)
dstSection.PageSetup.OddAndEvenPagesHeaderFooter = srcSection.PageSetup.OddAndEvenPagesHeaderFooter;
// Get header/footer of the specified type from the source section.
HeaderFooter srchf = srcSection.HeadersFooters[hfType];
// Nothing to copy.
if (srchf == null)
return;
// Get header/footer of the specified type from the destination section.
HeaderFooter dsthf = dstSection.HeadersFooters[hfType];
// Remove existing header/footer
if (dsthf != null)
dsthf.Remove();
NodeImporter importer = new NodeImporter(srcSection.Document, dstSection.Document, ImportFormatMode.KeepSourceFormatting);
// Copy whole header/footer into the destination document.
dstSection.HeadersFooters.Add(importer.ImportNode(srchf, true));
}
The following articles might be useful for you:
https://docs.aspose.com/words/net/aspose-words-document-object-model/
https://docs.aspose.com/words/net/working-with-headers-and-footers/
Thank you for the solution. We tried that seems header and footer has disappears.
Results.zip (6.9 MB)
@HASUADDOVATION There are no header/footer defined in the AnticimexDoc2Header.docx
and AnticimexFooter.docx
documents. The documents contains a simple floating content in the main document body.
Is there any method to add this document body as a header in another document.
@Duminda You can import and copy the first paragraph with shape into the destination document. Please see the following article for more information:
https://docs.aspose.com/words/net/insert-and-append-documents/#import-and-insert-nodes-manually
Document header = new Document("C:\\Temp\\AnticimexDoc2Header.docx");
Document footer = new Document("C:\\Temp\\AnticimexFooter.docx");
Document mainBody = new Document("C:\\Temp\\Main Body.docx");
// Copy content of the header document's main body into the promary header of the mainBody document
// and footer document's main content into footer.
foreach (Section s in mainBody.Sections)
{
CopyContentHeaderFooter(s, header, HeaderFooterType.HeaderPrimary);
CopyContentHeaderFooter(s, footer, HeaderFooterType.FooterPrimary);
}
mainBody.Save("C:\\Temp\\out.docx");
private static void CopyContentHeaderFooter(Section dstSection, Document src, HeaderFooterType hfType)
{
// Get header/footer of the specified type from the destination section.
HeaderFooter dsthf = dstSection.HeadersFooters[hfType];
// Remove existing header/footer
if (dsthf != null)
dsthf.Remove();
DocumentBuilder builder = new DocumentBuilder((Document)dstSection.Document);
builder.MoveToSection(dstSection.Document.IndexOf(dstSection));
builder.MoveToHeaderFooter(hfType);
builder.InsertDocument(src, ImportFormatMode.KeepSourceFormatting);
}
But you should note, your documents are fully created using floating content. Such document are extremally difficult to edit and handle.
@alexey.noskov I tried the above solution and this and this was the output, the footer is not complete(there was an image it has disappeared here) .and also cant justify the content of the document between header and footer.
testAnticimexAspose Result2.docx (3.6 MB)
@HASUADDOVATION First of all you are using Aspose.Words in evaluation mode and evaluation watermark is added. If you would like to test Aspose.Words without evaluation version limitations, you can request a free 30-days temporary license.
Secondly, as I have mentioned your documents are fully build using absolutely positioned content (frames and shapes). That is why content is not reflowed when header/footer is added. This is expected behavior.
I used a license and generated a document but still the problem do exist
testAnticimexAsposeWithLicense.docx (3.5 MB)
@Duminda As I have mentioned your documents are fully build using absolutely positioned content. That is why content is not reflowed when header/footer is added. This is expected behavior. You will observe the same behavior if add header/footer in MS Word. It is not normally recommended to use absolutely positioned content to build MS Word documents is it is supposed to edit them.
Main document is converted using Aspose.HTML to Word and Header and Footer also So After conversion we are trying to attach header and footer to the main document. So, how can I achieve this if objects are absolutely positioned in converting the Html document?
@Duminda Could you please attach the source document here for our reference? If the source document is HTML, why don’t you use Aspose.Words to convert it to DOCX?
When I search for Html to Word conversion Aspose.Html Is the only thing appears. Is this Html to Word conversion possible only using Aspose.Words or is it there in Aspose.Words cloud APIs’ also. Here is the source document for your reference
AnticimexDoc.zip (2.5 KB)
@Duminda HTML format is supported in both on-premises and cloud Aspose.Words API. Code for conversion is pretty simple:
Document doc = new Document(@"C:\Temp\in.html");
doc.Save(@"C:\Temp\out.docx");
Please see our documentation to learn what formats are supported by Aspose.Words:
https://docs.aspose.com/words/net/supported-document-formats/
However, please note, Aspose.Words is designed to work with MS Word document at first and when import HTML documents in most cases mimics MS Word behavior, which might differ from browser behavior.