none
Multiline Footers using c# RRS feed

  • Question

  • hello

    i am trying to develop a small application that creates a series of documents using information for various offices. I can get an image into the header and text into the footer, but i need to create multiple lines in the footer with different fonts/sizes. The following is my current code :-

    foreach (Word.Section section in doc.Sections) { Word.Range rng = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range; var shape = rng.InlineShapes.AddPicture(@"wolverhampton.jpg"); Word.Shape header_img = shape.ConvertToShape(); float hdr_height = header_img.Height; header_img.Left -= doc.PageSetup.LeftMargin; header_img.Top -= doc.PageSetup.TopMargin; header_img.Width = doc.PageSetup.PageWidth; header_img.Height = 175; Word.Range rng1 = section.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range; //var shape1 = rng1.InlineShapes.AddPicture(@"C:\footer.jpg"); //Word.Shape footer_img = shape1.ConvertToShape(); //float ftr_height = footer_img.Height; //footer_img.Left -= doc.PageSetup.LeftMargin; //footer_img.Top = doc.PageSetup.PageHeight - footer_img.Height; //footer_img.Width = doc.PageSetup.PageWidth;

    rng1.Font.Size = 22; rng1.Text = "this is a test"; rng2.Font.Size = 10; rng2.Text = "Another test"; }

    in the above code i get 2 lines of text but the second font change reverts all text in the range to that font size. How can i add another range with the new text in ? Should i be using another method to insert this text to the footer ?

    thanks in advance


    edit: sorry just realised this code does not contain the line to define rng2, as  i am in the middle of editing ( cant remeber what i had in to define the range at the moment)
    • Edited by jonnydexter Thursday, September 17, 2015 11:15 AM
    Thursday, September 17, 2015 11:13 AM

