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

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;
}
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 >>

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" />
// 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.
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 >>

Can you please provide a sample demo application for header and footer.
Thanks and Regards,
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 >>

Thank you.
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.
Iva Toteva
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

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?)
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;
Iva Toteva
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Jonathan

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
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;
Best wishes,
Boby
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

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;
}

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,

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
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,
Vasilthe Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

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


Thanks for your suggestion. Works good!

thanks
Vish

I have not done this, as a I said, so this recommendation may be worthless. Caveat emptor.
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 >>