none
Adding header to footer to an existing office documents RRS feed

  • Question

  • Hi All,
         Hope you are all well?
    I have a scenario where i would like to add a header(e.g image) and a footer some text or also image to an existing force documents.
    Is this possible using Open XML.
    So the idea is the user will select a a header a footer and then the document and hit submit.

    Thanks in Advance

    Sunday, May 6, 2012 12:17 PM

Answers

  • Hi

    Please see if below code helps you

                 //Create word document   
                 Document objDoc = new Document();
                 //Create section   
                 Section section = objDoc.AddSection();
                 //Create a second section   
                 Section section1 = objDoc.AddSection();
                 //Put the picture into the Header.   
                 HeaderFooter objHeader = section.HeadersFooters.Header;
                 Paragraph objHeaderParagraph = objHeader.AddParagraph();
                 // You can get this image or text from User
                 DocPicture objHeaderPicture = objHeaderParagraph.AppendPicture(Image.FromFile("MyHeader.jpg"));
                 //Picture layout   
                 objHeaderPicture.TextWrappingStyle = TextWrappingStyle.Behind;
                 objHeaderPicture.HorizontalOrigin = HorizontalOrigin.Page;
                 objHeaderPicture.HorizontalAlignment = ShapeHorizontalAlignment.Left;
                 objHeaderPicture.VerticalOrigin = VerticalOrigin.Page;
                 objHeaderPicture.VerticalAlignment = ShapeVerticalAlignment.Top;
                 //Put a paragraph as the Header.   
                 Paragraph objHeaderParagraph1 = objHeader.AddParagraph();
                 objHeaderParagraph1.AppendText("This is my Test doc").CharacterFormat.FontSize = 15;             
                 //Header border   
                 objHeaderParagraph1.Format.Borders.Bottom.BorderType = Spire.Doc.Documents.BorderStyle.Single;
                 objHeaderParagraph1.Format.Borders.Bottom.Space = 0.05F;
                 //Put the picture into the Footer.   
                 HeaderFooter footer = section.HeadersFooters.Footer;
                 Paragraph objFooterParagraph = footer.AddParagraph();
                 // You can get this image or text from User
                 DocPicture objFooterPicture = objFooterParagraph.AppendPicture(Image.FromFile("MyFooter.jpg"));
                 //Picture layout   
                 objFooterPicture.TextWrappingStyle = TextWrappingStyle.Behind;
                 objFooterPicture.HorizontalOrigin = HorizontalOrigin.Page;
                 objFooterPicture.HorizontalAlignment = ShapeHorizontalAlignment.Left;
                 objFooterPicture.VerticalOrigin = VerticalOrigin.Page;
                 objFooterPicture.VerticalAlignment = ShapeVerticalAlignment.Bottom;
                 //Footer border   
                 objFooterParagraph.Format.Borders.Top.BorderType = Spire.Doc.Documents.BorderStyle.Single;
                 objFooterParagraph.Format.Borders.Top.Space = 0.05F;
                 //Footer page number   
                 objFooterParagraph.AppendField("Page Number", FieldType.FieldPage);
                 objFooterParagraph.AppendText(" out of ");
                 objFooterParagraph.AppendField(FieldType.FieldNumPages);
                 objFooterParagraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Right;
                 //Save doc file.   
                 objDoc.SaveToFile("MyHeaderFooter.doc", FileFormat.Doc);
                 //Launching the MS Word file.   
                 System.Diagnostics.Process.Start("MyHeaderFooter.doc"); 

    Thanks and RegardS
    Sharad


    sharad

    Thursday, May 10, 2012 6:33 AM
  • It's my custom Style element. You can removed it from Code as it may not be useful for you.

    Thanks Patrick.


    sharad


    Wednesday, May 16, 2012 12:03 PM

All replies

  • Hi Patrick.I,

    Thanks for posting in the MSDN Forum.

    Would you please tell me which kind of document your will handle? Is it a Word document?

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Monday, May 7, 2012 3:14 AM
    Moderator
  • Hi

    Please see if below code helps you

                 //Create word document   
                 Document objDoc = new Document();
                 //Create section   
                 Section section = objDoc.AddSection();
                 //Create a second section   
                 Section section1 = objDoc.AddSection();
                 //Put the picture into the Header.   
                 HeaderFooter objHeader = section.HeadersFooters.Header;
                 Paragraph objHeaderParagraph = objHeader.AddParagraph();
                 // You can get this image or text from User
                 DocPicture objHeaderPicture = objHeaderParagraph.AppendPicture(Image.FromFile("MyHeader.jpg"));
                 //Picture layout   
                 objHeaderPicture.TextWrappingStyle = TextWrappingStyle.Behind;
                 objHeaderPicture.HorizontalOrigin = HorizontalOrigin.Page;
                 objHeaderPicture.HorizontalAlignment = ShapeHorizontalAlignment.Left;
                 objHeaderPicture.VerticalOrigin = VerticalOrigin.Page;
                 objHeaderPicture.VerticalAlignment = ShapeVerticalAlignment.Top;
                 //Put a paragraph as the Header.   
                 Paragraph objHeaderParagraph1 = objHeader.AddParagraph();
                 objHeaderParagraph1.AppendText("This is my Test doc").CharacterFormat.FontSize = 15;             
                 //Header border   
                 objHeaderParagraph1.Format.Borders.Bottom.BorderType = Spire.Doc.Documents.BorderStyle.Single;
                 objHeaderParagraph1.Format.Borders.Bottom.Space = 0.05F;
                 //Put the picture into the Footer.   
                 HeaderFooter footer = section.HeadersFooters.Footer;
                 Paragraph objFooterParagraph = footer.AddParagraph();
                 // You can get this image or text from User
                 DocPicture objFooterPicture = objFooterParagraph.AppendPicture(Image.FromFile("MyFooter.jpg"));
                 //Picture layout   
                 objFooterPicture.TextWrappingStyle = TextWrappingStyle.Behind;
                 objFooterPicture.HorizontalOrigin = HorizontalOrigin.Page;
                 objFooterPicture.HorizontalAlignment = ShapeHorizontalAlignment.Left;
                 objFooterPicture.VerticalOrigin = VerticalOrigin.Page;
                 objFooterPicture.VerticalAlignment = ShapeVerticalAlignment.Bottom;
                 //Footer border   
                 objFooterParagraph.Format.Borders.Top.BorderType = Spire.Doc.Documents.BorderStyle.Single;
                 objFooterParagraph.Format.Borders.Top.Space = 0.05F;
                 //Footer page number   
                 objFooterParagraph.AppendField("Page Number", FieldType.FieldPage);
                 objFooterParagraph.AppendText(" out of ");
                 objFooterParagraph.AppendField(FieldType.FieldNumPages);
                 objFooterParagraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Right;
                 //Save doc file.   
                 objDoc.SaveToFile("MyHeaderFooter.doc", FileFormat.Doc);
                 //Launching the MS Word file.   
                 System.Diagnostics.Process.Start("MyHeaderFooter.doc"); 

    Thanks and RegardS
    Sharad


    sharad

    Thursday, May 10, 2012 6:33 AM
  • Sharad whats this "Spire.Doc.Documents.BorderStyle.Single"

    Cheers

    Monday, May 14, 2012 7:41 AM
  • It's my custom Style element. You can removed it from Code as it may not be useful for you.

    Thanks Patrick.


    sharad


    Wednesday, May 16, 2012 12:03 PM