How to change the header and footer in the Section Breaks Next Page using OpenXML?

I have a word document file in which I added a Section Break of Next Page, now I want to change the header and footer of that page.
Scenario of example, I have a doc file which has four pages with headers and footers and added fifth page in the section break next page, I want to change the header and footer of the fifth page only. This is achievable manually by deselecting the Link to Previous
button in the Word Application but I don't know how to change it using XML?
My code that adds the new page in the section breaks is:
class Program
static void Main(string[] args)
string path = @"C:\Riyaz\sample.docx";
string strtxt = "Hello This is done by programmatically";
OpenAndAddTextToWordDocument(path,strtxt);
public static void OpenAndAddTextToWordDocument(string filepath, string txt)
using (DocX document = DocX.Load(@"C:\Riyaz\sample.docx"))
document.InsertSectionPageBreak();
Paragraph p1 = document.InsertParagraph();
p1.Append("This is new section");
document.Save();
Please help.

Here is the sample for your reference:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
namespace WordAddNewFooterHeader
class Program
static void Main(string[] args)
string path = @"E:\Document\TestHeaderandfooter-Copy.docx";
string strtxt = "OpenXML SDK";
OpenAndAddTextToWordDocument(path, strtxt);
public static void OpenAndAddTextToWordDocument(string filepath, string txt)
// Open a WordprocessingDocument for editing using the filepath.
WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(filepath, true);
MainDocumentPart part = wordprocessingDocument.MainDocumentPart;
Body body = part.Document.Body;
//create a new footer Id=rIdf2
FooterPart footerPart2 = part.AddNewPart<FooterPart>("rIdf2");
GenerateFooterPartContent(footerPart2);
//create a new header Id=rIdh2
HeaderPart headerPart2 = part.AddNewPart<HeaderPart>("rIdh2");
GenerateHeaderPartContent(headerPart2);
//replace the attribute of SectionProperties to add new footer and header
SectionProperties lxml = body.GetFirstChild<SectionProperties>();
lxml.GetFirstChild<HeaderReference>().Remove();
lxml.GetFirstChild<FooterReference>().Remove();
HeaderReference headerReference1 = new HeaderReference() { Type = HeaderFooterValues.Default, Id = "rIdh2" };
FooterReference footerReference1 = new FooterReference() { Type = HeaderFooterValues.Default, Id = "rIdf2" };
lxml.Append(headerReference1);
lxml.Append(footerReference1);
//add the correlation of last Paragraph
OpenXmlElement oxl = body.ChildElements.GetItem(body.ChildElements.Count - 2);
ParagraphProperties paragraphProperties1 = new ParagraphProperties();
SectionProperties sectionProperties1 = new SectionProperties() { RsidR = oxl.GetAttribute("rsidR", oxl.NamespaceUri).Value };
HeaderReference headerReference2 = new HeaderReference() { Type = HeaderFooterValues.Default, Id = part.GetIdOfPart(part.HeaderParts.FirstOrDefault()) };
FooterReference footerReference2 = new FooterReference() { Type = HeaderFooterValues.Default, Id = part.GetIdOfPart(part.FooterParts.FirstOrDefault()) };
PageSize pageSize1 = new PageSize() { Width = (UInt32Value)12240U, Height = (UInt32Value)15840U };
PageMargin pageMargin1 = new PageMargin() { Top = 1440, Right = (UInt32Value)1440U, Bottom = 1440, Left = (UInt32Value)1440U, Header = (UInt32Value)720U, Footer = (UInt32Value)720U, Gutter = (UInt32Value)0U };
Columns columns1 = new Columns() { Space = "720" };
DocGrid docGrid1 = new DocGrid() { LinePitch = 360 };
sectionProperties1.Append(headerReference2);
sectionProperties1.Append(footerReference2);
sectionProperties1.Append(pageSize1);
sectionProperties1.Append(pageMargin1);
sectionProperties1.Append(columns1);
sectionProperties1.Append(docGrid1);
paragraphProperties1.Append(sectionProperties1);
oxl.InsertAt<ParagraphProperties>(paragraphProperties1, 0);
body.InsertBefore<Paragraph>(GenerateParagraph(txt, oxl.GetAttribute("rsidRDefault", oxl.NamespaceUri).Value), body.GetFirstChild<SectionProperties>());
part.Document.Save();
wordprocessingDocument.Close();
//Generate new Paragraph
public static Paragraph GenerateParagraph(string text, string rsidR)
Paragraph paragraph1 = new Paragraph() { RsidParagraphAddition = rsidR };
ParagraphProperties paragraphProperties1 = new ParagraphProperties();
Tabs tabs1 = new Tabs();
TabStop tabStop1 = new TabStop() { Val = TabStopValues.Left, Position = 5583 };
tabs1.Append(tabStop1);
paragraphProperties1.Append(tabs1);
Run run1 = new Run();
Text text1 = new Text();
text1.Text = text;
run1.Append(text1);
Run run2 = new Run();
TabChar tabChar1 = new TabChar();
run2.Append(tabChar1);
paragraph1.Append(paragraphProperties1);
paragraph1.Append(run1);
paragraph1.Append(run2);
return paragraph1;
static void GenerateHeaderPartContent(HeaderPart hpart)
Header header1 = new Header();
Paragraph paragraph1 = new Paragraph();
ParagraphProperties paragraphProperties1 = new ParagraphProperties();
ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() { Val = "Header" };
paragraphProperties1.Append(paragraphStyleId1);
Run run1 = new Run();
Text text1 = new Text();
text1.Text = "";
run1.Append(text1);
paragraph1.Append(paragraphProperties1);
paragraph1.Append(run1);
header1.Append(paragraph1);
hpart.Header = header1;
static void GenerateFooterPartContent(FooterPart fpart)
Footer footer1 = new Footer();
Paragraph paragraph1 = new Paragraph();
ParagraphProperties paragraphProperties1 = new ParagraphProperties();
ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() { Val = "Footer" };
paragraphProperties1.Append(paragraphStyleId1);
Run run1 = new Run();
Text text1 = new Text();
text1.Text = "";
run1.Append(text1);
paragraph1.Append(paragraphProperties1);
paragraph1.Append(run1);
footer1.Append(paragraph1);
fpart.Footer = footer1;
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click
HERE to participate the survey.