Answers

  • OK - after much messing about and testing I finally have something that does what I want for the moment. Although I can probably see me having to fight with this again in the near future.

    public void word_handler()
            {
                word.Document Doc = new word.Document();
                Doc = WD.Documents.Open(@"c:\test1.doc");
                WD.Visible = true;
                Doc.Sections.PageSetup.DifferentFirstPageHeaderFooter = -1;
    
                foreach (word.Section section in Doc.Sections)
                {
                    
                    word.Range footer = section.Footers[word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range;
                   
                    word.Paragraph para = footer.Paragraphs.Add(footer);
                    para.Range.Text = "First Para";
                    para.Range.Font.Size = 15;
                    para.Range.Font.Bold = 1;    
                    para.Range.InsertParagraphAfter(); //move insertion point to the end of the range
    
                    word.Paragraph p2 = footer.Paragraphs.Add(para.Range); //create the actual 'new' paragraph
                    p2.Range.Text = "Second Para";
                    p2.Range.Font.Size = 10;
                    p2.Range.Italic = 1;
                    p2.Range.InsertParagraphAfter();
    
                    word.Paragraph p3 = footer.Paragraphs.Add(p2.Range);
                    p3.Range.Text = "Third Para";
                    p3.Range.Font.Size = 14;
                    p3.Range.Italic = 0;
                    p3.Range.set_Style(word.WdBuiltinStyle.wdStyleNormal);
                }
            }

    If anyone has an easier way or a way to reduce the amount of changes to the range to get the desired text effect, I would be interested to know.

    I hope this clarifies my original intention and helps someone else going forward.

    I appreciate any help offered/given and thank you most sincerely.

    I will mark this as answered.


    • Marked as answer by jonnydexter Sunday, September 20, 2015 10:54 PM
    • Edited by jonnydexter Sunday, September 20, 2015 10:55 PM
    Sunday, September 20, 2015 10:54 PM

All replies

  • Hi jonnydexter,

    To deal with this issue, you need to reset the range.

    >> but i need to create multiple lines in the footer with different fonts/sizes

    You could iterate the paragraphs, then reset the range and change the fonts/sizes.

    For example:

    Word.Range rng = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range;
     Word.Paragraphs ps = rng.Paragraphs;
    foreach(Word.Paragraph p in ps)
          {
               Word.Range r = p.Range;
               r.SetRange(r.Start, r.End - 1);
               r.Font.Size = new Random().Next(1, 30);
          }

    Regards

    Starain


    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.

    Friday, September 18, 2015 7:41 AM
    Moderator
  • Hi Starain

    thanks for the reply, i can see what you are doing with this code, but i am looking to create new paragraphs and add the text as i go.

    The data is currently coming in from a dataset ( imported from XL).

    I have tried to add and insertafter with paragraphs and can see the count increase but do not know how to get into the required paragraph. indexing from the original startpoint does not work.

    public void word_handler()
            {
                word.Document Doc = new word.Document();
                Doc = WD.Documents.Open(@"c:\test1.doc");
                WD.Visible = true;
                Doc.Sections.PageSetup.DifferentFirstPageHeaderFooter = -1;
    
                
                foreach (word.Section section in Doc.Sections)
                {
    
                    word.Range footer = section.Footers[word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range;
                    
                    word.Range para = footer.Paragraphs[1].Range;
                    footer.InsertParagraph();
                    word.Range para1 = footer.Paragraphs[2].Range;
                    para.Text = "First Line";
                    para1.Text = "MORE TEXT"+Environment.NewLine;
                    //para.Text = "Line of Text" + Environment.NewLine;
    
    
                    para.Font.Name = "calibri";
                    para.Font.Bold = 1;
                }
            }
    

    The above code appears to create another paragraph but when adding the text, it replaces the text already on the footer ??

    i have tried creating a document with an example and then try to examine it in VS but it appears to be a paragraph collection. hence me trying to add more paragraphs.

    Am i understanding the DOM properly :-

    The footer range contains paragraphs (which have a range of their own) which contain sentences (which can be 1 or more characters (which also has a range ?))

    Sorry if i am confusing you but i cannot find a DOM layout for 2010 that shows what i am looking for, or explains the layout sufficiently. If anyone has a link to something suitable it would be appreciated.

    thanks




    • Edited by jonnydexter Sunday, September 20, 2015 7:09 PM
    Sunday, September 20, 2015 6:39 PM
  • OK - after much messing about and testing I finally have something that does what I want for the moment. Although I can probably see me having to fight with this again in the near future.

    public void word_handler()
            {
                word.Document Doc = new word.Document();
                Doc = WD.Documents.Open(@"c:\test1.doc");
                WD.Visible = true;
                Doc.Sections.PageSetup.DifferentFirstPageHeaderFooter = -1;
    
                foreach (word.Section section in Doc.Sections)
                {
                    
                    word.Range footer = section.Footers[word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range;
                   
                    word.Paragraph para = footer.Paragraphs.Add(footer);
                    para.Range.Text = "First Para";
                    para.Range.Font.Size = 15;
                    para.Range.Font.Bold = 1;    
                    para.Range.InsertParagraphAfter(); //move insertion point to the end of the range
    
                    word.Paragraph p2 = footer.Paragraphs.Add(para.Range); //create the actual 'new' paragraph
                    p2.Range.Text = "Second Para";
                    p2.Range.Font.Size = 10;
                    p2.Range.Italic = 1;
                    p2.Range.InsertParagraphAfter();
    
                    word.Paragraph p3 = footer.Paragraphs.Add(p2.Range);
                    p3.Range.Text = "Third Para";
                    p3.Range.Font.Size = 14;
                    p3.Range.Italic = 0;
                    p3.Range.set_Style(word.WdBuiltinStyle.wdStyleNormal);
                }
            }

    If anyone has an easier way or a way to reduce the amount of changes to the range to get the desired text effect, I would be interested to know.

    I hope this clarifies my original intention and helps someone else going forward.

    I appreciate any help offered/given and thank you most sincerely.

    I will mark this as answered.


    • Marked as answer by jonnydexter Sunday, September 20, 2015 10:54 PM
    • Edited by jonnydexter Sunday, September 20, 2015 10:55 PM
    Sunday, September 20, 2015 10:54 PM