This is a migrated thread and some comments may be shown as answers.

Adding footer and header

23 Answers 702 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Guy
Top achievements
Rank 1
Guy asked on 03 Aug 2011, 03:55 PM
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.

23 Answers, 1 is accepted

Sort by
0
udaybhaskar
Top achievements
Rank 1
answered on 04 Aug 2011, 05:58 AM
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;
        }
0
Ivailo Karamanolev
Telerik team
answered on 05 Aug 2011, 01:56 PM
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 >>

0
Allen
Top achievements
Rank 2
Iron
Veteran
answered on 27 Sep 2011, 10:21 PM
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.
0
Iva Toteva
Telerik team
answered on 30 Sep 2011, 05:27 PM
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 >>

0
Nagarjun
Top achievements
Rank 1
answered on 09 Nov 2011, 06:16 PM
hi Iva Toteva,
 
Can you please provide a sample demo application for header and footer.

Thanks and Regards,
Nagarjun.
0
Iva Toteva
Telerik team
answered on 18 Nov 2011, 10:50 AM
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 >>

0
Allen
Top achievements
Rank 2
Iron
Veteran
answered on 18 Nov 2011, 02:43 PM
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.
0
Iva Toteva
Telerik team
answered on 24 Nov 2011, 04:51 PM
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 >>

0
Jonathan
Top achievements
Rank 1
answered on 01 Dec 2011, 10:40 PM
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?) 
0
Iva Toteva
Telerik team
answered on 02 Dec 2011, 05:34 PM
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 >>

0
Jonathan
Top achievements
Rank 1
answered on 02 Dec 2011, 07:04 PM
Thanks, Iva!
Jonathan
0
Jonathan
Top achievements
Rank 1
answered on 03 Dec 2011, 05:36 AM
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

0
Boby
Telerik team
answered on 07 Dec 2011, 06:32 PM
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 >>

0
Jonathan
Top achievements
Rank 1
answered on 07 Dec 2011, 10:25 PM

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;
}
0
Manuel
Top achievements
Rank 1
answered on 09 Dec 2011, 03:24 PM
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,
0
Jonathan
Top achievements
Rank 1
answered on 09 Dec 2011, 05:36 PM
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
0
Vasil
Telerik team
answered on 13 Dec 2011, 12:23 PM
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 >>

0
Manuel
Top achievements
Rank 1
answered on 13 Dec 2011, 03:58 PM
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
0
Allen
Top achievements
Rank 2
Iron
Veteran
answered on 13 Dec 2011, 04:18 PM
Why not just create an RTF?  Doesn't Open Office support those?  Is you document taking advantage of any new MS Word Docx features?
0
Manuel
Top achievements
Rank 1
answered on 14 Dec 2011, 10:34 AM
Hi Allen,

Thanks for your suggestion. Works good!

0
Vishnu
Top achievements
Rank 1
answered on 08 Sep 2013, 02:59 PM
how to apply different footers to different sections that are merged from different RadDocuments ?

thanks
Vish
0
Allen
Top achievements
Rank 2
Iron
Veteran
answered on 08 Sep 2013, 03:13 PM
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.
0
Vasil
Telerik team
answered on 10 Sep 2013, 02:15 PM
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 >>

Tags
RichTextBox
Asked by
Guy
Top achievements
Rank 1
Answers by
udaybhaskar
Top achievements
Rank 1
Ivailo Karamanolev
Telerik team
Allen
Top achievements
Rank 2
Iron
Veteran
Iva Toteva
Telerik team
Nagarjun
Top achievements
Rank 1
Jonathan
Top achievements
Rank 1
Boby
Telerik team
Manuel
Top achievements
Rank 1
Vasil
Telerik team
Vishnu
Top achievements
Rank 1
Share this question
or