Similar Messages

  • How can I remove the header and footer for more space in my Pages docs?

    I could really use the extra space on my docs. How can I get rid of the header and footer?
    Thanks

    Hi Timothy
    Just to detail exactly where to find the options:
    +Inspector > Document > Document > Document Margins > Headers/Footers+
    +commnd option i+ shows and hides your Inspector palette/s.
    Peter

  • I want to access the "Page Setu " menu item of the "File" menu of the Netscape using Applet or JavaScript so that i can remove the header and footer from the page that will be printed.

    A print command should be given from a button on the page. Hope someone helps me

    If you have secondary hard drives or other writable volumes connected to your Mac, each contains an invisible Trash folder named .Trashes at the root (top) level of the volume, which in turn contains an invisible Trash folder for each user.
    -from -> Solving Trash Problems.
    Clinton

  • Help: the header and footer in the dreamweaver are doubled

    http://coolsite04.businesscatalyst.com/
    this is my site how can i fix it.
    i'm using the basic template

    Issue resolved.
    had to change the Main Template.html to  {tag_pagecontent}

  • How do i add images in the header and footer to a PDF using iText

    Hi ,
    I want to add images to the header and footer of every page while i am genrating a pdf i have created a separate class called EndPage which i am instanceiating its default constructor in another class 's button action method.
    The above code genrates a PDF for me however it genrates a file with file size zero bytes and does not open it following is my sample code
    //**********Any Help would be appreciated
    Thank You
    public class My_Class
    public String pdf_action()
    EndPage ep=new EndPage();
    return null;
    }//My_class Ends
    class EndPage extends PdfPageEventHelper
    * Demonstrates the use of PageEvents.
    * @param args no arguments needed
    public EndPage()
    try {
    com.lowagie.text.Document document = new Document(PageSize.A4, 50, 50, 70, 70);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D://proposals/endpage.pdf"));
    writer.setPageEvent(new EndPage());
    document.open();
    String text = "Lots of text. ";
    for (int k = 0; k < 10; ++k)
    text += text;
    document.add(new Paragraph(text));
    document.close();
    catch (Exception de) {
    de.printStackTrace();
    public void onEndPage(PdfWriter writer, Document document) {
    try {
    Rectangle page = document.getPageSize();
    PdfPTable head = new PdfPTable(3);
    for (int k = 1; k <= 6; ++k)
    head.addCell("head " + k);
    head.setTotalWidth(page.width() - document.leftMargin() - document.rightMargin());
    head.writeSelectedRows(0, -1, document.leftMargin(), page.height() - document.topMargin() + head.getTotalHeight(),
    writer.getDirectContent());
    PdfPTable foot = new PdfPTable(3);
    for (int k = 1; k <= 6; ++k)
    foot.addCell("foot " + k);
    foot.setTotalWidth(page.width() - document.leftMargin() - document.rightMargin());
    foot.writeSelectedRows(0, -1, document.leftMargin(), document.bottomMargin(),
    writer.getDirectContent());
    catch (Exception e) {
    throw new ExceptionConverter(e);
    }

    Hi,
    Thanks for the quick response.
    The problem is that when I keep the logo as a watermark, the pdf is not adjusting itself to include the logo as header.
    But if I add a header text via Tools -> Headers and Footers, the pdf is adjusting itself so that the header text is at the beginning , not overlapping with the contents of pdf.
    But while using logo as watermark, some times overlapping of the pdf contents and logo is happening.
    Is there any way to add a logo in the Header and Footer via the option in Tools -> Headers and Footers
    Thanks,
    Vidhya

  • Avoid printing Header and Footer in the last page

    Hi,
    Could anyone please let me know how to avoid print the header and footer in the last page?
    Note: I'm printing RTF template for publishing the output.
    Looking forward for your valuable inputs/suggestions.
    Thanks in advance,
    Regards,
    Muru

    Hai,
    My report got FROM PO & TO PO parameters and i need to print footer only in first page of each PO. Tried with section but now i am getting first page of all PO contionious and then all lines together.
    Please call me or sent replies to [email protected]

  • Customizing header and footer in the printed documentation

    Hi,
    I'm using a trial version of RoboHelp 2007 and Word 2003.
    I have two question about customizing the header and footer
    of the printed documentation.
    In the printed documentation the footer repeat at the bottom
    of each page and contain page numbers, which appear left-justified
    on even-numbered pages and right-justified on odd-numbered pages.
    How can I customize the footer, that pages numbers appear
    always right-justified?
    In the printed documentation the header repeat at the top of
    each page and are blank on the first page of sections. The name of
    the manual appears on even-numbered pages, left-justified. The name
    of the root chapter appears on odd-numbered pages, right-justified.
    How can I customize the header, that
    on every page the name of the root chapter and the name oft
    the manual appears? Furhermore I want to include a picture in the
    header. How can I realise this?

    You can't from RH. See the article on my site.
    Images in the header are tricky. If you use the setting that
    I think enables that, you will also get the images in the TOC. Try
    putting a two cell table and putting the heading in one and the
    image in another.

  • I want to remove the header and footer when printing from my mac, i can do this in the Page Setup on my PC but not on the Mac. I can do it each time I print but then it resets. How do I make a universal change?

    I want to remove the header and footer when printing from my mac, I can do this in the Page Setup on my PC but not on the Mac. I can do it each time I print but then it resets. How do I make a universal change?
    == This happened ==
    Every time Firefox opened

    Go to the "File" menu, then "Print." The window that appears will have three drop-down menus at the top.
    The first is labeled "Printer",
    The second is "Presets".
    The third is unlabeled but has "Copies & Pages" selected by default.
    Click on that third menu and select "Firefox" near the bottom of the list.

  • I have just updated to Pages 5.1 and now the templares which I have saved do not displat the header and footer which they were saved with in the previous version.  How can I have the header and footer included when I open a template?

    I have just updated to Pages 5.1 and now the templares which I have saved do not displat the header and footer which they were saved with in the previous version.  How can I have the header and footer included when I open a template?

    That is becuase Pages 5.1 has numerous unnounced surprises for the unwary user.  Among the surprises:
    - it strips out images from headers and footers without warning
    - it strips out bookmarks without wanring (yes, all those bookamrks you laboriously put into a document: poof! Gone.)
    - it strips out alternating left/right margins, without warning.
    There are many more suprises, as you'll see from glancing through these discussions. They fall into two main categories: things that are present in version 4.3 but that are missing in 5.1, things like
    -mail merge
    -meaningful Applescript/Services support
    -non-contiguous text selection
    -ability to set defaul zoom
    -ability to see comments while editing
    -ability to print comments
    -ability to read RTF files
    -ability to drag in hyperlinked ted from a browser
    These about about 90 other features are gone.
    Then there are the bugs.  Hyperlinks do not reliably export to a PDF file any longer. There have been multiple problems with printing report. Some fonts place stray characters in the headers.  Many reports of the program crashing, courrupting files, etc.
    Fortunately for you, the previous version of Pages is still on your system.  Export any documents you may have created or edited with Pages 5.1 (another great surprise: files created with version 5.1 cannot be read by any previous version of Pages: not just 4.3 but even 5.01!).  Then trash 5.1 and use 4.3 until (unless?) it is stable and has a reasonably robust feature set.

  • Pages 5.2 How do I remove the fields in the header and footer?

    I need to add objects and footers that are longer than the field dividers allow in the header and footer of my document. How do I remove the field divisions?

    You can't remove the dividers in Pages 5.
    They don't exist in Pages '09.
    On the Mac you can simply keep typing and the text crosses the dividers.
    A quick test on my iPod touch suggests the same on iOS.
    Peter

  • How do you print from safari without the header and footer?

    I may be missing something but...
    When printing in Safari how do you remove the header and footer.  I am using PDF creator to save pages as PDF for things like online pay slips and want them to be 'clean'.
    Running Safari 5.1 on Windows Vista
    Thanks in advance for any help.
    Mark

    I may be missing something but...
    When printing in Safari how do you remove the header and footer.  I am using PDF creator to save pages as PDF for things like online pay slips and want them to be 'clean'.
    Running Safari 5.1 on Windows Vista
    Thanks in advance for any help.
    Mark

  • HT1349 how  do you remove the header and footer from a document

    how  do you remove the header and footer from a document

    It depends upon the app you are using.

  • Removing the header and footer in Pages

    I just began to use Pages after purchasing a new ibook. In AppleWorks, headers and footers were never inserted by default. In Pages, how can I remove the header and footer in Pages so I can have more space for the body section of the document?

    I had already set page margins to zero, and I disagree with you: Headers and footers still appear with a height of approximately 1/8 inch.
    SInce I posted my previous message, I have experimented a bit. I was able to (almost but not quite) totally eliminate the header and footer by clicking into the header and footer area and reducing the font size to the absolute minimum. After having done that, the body was basically in-line with 0" on the vertical page ruler.
    Also, in my opinion, whether or not most printers do a full page bleed is irrelevant. I should be able to work with the page body without its being offset from the page ruler if I don't want to employ a header or footer.
    Rusty

  • Need to remove the header and footer regions on one page

    Hi,
    I have a requirement to remove the header and footer regions on one page only. The regions I'm referring to contain the following:
    Header:
    Company logo, Home link, Logout link, Preferences link
    Footer:
    Copyright info, Privacy Statement, Home link, Logout link, Preferences link
    These are the standard Oracle header and footer regions/page items, so I am not specifying a particular page name, although it would be the create time cards page we want to modify.
    Also, we only want to remove these regions/items from ONE page, not an entire responsibility.
    Any help is greatly appreciated.
    Thanks!
    Suzanne

    Hi,
    In order to hide,
    Home link, Logout link, Preferences link
    Use this code in the processrequest of the controller on the page on which you want tohide these
    OAPageLayoutBean page = pageContext.getPageLayoutBean();
    page.prepareForRendering(pageContext);
    page.setGlobalButtons((oracle.cabo.ui.UINode)null);
    And for hiding the Copyright info, Privacy Statement:
    Click on Personalise page link
    then Click on Choose Context
    Choose "OAFooter" as Scope
    and click "Apply"
    and then Personalise the Copyright and Privay Statememnt icon and set rendered as False.
    Thanks,
    Gaurav
    Edited by: Gauravv on Aug 25, 2009 8:36 PM

  • Unsupported version Error while generating header and footer for the PDF

    hi
    I want to genrate header and footer for the PDF file however on the call of the EndPage class i got a Unsupported Version Error Unsupported major minor Version Error and Class loader Define class
    What has gone wrong ?.

    Can you provide more information about what you were trying to do? In particular, what does your code like like?
    Also, can you include the stack trace, and the complete error message?

Maybe you are looking for

  • Why when I store from my backup, it restores to a blank one?

    I recently restored my iPhone. But before I did I saved & backed up with both iCloud and iTunes. And when I try to retrive my stuff using the backup, from BOTH back ups it's just a blank one. It has nothing on it, no Contacts, Pictures, or anything t

  • Have JQuery for List View web part, But Full Text Not Visible

    I have a list view web part on a page with which, I've edited the view the way I need which includes grouping. Inside IE's Developer tools, I can see the structure (TD and then an anchor tag linking to document) and see the class SharePoint uses for

  • QT crashes with PDF image sequence

    A script generates 72 small (ca. 1100 bytes) pdf files as movie frames and I "Open Image Sequence...". The movie, whether saved or not, crashes at a certain point. All the frame files open without a problem in Preview. By laboriously weeding out the

  • Aperture 3 don't allow same reference file mgmt?

    With Aperture 2, I've kept all masters in Nikon Transfer folders. When reviewing and editing photos, I used Aperture to delete all unnecessary photos - it allowed me to choose if I want to delete version or both version and master (where ever that mi

  • Organiser will not sign in to account

    Adobe 13 on Mac Editor is signed in I can sign in on Web Organiser sign in fails I get " Sign in failed. Please check your connection and try again" I am connected as I can log in on web to my Adobe account Steve