Add Footer only to last page of the word document using open XML sdk with C# -
i new open xml sdk 2.5 using c#. created doc table in , contents in table may go more 500 rows. document may have multiple pages. have added header , footer successfully. want need add footer last page in document. how this. please find attached sample code. hope question clear
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using documentformat.openxml; using documentformat.openxml.packaging; using documentformat.openxml.wordprocessing; namespace consoleapplication8 { class program { static void main(string[] args) { string filename = @'c:\temp\headerfooterdocument.docx'; createtable(filename); } public static void applyheader(wordprocessingdocument doc) { // main document part. maindocumentpart maindocpart = doc.maindocumentpart; headerpart headerpart1 = maindocpart.addnewpart<headerpart>('r97'); header header1 = new header(); paragraph paragraph1 = new paragraph() { }; run run1 = new run(); text text1 = new text(); text1.text = 'header stuff'; run1.append(text1); text text3 = new text(); text3.text = '\rheader stuff'; run1.append(text3); paragraph1.append(run1); header1.append(paragraph1); headerpart1.header = header1; sectionproperties sectionproperties1 = maindocpart.document.body.descendants<sectionproperties>().firstordefault(); if (sectionproperties1 == null) { sectionproperties1 = new sectionproperties() { }; maindocpart.document.body.append(sectionproperties1); } headerreference headerreference1 = new headerreference() { type = headerfootervalues.default, id = 'r97' }; sectionproperties1.insertat(headerreference1, 0); } public static void applyfooter(wordprocessingdocument doc) { // main document part. maindocumentpart maindocpart = doc.maindocumentpart; footerpart footerpart1 = maindocpart.addnewpart<footerpart>('r98'); footer footer1 = new footer(); paragraph paragraph1 = new paragraph() { }; run run1 = new run(); text text1 = new text(); text1.text = 'footer stuff'; run1.append(text1); paragraph1.append(run1); footer1.append(paragraph1); footerpart1.footer = footer1; sectionproperties sectionproperties1 = maindocpart.document.body.descendants<sectionproperties>().firstordefault(); if (sectionproperties1 == null) { sectionproperties1 = new sectionproperties() { }; maindocpart.document.body.append(sectionproperties1); } footerreference footerreference1 = new footerreference() { type = documentformat.openxml.wordprocessing.headerfootervalues.default, id = 'r98' }; sectionproperties1.insertat(footerreference1, 0); } // insert table word processing document. public static void createtable(string filename) { // use file name , path passed in argument // open existing word 2007 document. using (wordprocessingdocument worddocument = wordprocessingdocument.create(filename, wordprocessingdocumenttype.document)) { // add main document part. maindocumentpart mainpart = worddocument.addmaindocumentpart(); // create document structure , add text. mainpart.document = new document(); body body = mainpart.document.appendchild(new body()); paragraph para = body.appendchild(new paragraph()); run run = para.appendchild(new run()); run.appendchild(new text('create text in body - createwordprocessingdocument')); applyheader(worddocument); applyfooter(worddocument); // create empty table. table table = new table(); // create tableproperties object , specify border information. tableproperties tblprop = new tableproperties( new tableborders( new topborder() { val = new enumvalue<bordervalues>(bordervalues.none), size = 24 }, new bottomborder() { val = new enumvalue<bordervalues>(bordervalues.none), size = 24 }, new leftborder() { val = new enumvalue<bordervalues>(bordervalues.none), size = 24 }, new rightborder() { val = new enumvalue<bordervalues>(bordervalues.none), size = 24 }, new insidehorizontalborder() { val = new enumvalue<bordervalues>(bordervalues.none), size = 24 }, new insideverticalborder() { val = new enumvalue<bordervalues>(bordervalues.none), size = 24 } ) ); // append tableproperties object empty table. table.appendchild<tableproperties>(tblprop); // create row. tablerow tr = new tablerow(); // create cell. tablecell tc1 = new tablecell(); // specify width property of table cell. tc1.append(new tablecellproperties( new tablecellwidth() { type = tablewidthunitvalues.dxa, width = '2400' })); // specify table cell content. tc1.append(new paragraph(new run(new text('some text')))); // append table cell table row. tr.append(tc1); // create second table cell copying outerxml value of first table cell. tablecell tc2 = new tablecell(tc1.outerxml); // append table cell table row. tr.append(tc2); // append table row table. table.append(tr); (int rows = 1; rows < 50; rows++) { tablerow tr2 = new tablerow(); (int index2 = 0; index2 < 2; index2++) { tablecell newcells = new tablecell(); newcells.append(new paragraph(new run(new text('row' + rows + 'column' + index2)))); tr2.append(newcells); } table.append(tr2); // append table document. } worddocument.maindocumentpart.document.body.append(table); } } } }
headers , footers linked sections in word. headers , footers can vary according section. , types of header , footer are: first page, odd pages (default) , pages when corresponding properties activated. default, there's odd page type , "cascade" throughout document if there no other instruction.
there no option "last page" header or footer. can faked using set of nested fields code tests whether page number of current page equivalent total number of pages in document. if is, header or footer displayed, otherwise nothing. field code looks this:
{ if { page } = {numpagess } "text appear" "" }
note field brackets { } must inserted using ctrl+f9, cannot typed keyboard other way. use alt+f9 toggle between field code , field result displays.
create small sample document field code, view in open xml sdk see xml generates , code used create it.
Comments
Post a Comment