Adding footer and header

24 posts, 0 answers
  1. Guy
    Guy avatar
    46 posts
    Member since:
    Nov 2008

    Posted 03 Aug 2011 Link to this post

    Hi,

    I saw in the Q2 2011 the footer/ header feature is availaible. I Created dynamically my RadDocument i inserted in the RadRichTextBox. How can i add the footer, header and page number to my RadDocument??

    My second question is about the problem i got from Tables I inserted in my RadDocument. I customized myself the PreferedWidth on my TableCell when i create my tables. This was working in the previous version but when i upgraded to Q2 my TableCell have always the same width according to the page Width. Is there any way to solve this problems??

    Thank u  for your precisous help..

    Best regards.
  2. udaybhaskar
    udaybhaskar avatar
    10 posts
    Member since:
    Aug 2010

    Posted 04 Aug 2011 Link to this post

    Hi Guy, Below is the sample code to create Header & Footer for a Document. We have to create Header and Footer and add them to the Document.

    public
    RadDocument CreateDocumentWithCustomControls()
            {
                RadDocument doc = new RadDocument();
                doc.LayoutMode = DocumentLayoutMode.Paged;
      
                #region Headers&Footers
      
                RadDocument headerDoc = new RadDocument();
                RadDocument footerDoc = new RadDocument();
      
                Section hSection = new Section();
                headerDoc.Sections.Add(hSection);
                headerDoc.SectionDefaultPageMargin = new Telerik.Windows.Documents.Layout.Padding(5, 5, 5, 5);
      
                Paragraph hparagraph = new Paragraph();
                hSection.Blocks.Add(hparagraph);
      
                Span hspan = new Span("Default Header");
                hspan.FontSize = 30;
      
                hparagraph.Inlines.Add(hspan);
      
                Section fSection = new Section();
                footerDoc.Sections.Add(fSection);
                footerDoc.SectionDefaultPageMargin = new Telerik.Windows.Documents.Layout.Padding(5, 5, 5, 5);
      
                Paragraph fparagraph = new Paragraph();
                fSection.Blocks.Add(fparagraph);
      
                Span fspan = new Span("Default Footer");
                fspan.FontSize = 30;
      
                fparagraph.Inlines.Add(fspan);
      
                Header header = new Header();
                Footer footer = new Footer();
                header.Body = headerDoc;
                footer.Body = footerDoc;
      
                #endregion
      
                Section section = new Section();
                section.Headers.Default = header;
                section.Footers.Default = footer;
      
                doc.Sections.Add(section);
                doc.SectionDefaultPageMargin = new Telerik.Windows.Documents.Layout.Padding(10, 10, 10, 10);
      
                Paragraph paragraph = new Paragraph();
                section.Blocks.Add(paragraph);
      
                Span span = new Span("Some sample text...");
                span.FontSize = 30;
                paragraph.Inlines.Add(span);
      
                return doc;
            }
  3. DevCraft banner
  4. Ivailo Karamanolev
    Admin
    Ivailo Karamanolev avatar
    276 posts

    Posted 05 Aug 2011 Link to this post

    Hi Guy,

    As for the headers and footers, I can see udaybhaskar gave you a good sample, let us know if you have any additional questions.
    As for the table layout, there is a known issue in the Q2 release that prevents the tables from being laid out properly when you set preferred width to cells. This has since been fixed and should appear in the next LIB.

    Regards,
    Ivailo
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

  5. Allen
    Allen avatar
    50 posts
    Member since:
    Mar 2011

    Posted 27 Sep 2011 Link to this post

    Is there an example of this for WPF that uses Xaml and binding or must I use code-behind? 

    Here is how I am binding to a string variable accumulated  from logging messages right now:
    <Txt:TxtDataProvider x:Name="textDataProvider"<br> 
             Text="{Binding SysMessageText, Mode=OneWay}"<
    br>    
          RichTextBox="{Binding ElementName=radRichTextBox}"  <
    br
             SetupDocument="TextDataProviderSetupDocument" />
    and in Code-behind in the vent handler for SetupDocument

     
    // CODE-BEHIND<br><br>       
    private void TextDataProviderSetupDocument(object sender, SetupDocumentEventArgs e)<br> 
          {<br>            e.Document.LayoutMode = DocumentLayoutMode.Paged;<br>
               e.Document.ParagraphDefaultSpacingAfter = 10;<br>  
              e.Document.PageViewMargin = new SizeF(10, 10);<br>       
        e.Document.SectionDefaultPageMargin = new Padding(95);<br>        }

    This works and makes a document that is acceptable but I would like it to have a header and foter  calculated for each session and page numbers.  I am not averse to using code behind as you can see but the way to do this seems a bit mysterious.

    But I cannot detect a way to declaratively add headers and footers to this text.
  6. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 30 Sep 2011 Link to this post

    Hi Allen,

    If you use XamlDataProvider instead of TxtDataProvider, you can persist the LayoutMode, ParagraphDefaultSpacingAfter, PageViewMargin and SectionDefaultPageMargin in the exported XAML string and you would not need to set them each time on SetupDocument. It will also enable the persistence of Headers and Footers.
    If you decide to continue using TxtDataProvider, you can set the Headers and Footers of the document in the following way:
    e.Document.Sections.First.Headers.Default = new Header() { Body = headerDocument };
    e.Document.Sections.First.Footers.Default = new Footer() { Body = footerDocument };

    where headerDocument and footerDocument are RadDocuments, which you have created as in Udaybhaskar's post.

    All the best,
    Iva
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  7. Nagarjun
    Nagarjun avatar
    8 posts
    Member since:
    Nov 2011

    Posted 09 Nov 2011 Link to this post

    hi Iva Toteva,
     
    Can you please provide a sample demo application for header and footer.

    Thanks and Regards,
    Nagarjun.
  8. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 18 Nov 2011 Link to this post

    Hi Nagarjun,

    Please find attached the demo you requested. It uses the code for udaybhaskar's snippet to create a sample document with headers and footers.

    Kind regards,
    Iva Toteva
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  9. Allen
    Allen avatar
    50 posts
    Member since:
    Mar 2011

    Posted 18 Nov 2011 Link to this post

    I wonder if you might enrich this example slightly by including the directives for a  page # and print or edit date in the footer or header, or furnish a link to the proper syntax.  Alternatively, or perhaps in addition, provide an example that embeds an exterrnally generated RadDocument or Word/RTF document to provide the header/footer and body, in as DECLARATIVE a manner as possible to avoid unnecessary code-behind. 
    Thank you.
  10. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 24 Nov 2011 Link to this post

    Hello Allen,

    Please find attached a demo which illustrates how a document with a PageField and DateField in the headers and footers can be created programatically. I have also included the requested creation of headers and footers using docx/RTF files.

    However, neither of these tasks can be achieved in a declarative way. RadRichTextBox uses commands in order to enable the most common operations without the use of code-behind. For example, there are commands for opening a document, saving a document, performing different modifications to the formatting in parts of the document, etc. Setting different headers and footers to an existing document is not one of these modifications.

    The only way to declaratively set the content of the document is to set it in XAML. I have also included an example of that in the demo.
    I hope this helps.

    Kind regards,
    Iva Toteva
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  11. Jonathan
    Jonathan avatar
    7 posts
    Member since:
    Apr 2011

    Posted 01 Dec 2011 Link to this post

    Hello:

    I'm trying the c# code from the above project to place page and date-time stamp in the header and footer of a raddocument in a radrichtextbox in a WPF form.

    I'm getting an error on the line:

    document.CaretPosition.MoveToInline(cell12.EnumerateChildrenOfType<Inline>().Last().GetAssociatedLayoutBoxes().Last() as InlineLayoutBox, 0);

    The compiler is complaining about the first instance of "Last": Here is the error language:

    Error 9 'System.Collections.Generic.IEnumerable<Telerik.Windows.Documents.Model.Inline>' does not contain a definition for 'Last' and no extension method 'Last' accepting a first argument of type 'System.Collections.Generic.IEnumerable<Telerik.Windows.Documents.Model.Inline>' could be found (are you missing a using directive or an assembly reference?) 
  12. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 02 Dec 2011 Link to this post

    Hello Jonathan,

    The method Last() is included in the System.Linq  namespace. The problem must be resolved by adding a using declaration like this:

    using System.Linq;

    Greetings,
    Iva Toteva
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  13. Jonathan
    Jonathan avatar
    7 posts
    Member since:
    Apr 2011

    Posted 02 Dec 2011 Link to this post

    Thanks, Iva!
    Jonathan
  14. Jonathan
    Jonathan avatar
    7 posts
    Member since:
    Apr 2011

    Posted 02 Dec 2011 Link to this post

    I have gotten the headers and footers to work nicely.

    Now I have two questions:

    1) I'd like to set the margins of the overall doc to be "narrow" -- I can do this manually after the document is finished loading using the radrichtextbox ui. Is there a way to set the margins to narrow in code? I've tried:

    doc.SectionDefaultPageMargin =

     

    new Padding(0);

     

    this doesn't seem to be having any effect.

    2) I'd like my headers and footers to appear only on first page. Or, at least I'd like to have first page header/footer be different from subsequent pages. How to do this in code?

    Thanks,

    Jonathan

  15. Boby
    Admin
    Boby avatar
    595 posts

    Posted 07 Dec 2011 Link to this post

    Hi Jonathan,
    1) You can set the page margin for the whole document (as descreibed here) or for the specific section:
    Section firstSection = doc.Sections.First;
    firstSection.PageMargin = PageMarginTypesConverter.ToPadding(PageMarginTypes.Narrow);

    2) You can set different header for the first page using the following:

    Section firstSection = doc.Sections.First;
    firstSection.HasDifferentFirstPageHeaderFooter = true;
    firstSection.Headers.First.Body = firstHeaderDocument;
    If you need further assistance do not hesitate to contact us again.

    Best wishes,
    Boby
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  16. Jonathan
    Jonathan avatar
    7 posts
    Member since:
    Apr 2011

    Posted 07 Dec 2011 Link to this post

    Boby,

    Thanks for the tips. I'm not having success, however.

    First: Headers & Footers. I'm following your example, combining with earlier examples. My aim is to have a header & footer on page 1, and no header or footer on all subsequent pages (The header & footer are supposed to be "letterhead"). 

    Instead, I'm getting the opposite. Nothing on page one, nice header & footer on page 2.

    Ideally, I'd like to be able to have a footer with a page number on page 2.

    How do I get the header and footer on page 1, with something different on page 2?

    Here's my method:


    private RadDocument GetHeaderAndFooter(string headerCode, string footerCode)
    {
        RadDocument doc = new RadDocument();
        doc.LayoutMode = DocumentLayoutMode.Paged;
        #region Headers
        RadDocument headerDoc ; //= new RadDocument();
        InstrumentBlockEntity HeadBlock = this.oInstrumentBlock.GetBlockEntityByCode(headerCode);
        headerDoc = this.ImportDocx(HeadBlock.BinaryBlockText);
        headerDoc.LayoutMode = DocumentLayoutMode.Flow;
        Section hSection = new Section();
        headerDoc.Sections.Add(hSection);
        headerDoc.SectionDefaultPageMargin = new Telerik.Windows.Documents.Layout.Padding(0);
        Header header = new Header();
        header.Body = headerDoc;
        header.Body.SectionDefaultPageMargin = new Telerik.Windows.Documents.Layout.Padding(20, 20, 20, 20);
              
        #endregion
        #region Footers
        Footer footer = new Footer();
        if (!mmType.IsEmpty(footerCode))
        {
            RadDocument footerDoc ; //= new RadDocument();
            InstrumentBlockEntity FootBlock = this.oInstrumentBlock.GetBlockEntityByCode(footerCode);
            footerDoc = this.ImportDocx(FootBlock.BinaryBlockText);
            footerDoc.LayoutMode = DocumentLayoutMode.Flow;
            Section fSection = new Section();
            footerDoc.Sections.Add(fSection);
            footerDoc.SectionDefaultPageMargin = new Telerik.Windows.Documents.Layout.Padding(0);
            footer.Body = footerDoc;
        }
        #endregion
        Section section = new Section();
        doc.Sections.Add(section);
        Section firstSection = doc.Sections.First;
        firstSection.HasDifferentFirstPageHeaderFooter = true;
        firstSection.Headers.Default = header;
        firstSection.Footers.Default = footer;
        doc.SectionDefaultPageMargin = new Telerik.Windows.Documents.Layout.Padding(20, 10, 20, 20);
          
        return doc;
    }
  17. Manuel
    Manuel avatar
    61 posts
    Member since:
    Oct 2011

    Posted 09 Dec 2011 Link to this post

    Hi there,

    I´, Thinking that maybe RadDocument has a bug with OpenOffice.
    In my silverlight app I create automatically a RadDocument with some text. And then I have a second button that will generate a pdf of this RadDocument. However, I can´t create a pdf file from RadDocument...OpenOffice give me an error, and also give an error, when I try open the docx created by RadDocument using OpenOffice.

    So I did simple test, open and generate a pdf file with docx file created using MS-Office, the application works good. When I create the docx file using RadDocument, my application give the error, and if I try open with OpenOffice is impossible also.

    So, maybe the problem is using RadDocument, maybe have some problem with OpenOffice.

    Here my code to create the document:

    public void ComandoGeraDOCDocumentoReuniaoClick(object sender, RoutedEventArgs e)
            {
                HtmlFormatProvider providerHtml = new HtmlFormatProvider();
                TxtFormatProvider providerText = new TxtFormatProvider();
                DocxFormatProvider provider = new DocxFormatProvider();
                
                //criar documento
                RadDocument doc = new RadDocument();
                Telerik.Windows.Documents.Model.Section section = new Telerik.Windows.Documents.Model.Section();
                doc.Sections.Add(section);
                doc.SectionDefaultPageMargin = new Telerik.Windows.Documents.Layout.Padding(100,100,100,100);
                
                //titulo da minuta
                Telerik.Windows.Documents.Model.Paragraph paragraph = new Telerik.Windows.Documents.Model.Paragraph();
                section.Blocks.Add(paragraph);
                Telerik.Windows.Documents.Model.Span span = new Telerik.Windows.Documents.Model.Span("Minuta");
                span.FontSize = 30;
                paragraph.Inlines.Add(span);

                int i = 1;
                //para cada assunto aprovado
                foreach (Assuntos assunto in OCAssuntosReuniao.Where(A => A.AprovadoProp == 1))
                {
                    //inserir no doc
                    Telerik.Windows.Documents.Model.Paragraph paragraph1 = new Telerik.Windows.Documents.Model.Paragraph();
                    section.Blocks.Add(paragraph1);
                    Telerik.Windows.Documents.Model.Span spanAux = new Telerik.Windows.Documents.Model.Span("Assunto " + i++ + ":");
                    paragraph1.Inlines.Add(spanAux);

                    Telerik.Windows.Documents.Model.Paragraph paragraph2 = new Telerik.Windows.Documents.Model.Paragraph();
                    section.Blocks.Add(paragraph2);
                    Telerik.Windows.Documents.Model.Span spanAux2 = new Telerik.Windows.Documents.Model.Span(providerText.Export(providerHtml.Import(assunto.ConteudoProp)));
                    paragraph2.Inlines.Add(spanAux2);
                }

                //converte documento para bytes
                MemoryStream stream = new MemoryStream();
                provider.Export(doc, stream);
                
                byte[] conteudoDoc = Util.StreamToByteArray(stream);
                
                //procura documento
                DocumentosReuniao documentoReuniao = null;
                foreach (DocumentosReuniao docReuniao in OCDocumentosReuniao)
                {
                    if (docReuniao.DocumentoBaseSgaIdProp == 1) //1 - código do documento base, Minuta
                        documentoReuniao = docReuniao;
                }

                if (documentoReuniao == null)
                {
                    //cria documento
                    guardaficheiroBD(conteudoDoc.Length,
                        conteudoDoc,
                        "minuta.docx",
                        AppGlobais.Globais.reuniaoGlobal.IdProp,
                        AppGlobais.Globais.participanteReuniao.IdProp.ToString(),
                        "Minuta",
                        null,
                        0,
                        "1",
                        "Documento Gerado Automaticamente");
                }
                else
                {
                    //actualiza conteudo do documento
                    actualizaConteudoDocumentoReuniao(AppGlobais.Globais.reuniaoGlobal.IdProp,
                        documentoReuniao.UrlFicheiroProp,
                        conteudoDoc,
                        documentoReuniao.VersaoDocumentoProp,
                        true);
                }
            }


    Any idea how to solve that?

    Thanks,
  18. Jonathan
    Jonathan avatar
    7 posts
    Member since:
    Apr 2011

    Posted 09 Dec 2011 Link to this post

    RE different header on first page question, thanks to Telerik support for giving me the answer.

    Instead of:

    firstSection.HasDifferentFirstPageHeaderFooter = true
    firstSection.Headers.Default = header; 
    firstSection.Footers.Default = footer; 

    use:

    firstSection.HasDifferentFirstPageHeaderFooter = true
    firstSection.Headers.First = header; 
    firstSection.Footers.First = footer; 

    since the Headers.Default will be used for subsequent pages, when HasDifferentFirstPageHeaderFooter is true.

    Jonathan
  19. Vasil
    Admin
    Vasil avatar
    124 posts

    Posted 13 Dec 2011 Link to this post

    Hi Manuel,

    Docx fles created by RadRichTextBox cannot be opened by OpenOffice; this is known problem, we have it in our to-do list and it is scheduled for the upcoming 2011 Q3 SP1 or internal build soon after that.

    Please, excuse us for the inconvenience. Don't hesitate to contact us if you have other questions.

    Regards,

    Vasil
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  20. Manuel
    Manuel avatar
    61 posts
    Member since:
    Oct 2011

    Posted 13 Dec 2011 Link to this post

    Hi Vasil,

    Thanks for the fast answer. This for me is a big problem. There is another way of do that? There is some way of create a docx file withou using RadDocument? Maybe some free library? I don´t know.

    Thanks
  21. Allen
    Allen avatar
    50 posts
    Member since:
    Mar 2011

    Posted 13 Dec 2011 Link to this post

    Why not just create an RTF?  Doesn't Open Office support those?  Is you document taking advantage of any new MS Word Docx features?
  22. Manuel
    Manuel avatar
    61 posts
    Member since:
    Oct 2011

    Posted 14 Dec 2011 Link to this post

    Hi Allen,

    Thanks for your suggestion. Works good!

  23. Vishnu
    Vishnu avatar
    5 posts
    Member since:
    Sep 2013

    Posted 08 Sep 2013 Link to this post

    how to apply different footers to different sections that are merged from different RadDocuments ?

    thanks
    Vish
  24. Allen
    Allen avatar
    50 posts
    Member since:
    Mar 2011

    Posted 08 Sep 2013 Link to this post

    I have not done this, since my document only had 1 section.  However, if I were going to attack this, I would define a template document with as many sections as expected, each formatted as desired vis a vis footers and headers, then traverse this document section-by-section and squirt in the merged documents.  This is predictable.  An alternative is to determine how many documents are to be inserted, then add sections for each to a starter document, where the headers and foooters cannot be pre-cooked.  You will have to pay attention to the settings for different headers and footers by section and so forth or it may be that the template headers and footers will override the documents you are trying to merge in.

    I have not done this, as a I said, so this recommendation may be worthless.  Caveat emptor.
  25. Vasil
    Admin
    Vasil avatar
    124 posts

    Posted 10 Sep 2013 Link to this post

    Hi Vish,

    I can see that Allen already provided you with some valuable ideas. 

    I am not sure what the exact result you are trying to achieve is, but you can find an example in our SDK repository demonstrating how to merge several documents by preserving all properties of the little pieces including their headers and footers. Tthe example is located here, I hope it helps!

    Feel free to get back to us if you have other questions.

    Regards,
    Vasil
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Back to Top
DevCraft banner