How to create Header and footer with VB.net in Open XML Word document
Home › Forums › Open-Xml-Sdk › How to create Header and footer with VB.net in Open XML Word document
This topic contains 12 replies, has 3 voices, and was last updated by rajni gupta 8 years, 8 months ago.
-
AuthorPosts
-
March 21, 2016 at 10:49 am #3219
Actually I want to generate a word document with the help of Open XML in VB.net, I need to integrate Header and footer in this doc.I tried with SDK 2.0 but I am not able to add header and footer in the code.
If any one can help, I will be very thankful.March 21, 2016 at 1:23 pm #3220Have you watched the screen-casts in the following series:
In particular, you will be interested in #8 on Sections, Headers, and Footers.
March 22, 2016 at 7:26 am #3231Hi I saw the code about which you was saying but not helpful.I integrated the following code.
can you help me.Private Sub CommonK()
Try
docFileName = String.Format(“Report_{0}”, “2014-2015”)
docFilePath = Server.MapPath(Path.Combine(WebConfigurationManager.AppSettings(“ReportOutputPath”), docFileName))
wordDoc = OpenXMLHelper.CreateWordProcessingDocument(docFilePath)
mainDocPart = OpenXMLHelper.CreateMainDocumentPart(wordDoc)
‘create the document
document = OpenXMLHelper.CreateDocument(mainDocPart)
‘create the body
body1 = OpenXMLHelper.CreateBody(document)
OpenXMLHelper.CreateBodyProperties(body1, 800, 1440, 1000, 720, 720, 720, 0)
‘===================PREPARE & ADD STYLES INIT=======================
‘create a style definition part.
Dim styleDefPart As StyleDefinitionsPart = OpenXMLHelper.CreateStyleDefinitionPart(mainDocPart)
‘init the styles
Dim styles As Styles = OpenXMLHelper.InitStyles(styleDefPart)
OpenXMLHelper.CreateStyle(styles, “PageNormalStyle”, “Arial”, 5, False, False, “Black”)
OpenXMLHelper.CreateStyle(styles, “AmtItalicStyle”, “Arial Unicode MS”, 14, False, False, “Black”)p = New Paragraph()
p = OpenXMLHelper.CreateParagraphWithNewLine(MyEncoder.HtmlEncode(Session(“DemandName”)), JustificationValues.Center)
body1.Append(p)p1 = New Paragraph()
r = New Run(New Text(“Ministry Name”))
p_prp = OpenXMLHelper.CreateParagraphProperties(p1, “BoldStyle”, JustificationValues.Center, 0)
p1.Append(r)
body1.Append(p1)p = OpenXMLHelper.CreateParagraphWithNewLine(MyEncoder.HtmlEncode(“A. The Budget allocations, net of recoveries, are given below: “), JustificationValues.Left)
body1.Append(p)p = OpenXMLHelper.CreateParagraphWithNewLine(MyEncoder.HtmlEncode(” “), JustificationValues.Left)
body1.Append(p)
‘From Here‘Put Data here
‘Dim footnotes1 As New Footnotes()
‘footnotes1.MCAttributes = New MarkupCompatibilityAttributes()
” footnotes1.Ignorable = “w14 wp14”‘Dim footnote1 As New Footnote()
‘footnote1.Type = FootnoteEndnoteValues.Separator
‘footnote1.Id = -1Dim paragraph1 As New Paragraph()
paragraph1.RsidParagraphAddition = “004F1932”
paragraph1.RsidParagraphProperties = “00F6695D”
paragraph1.RsidRunAdditionDefault = “00A242E6”Dim run1 As New Run()
Dim text1 As New Text()
text1.Space = SpaceProcessingModeValues.Preserve
text1.Text = “National”
run1.Append(text1)
paragraph1.Append(run1)
‘footnote1.Append(paragraph1)
‘footnotes1.Append(footnote1)Dim paragraph2 As New Paragraph()
paragraph2.RsidParagraphAddition = “00A242E6”
paragraph2.RsidParagraphProperties = “00F6695D”
paragraph2.RsidRunAdditionDefault = “00A242E6”Dim paragraph3 As New Paragraph()
paragraph3.RsidParagraphAddition = “00A242E6”
paragraph3.RsidParagraphProperties = “00F6695D”
paragraph3.RsidRunAdditionDefault = “00A242E6”Dim paragraph4 As New Paragraph()
paragraph4.RsidParagraphAddition = “00A242E6”
paragraph4.RsidParagraphProperties = “00F6695D”
paragraph4.RsidRunAdditionDefault = “00A242E6”Dim paragraph5 As New Paragraph()
paragraph5.RsidParagraphAddition = “00A242E6”
paragraph5.RsidParagraphProperties = “00F6695D”
paragraph5.RsidRunAdditionDefault = “00A242E6”Dim paragraph6 As New Paragraph()
paragraph6.RsidParagraphAddition = “00A242E6”
paragraph6.RsidParagraphProperties = “00F6695D”
paragraph6.RsidRunAdditionDefault = “00A242E6”Dim paragraph7 As New Paragraph()
paragraph7.RsidParagraphAddition = “00A242E6”
paragraph7.RsidParagraphProperties = “00F6695D”
paragraph7.RsidRunAdditionDefault = “00A242E6”Dim paragraph8 As New Paragraph()
paragraph8.RsidParagraphAddition = “00A242E6”
paragraph8.RsidParagraphProperties = “00F6695D”
paragraph8.RsidRunAdditionDefault = “00A242E6”Dim paragraph9 As New Paragraph()
paragraph9.RsidParagraphAddition = “00A242E6”
paragraph9.RsidParagraphProperties = “00F6695D”
paragraph9.RsidRunAdditionDefault = “00A242E6”Dim paragraph10 As New Paragraph()
paragraph10.RsidParagraphAddition = “00A242E6”
paragraph10.RsidParagraphProperties = “00F6695D”
paragraph10.RsidRunAdditionDefault = “00A242E6”Dim paragraph11 As New Paragraph()
paragraph11.RsidParagraphAddition = “00A242E6”
paragraph11.RsidRunAdditionDefault = “00A242E6”Dim run2 As New Run()
Dim break1 As New Break()
break1.Type = BreakValues.Pagerun2.Append(break1)
paragraph11.Append(run2)
Dim paragraph12 As New Paragraph()
paragraph12.RsidParagraphAddition = “00A242E6”
paragraph12.RsidParagraphProperties = “00F6695D”
paragraph12.RsidRunAdditionDefault = “00A242E6”Dim paragraph13 As New Paragraph()
paragraph13.RsidParagraphMarkRevision = “00A242E6”
paragraph13.RsidParagraphAddition = “00A242E6”
paragraph13.RsidParagraphProperties = “00A242E6”
paragraph13.RsidRunAdditionDefault = “00A242E6”Dim paragraph14 As New Paragraph()
paragraph14.RsidParagraphAddition = “00A242E6”
paragraph14.RsidParagraphProperties = “00A242E6”
paragraph14.RsidRunAdditionDefault = “00A242E6”Dim paragraph15 As New Paragraph()
paragraph15.RsidParagraphMarkRevision = “00A242E6”
paragraph15.RsidParagraphAddition = “00A242E6”
paragraph15.RsidParagraphProperties = “00A242E6”
paragraph15.RsidRunAdditionDefault = “00A242E6”Dim runrr As New Run()
Dim text2 As New Text()
text2.Space = SpaceProcessingModeValues.Preserve
text2.Text = “NIc”
runrr.Append(text2)Dim paragraphProperties1 As New ParagraphProperties()
Dim tabs1 As New Tabs()
Dim tabStop1 As New TabStop()
tabStop1.Val = TabStopValues.Left
tabStop1.Position = 6371
tabs1.Append(tabStop1)paragraphProperties1.Append(tabs1)
Dim run3 As New Run()
Dim tabChar1 As New TabChar()run3.Append(tabChar1)
Dim bookmarkStart1 As New BookmarkStart()
bookmarkStart1.Name = “_GoBack”
bookmarkStart1.Id = “0”Dim bookmarkEnd1 As New BookmarkEnd()
bookmarkEnd1.Id = “0”paragraph15.Append(runrr)
paragraph15.Append(paragraphProperties1)
paragraph15.Append(run3)
paragraph15.Append(bookmarkStart1)
paragraph15.Append(bookmarkEnd1)Dim sectionProperties1 As New SectionProperties()
sectionProperties1.RsidRPr = “00A242E6”
sectionProperties1.RsidR = “00A242E6”Dim footerReference1 As New FooterReference()
footerReference1.Type = HeaderFooterValues.[Default]
footerReference1.Id = “rId7”Dim pageSize1 As New PageSize()
pageSize1.Width = 12240UI
pageSize1.Height = 15840UIDim pageMargin1 As New PageMargin()
pageMargin1.Top = 1440
pageMargin1.Right = 1440UI
pageMargin1.Bottom = 1440
pageMargin1.Left = 1440UI
pageMargin1.Header = 720UI
pageMargin1.Footer = 720UI
pageMargin1.Gutter = 0UIDim columns1 As New Columns()
columns1.Space = “720”Dim docGrid1 As New DocGrid()
docGrid1.LinePitch = 360sectionProperties1.Append(footerReference1)
sectionProperties1.Append(pageSize1)
sectionProperties1.Append(pageMargin1)
sectionProperties1.Append(columns1)
sectionProperties1.Append(docGrid1)‘ body1.Append(footnote1)
body1.Append(paragraph2)
body1.Append(paragraph3)
body1.Append(paragraph4)
body1.Append(paragraph5)
body1.Append(paragraph6)
body1.Append(paragraph7)
body1.Append(paragraph8)
body1.Append(paragraph9)
body1.Append(paragraph10)
body1.Append(paragraph11)
body1.Append(paragraph12)
body1.Append(paragraph13)
body1.Append(paragraph14)
body1.Append(paragraph15)
‘ body1.Append(footnotes1)
body1.Append(sectionProperties1)Catch ex As Exception
Throw ex
End Try‘End Here
‘Dim sectionProperties1 As New SectionProperties()
‘sectionProperties1.RsidRPr = “0075600D”
‘sectionProperties1.RsidR = “006217B2″‘Dim pageSize1 As New PageSize()
”runFonts32.Width = DirectCast(12240UI, UInt32Value)
”runFonts32.Height = DirectCast(15840UI, UInt32Value)‘Dim pageMargin1 As New PageMargin()
‘pageMargin1.Top = 1440
”pageMargin1.Right = DirectCast(1440UI, UInt32Value)
‘pageMargin1.Bottom = 1440
‘pageMargin1.Left = 1440UI
‘pageMargin1.Header = 720UI
‘pageMargin1.Footer = 720UI
‘pageMargin1.Gutter = 0UI‘Dim columns1 As New Columns()
‘columns1.Space = “720”‘Dim docGrid1 As New DocGrid()
‘docGrid1.LinePitch = 360‘sectionProperties1.Append(pageSize1)
‘sectionProperties1.Append(pageMargin1)
‘sectionProperties1.Append(columns1)
‘sectionProperties1.Append(docGrid1)‘body1.Append(sectionProperties1)
‘Return body1document.Save()
wordDoc.Close()HttpContext.Current.Response.ClearContent()
Response.ContentType = “application/vnd.ms-word”
Response.AddHeader(“Content-Disposition”, String.Format(“inline; filename={0}”, docFileName + “.docx”))
HttpContext.Current.Response.WriteFile(MyEncoder.HtmlEncode(docFilePath))
HttpContext.Current.Response.End()End Sub
March 22, 2016 at 11:44 am #3233Unfortunately, I have schedules and such that prohibit debugging the specific code. Have you watched all of the screen-casts in that series? Also have you watched the intro to Open XML screen-cast series:
There are screen-casts in that series that explain the specifics of relationships, relationship types, content types, explicit relationships, and implicit relationships. You will need to understand this in order to create your program.
If you have specific questions about the markup, or directions to go, happy to hear and give my opinion.
March 23, 2016 at 7:23 am #3251Hi Eric,
Thanks for your reply but I checked all the links and I checked my XML also.The XML is as following:
I am not getting any help from anywhere.
So please help me.<w:body xmlns:w=”http://schemas.openxmlformats.org/wordprocessingml/2006/main”>
<w:p w:rsidr=”001F0702″ w:rsidp=”00500849″ w:rsidrdefault=”001F0702″>
<w:ppr>
<w:tabs>
<w:tab w:val=”center” w:pos=”1080″ />
</w:tabs>
<w:ind w:left=”1170″ w:right=”360″ />
</w:ppr>
</w:p>
<w:p w:rsidr=”001F0702″ w:rsidrdefault=”001F0702″>
<w:r>
<w:br w:type=”page” />
</w:r>
<w:bookmarkstart w:name=”_GoBack” w:id=”0″ />
<w:bookmarkend w:id=”0″ />
</w:p>
<w:p w:rsidr=”004F1932″ w:rsidp=”00F6695D” w:rsidrdefault=”00A242E6″>
<w:r>
<w:lastrenderedpagebreak />
<w:t xml:space=”preserve”>
</w:t>
</w:r>
</w:p>
<w:p w:rsidr=”00A242E6″ w:rsidp=”00F6695D” w:rsidrdefault=”00A242E6″ />
<w:p w:rsidr=”00A242E6″ w:rsidp=”00F6695D” w:rsidrdefault=”00A242E6″ />
<w:p w:rsidr=”00A242E6″ w:rsidp=”00F6695D” w:rsidrdefault=”00A242E6″ />
<w:p w:rsidr=”00A242E6″ w:rsidp=”00F6695D” w:rsidrdefault=”00A242E6″ />
<w:p w:rsidr=”00A242E6″ w:rsidp=”00F6695D” w:rsidrdefault=”00A242E6″ />
<w:p w:rsidr=”00A242E6″ w:rsidp=”00F6695D” w:rsidrdefault=”00A242E6″ />
<w:p w:rsidr=”00A242E6″ w:rsidp=”00F6695D” w:rsidrdefault=”00A242E6″ />
<w:p w:rsidr=”00A242E6″ w:rsidp=”00F6695D” w:rsidrdefault=”00A242E6″ />
<w:p w:rsidr=”00A242E6″ w:rsidp=”00F6695D” w:rsidrdefault=”00A242E6″ />
<w:p w:rsidr=”00A242E6″ w:rsidrdefault=”00A242E6″>
<w:r>
<w:br w:type=”page” />
</w:r>
</w:p>
<w:p w:rsidr=”00A242E6″ w:rsidp=”00F6695D” w:rsidrdefault=”00A242E6″ />
<w:p w:rsidrpr=”00A242E6″ w:rsidr=”00A242E6″ w:rsidp=”00A242E6″ w:rsidrdefault=”00A242E6″ />
<w:p w:rsidr=”00A242E6″ w:rsidp=”00A242E6″ w:rsidrdefault=”00A242E6″ />
<w:p w:rsidrpr=”00A242E6″ w:rsidr=”00A242E6″ w:rsidp=”00A242E6″ w:rsidrdefault=”00A242E6″>
<w:ppr>
<w:tabs>
<w:tab w:val=”left” w:pos=”6371″ />
</w:tabs>
</w:ppr>
<w:r>
<w:tab />
</w:r>
</w:p>
<w:sectpr w:rsidrpr=”00A242E6″ w:rsidr=”00A242E6″ w:rsidsect=”00500849″>
<w:headerreference w:type=”default” r:id=”rId8″ xmlns:r=”http://schemas.openxmlformats.org/officeDocument/2006/relationships” />
<w:footerreference w:type=”default” r:id=”rId9″ xmlns:r=”http://schemas.openxmlformats.org/officeDocument/2006/relationships” />
<w:pgsz w:w=”12240″ w:h=”15840″ />
<w:pgmar w:top=”1440″ w:right=”1440″ w:bottom=”1440″ w:left=”1530″ w:header=”720″
w:footer=”720″ w:gutter=”0″ />
<w:cols w:space=”720″ />
<w:docgrid w:linepitch=”360″ />
</w:sectpr>
</w:body>March 24, 2016 at 4:21 am #3263Hi,
Have you validated the XML through the validation functionality of the Open-Xml-Sdk?
https://msdn.microsoft.com/en-us/library/bb497334(v=office.12).aspx
This is the first step to finding out what is wrong.
BTW, it is helpful to me if you include very specific information, rather than just the XML. What exact error message or symptoms are you receiving? What specifically is not working.
Have you yet watched all of the screen-casts in that series that I referenced?
Cheers, Eric
March 24, 2016 at 5:46 am #3264Hi,
I validated my word document in sdk also but no error message is coming because it showing that there is no validation error.
See the problem is in my code is that the footer reference is not adding.
The code which I integrated is like this<w:sectPr w:rsidRPr=”00A242E6″ w:rsidR=”00A242E6″>
<w:pgSz w:w=”12240″ w:h=”15840″ />
<w:pgMar w:top=”1440″ w:right=”1440″ w:bottom=”1440″ w:left=”1440″ w:header=”720″ w:footer=”720″ w:gutter=”0″ />
<w:cols w:space=”720″ />
<w:docGrid w:linePitch=”360″ />
</w:sectPr>
but it should be like this<w:sectPr w:rsidR=”00DD14CB”>
<w:footerReference w:type=”default” r:id=”rId7″ xmlns:r=”http://schemas.openxmlformats.org/officeDocument/2006/relationships” />
<w:pgSz w:w=”12240″ w:h=”15840″ />
<w:pgMar w:top=”1440″ w:right=”1440″ w:bottom=”1440″ w:left=”1440″ w:header=”720″ w:footer=”720″ w:gutter=”0″ />
<w:cols w:space=”720″ />
<w:docGrid w:linePitch=”360″ />
</w:sectPr>But I am not getting what is missing in this code.I hope now you got my point, means what I want to say.
I saw all contents related to header and footer from your blog as well as from other links also.
If you will help, I will be very greatful.March 24, 2016 at 12:18 pm #3268Sure, happy to help, as much as possible.
I am not clear on what the actual problem is? When you say it is like this but it should be like that, what do you mean, exactly? What is the error, or misbehavior of your document that you are seeing?
March 28, 2016 at 7:26 am #3283Actually the code which I am using, is generating this footer Code
<w:sectPr w:rsidRPr=”00A242E6″ w:rsidR=”00A242E6″>
<w:pgSz w:w=”12240″ w:h=”15840″ />
<w:pgMar w:top=”1440″ w:right=”1440″ w:bottom=”1440″ w:left=”1440″ w:header=”720″ w:footer=”720″ w:gutter=”0″ />
<w:cols w:space=”720″ />
<w:docGrid w:linePitch=”360″ />
</w:sectPr>but in OpenXML SDK it is showing like this
<w:sectPr w:rsidR=”00DD14CB”>
<w:footerReference w:type=”default” r:id=”rId7″ xmlns:r=”http://schemas.openxmlformats.org/officeDocument/2006/relationships” />
<w:pgSz w:w=”12240″ w:h=”15840″ />
<w:pgMar w:top=”1440″ w:right=”1440″ w:bottom=”1440″ w:left=”1440″ w:header=”720″ w:footer=”720″ w:gutter=”0″ />
<w:cols w:space=”720″ />
<w:docGrid w:linePitch=”360″ />
</w:sectPr>So I am not getting how I can code this.
March 28, 2016 at 7:45 am #3285I think you are looking at two different w:sectPr elements. One of the sections has a header, the other does not.
Are you using the Visual Studio Package Editor PowerTool? It is an important tool.
I am wondering, have you watched the screen-casts here:
There are videos on various tools that can help you get your job done.
Cheers
March 28, 2016 at 9:47 am #3287NO you are not getting my point I am saying that the code which I am generating is not showing footer reference in section property.Yes I used Open XML SDK 2.5 for code generation.
Visual Studio Package Editor PowerTool is new for me.I have not used this tool previously and I am not aware about that.March 29, 2016 at 12:28 pm #3294I have never had the experience where my code was writing something, then I saw something different in the file itself. Not saying that it doesn’t happen – just that I have never seen it.
Have to note something here – I primarily use LINQ to XML to query and modify parts in an Open XML package, not the strongly typed OM of the Open XML SDK, which you are using. However, I have never experienced the symptoms you are seeing using the strongly-typed OM, not to say that it can’t happen. But I don’t think that the strongly-typed OM will add a header and a reference to a header in w:sectPr.
I would look elsewhere to find the source of the issue you are seeing.
March 30, 2016 at 4:58 am #3305Yes this is happening in my case.I am writting code in VB.net.So first I am opening word document in SDK 2.0.From SDK I am generating code.But when I am writing the same code in VB.net, footer is not showing.
You are using some other way for Open XML generation and I am using some other way.
Well you are a Master In Open XML and I am beginner.
May be I am not able to tell you my exact problem but anyways thanks a lot.I consumed your lot of times.
Thank you so much. -
AuthorPosts
You must be logged in to reply to this topic.