Hi,
Response.ContentType = "application/word"; Response.AppendHeader("Content-Disposition", string.Format("attachment; filename={0}_Final_Entry.doc", "contractoc"));
// here I need to add header and footer for word.. Anyone help me
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Ayin Rivera30 reputationPosted Jan 8, 2014, 9:55 PM EST
Raja A28 reputationPosted Jan 8, 2014, 10:07 PM EST
Jaganathan BantheswaranRank #170 · 14.1K reputationPosted Jan 8, 2014, 11:53 PM EST
You are trying to save the HTTP response as doc file. So whatever there in the response [document] will be saved as doc. If you want to add header or footer, you have to add it on the document/html page.
Raja A28 reputationPosted Jan 9, 2014, 7:58 PM EST
Jaganathan BantheswaranRank #170 · 14.1K reputationPosted Jan 9, 2014, 9:22 PM EST
within body,
This is the Title of the Content
Some text
Lorine Appleby38 reputationPosted Dec 19, 2014, 12:26 AM EST
Ok so if I understood you correctly you want to download your Word document but before you stream it to a client you want to add some custom header and footer.
Well the first question is where that document of yours is, does that "contractoc_Final_Entry.doc" file even exist?
Second I do not know why 'Jaganathan' thinks you want to save your response as a DOC file, the code that you provided does not indicate that, actually it does not provide much information at all. The only thing it shows is that you are defining the MIME type of the current response, so I can only presume that you want to execute a downloading of a word file.
But if 'Jaganathan' is right and you are actually trying to save your page as a word document then I'm afraid you cannot add header and footer to it. You see there is a common workaround in which developers would use a HTML content and save it to a file that has a word document extension. The trick is that most of the Word applications will be able to open this file, but it is not actually a word document at all. So I'm guessing that you are maybe using that workaround and if so note that you actually have a HTML file (with a DOC extension) and these files do not have a page header and page footer concepts.
So what you need to do is to convert the HTML content into a Word file in C# and then you will be able to add your header and footer:
var document = DocumentModel.Load(htmlStreamContent, LoadOptions.HtmlDefault); var section = document.Sections.First(); section.HeadersFooters[HeaderFooterType.HeaderDefault].Blocks.Add( new Paragraph(document, "Custom Header Content!")); section.HeadersFooters[HeaderFooterType.FooterDefault].Blocks.Add( new Paragraph(document, "Custom Footer Content!"));I used this C# component for manipulating with Word files because you also have an easy way how you can export a Word document in ASP.NET, with a single line so you can remove the line that you posted and just use this:
Regards