As an aside, I read in a post on this forum that it is possible to re-style the main control to remove the blue background. Would it be possible to do a similar thing to whichever style is applied to individual pages that are shown in a paged document in order to template in some page numbers and/or header or footers into the pages? Both of these things will be important for me going forward in my application (is there an ETA on when they might be included in the control itself?)
Thanks very much.
14 Answers, 1 is accepted
The solution to your first question is basically searching for the right parent of the element in the layout system and looking where in the tree it is. Here's some code you can directly use to achieve what you need:
public
int
GetPageNumberForDocumentElement(DocumentElement element)
{
LayoutBox layoutBox = element.FirstLayoutBox;
while
(!(layoutBox
is
SectionLayoutBox))
{
layoutBox = layoutBox.Parent;
}
return
layoutBox.ChildIndex;
}
I am attaching a project which shows how you can add page numbers in the bottom of the page. Note that the same approach may not work correctly with controls that can be focused, such as text boxes and rich text boxes. However, editing headers and footers in a separate window may work out.
We have planned implementing genuine headers and footers with import and export for the next major release - Q1 2011 or soon after that.
If you have other questions, do not hesitate to contact us again.
Regards,
Iva
the Telerik team

I try to use the GetPageNumberForDocumentElement metod and only return 0.
This is the code:
Section sectionText1 = new Section();
Paragraph paragraphtxt1 = new Paragraph();
paragraphtxt1.Inlines.Add(new Span("texto texto texto"+FormattingSymbolLayoutBox.PAGE_BREAK));
sectionText1 .Children.Add(paragraphtxt1);
Section sectionText2 = new Section();
Paragraph paragraphtxt2 = new Paragraph();
paragraphtxt2.Inlines.Add(new Span("texto texto texto"+FormattingSymbolLayoutBox.PAGE_BREAK));
sectionText2 .Children.Add(paragraphtxt2);
Section sectionText3 = new Section();
Paragraph paragraphtxt3 = new Paragraph();
paragraphtxt3.Inlines.Add(new Span("texto texto texto"+FormattingSymbolLayoutBox.PAGE_BREAK));
sectionText3 .Children.Add(paragraphtxt3);
editor.Document.Sections.Add(sectionText1 );
editor.Document.Sections.Add(sectionText2 );
editor.Document.Sections.Add(sectionText3 );
GetPageNumberForDocumentElement(editor.Document.Sections.Last())
And the GetPageNumberForDocumentElement returns 0;
Thanks very much.
Currently RadDocument supports only one section in its document. You can add new sections to the document, but they are merged into one when you start changing or export the document. Thus, the last section is actually the first one, which starts at page 0. This is limitation we will resolve in the next version.
Note that the editor's layout needs to be updated when you add document elements to the document.
This is how I have modified your code:
private
void
AddDocumentElements()
{
Section sectionText1 =
new
Section();
Paragraph paragraphtxt1 =
new
Paragraph();
paragraphtxt1.Inlines.Add(
new
Span(
"texto texto texto"
+ FormattingSymbolLayoutBox.PAGE_BREAK));
sectionText1.Children.Add(paragraphtxt1);
Section sectionText2 =
new
Section();
Paragraph paragraphtxt2 =
new
Paragraph();
paragraphtxt2.Inlines.Add(
new
Span(
"texto texto texto"
+ FormattingSymbolLayoutBox.PAGE_BREAK));
sectionText2.Children.Add(paragraphtxt2);
Section sectionText3 =
new
Section();
Paragraph paragraphtxt3 =
new
Paragraph();
paragraphtxt3.Inlines.Add(
new
Span(
"texto texto texto"
));
sectionText3.Children.Add(paragraphtxt3);
editor.Document.Sections.Add(sectionText1);
editor.Document.Sections.Add(sectionText2);
editor.Document.Sections.Add(sectionText3);
editor.Document.ShowFormattingSymbols =
true
;
editor.UpdateEditorLayout();
Block last = editor.Document.Sections.Last.Blocks.Last;
MessageBox.Show(GetPageNumberForDocumentElement(last).ToString());
}
Note that each paragraph (or its first span for that matter) starts from the paragraph-end symbol of the previous paragraph, so if you have only one paragraph on the last page, the index will not be correct (it will be the actual page number - 1).
If you have other questions, do not hesitate to contact us again.
Regards,
Iva
the Telerik team

With the GetPageNumberForDocumentElement function, i only read the '0'.
I only need to know the length of a Document in pages. I try using this code. But Imposible.
MsWord edt= new MsWord();
RadDocument testDocument = new RadDocument();
testDocument.LayoutMode =DocumentLayoutMode.Paged;
testDocument.ParagraphDefaultSpacingAfter = testDocument.ParagraphDefaultSpacingBefore = 0;
testDocument.PageViewMargin = new SizeF(10, 10);
testDocument.SectionDefaultPageMargin = new Padding(75, 120, 115, 120);
testDocument.Sections.Add(new Section());
edt.SetupNewDocument(testDocument);
Paragraph paragraphtxt1 = new Paragraph();
paragraphtxt1.Inlines.Add(new Span("texto texto texto" + FormattingSymbolLayoutBox.PAGE_BREAK));
Paragraph paragraphtxt2 = newParagraph();
paragraphtxt2.Inlines.Add(new Span("texto texto texto" + FormattingSymbolLayoutBox.PAGE_BREAK));
Paragraph paragraphtxt3 = new Paragraph();
paragraphtxt3.Inlines.Add(new Span("texto texto texto"));
testDocument.Sections.First.Blocks.Add(paragraphtxt1);
testDocument.Sections.First.Blocks.Add(paragraphtxt2);
testDocument.Sections.First.Blocks.Add(paragraphtxt3);
testDocument.ShowFormattingSymbols = true;
edt.UpdateLayout();
Block last = testDocument.Sections.Last.Blocks.Last;
MessageBox.Show(GetPageNumberForDocumentElement(last).ToString());
this document, have 3 pages really, then, i need only that know how get the number of pages.
Now, the document have only 1 section with 3 paragraph.
¿Is imposible to know the number of pages of the RichTextBox?
Really, i have a Blank richTextBox, and with code, i insert on int a lot of paragraph. I need to do a index of the document in the first page, and then i need to know for example the number of pages of the document before i insert the next paragraph.
Previusly, i create a TestRadDocument, an in it i insert all the text, to generate the index, then, i copy the paragraphs and tables in this rad docuemnt to the editor.docuemnt, including the index page with the numbers of page of each paragraph.
I do the next.
RadDocument testDocument = new RadDocument();
testDocument.LayoutMode = DocumentLayoutMode.Paged;
testDocument.ParagraphDefaultSpacingAfter = testDocument.ParagraphDefaultSpacingBefore = 0;
testDocument.PageViewMargin = new SizeF(10, 10);
testDocument.SectionDefaultPageMargin = new Padding(75, 120, 115, 120);
Section scp=new Section();
testDocument.Sections.Add(scp);
//This is the table in the first page for the index.
Table indexTable=new Table();
scp.Blocks.Add(indexTable);
//Insert a PageBreak to star in the next page.
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(new Span(FormattingSymbolLayoutBox.PAGE_BREAK));
//Before insert this paragraph i need to know the pages of the document to generate the index of this paragraph
//¿Is impossible?
indexTable.add(new TableRow()); // Code to add the row to the table with the index for paragraph
//calling the function to know the actual number of pages of the document.
scp.Blocks.Add(paragraph);
//Add text and paragraph
Paragraph paragraph2 = new Paragraph();
paragraph2.Inlines.Add("A lot of text, .......................... "));
//Before insert this paragraph i need to know the pages of the document to generate the index of this paragraph
//¿Is impossible?
indexTable.add(new TableRow()); // Code to add the row to the table with the indexfor paragraph.
//calling the function to know the actual number of pages of the document.
scp.Blocks.Add(paragraph);
//And do the same with more paragraph.
when finish inserting all the paragraph to the document, i copy all the blocks of this testdocument to the editor. document.
I starting thinking that is impossible to know the real page number of a paragraph in a inside a RadDocuemnt.
Any sugestion??
I need this.
Thanks very much.
Roberto.
I am not sure I got your case right.
Please find attached a demo that illustrates how you can programmatically add content to RadRichTextBox and getting the current page number of a document element.
Let us know if it is not what you had in mind or if you need further assistance.
Iva
the Telerik team

My problem is that.
If i change this function for example:
private RadDocument CreateCodeBehindDocumentWithTable()
{
RadDocument document = new RadDocument();
document.LayoutMode = DocumentLayoutMode.Paged;
document.ParagraphDefaultSpacingAfter = document.ParagraphDefaultSpacingBefore = 0;
document.PageViewMargin = new SizeF(10, 10);
document.SectionDefaultPageMargin = new Padding(75, 120, 115, 120);
Section section = new Section();
document.Sections.Add(section);
for (int x=0;x<6;x++)
section.Blocks.Add(CreateTable());
Table table = CreateTable();
Span sp = new Span();
Paragraph p = ((Paragraph)table.Rows.Last.Cells.Last.Blocks.Last);
foreach(DocumentElement de in p.Children)
{
if (de.GetType() == typeof(Span))
sp = ((Span)de);
}
section.Blocks.Add(table);
int rr = GetPageNumberForDocumentElement(sp);
sp.Text = "Page number on creation for this span in last row and cell : " + rr.ToString();
document.ShowFormattingSymbols = true;
return document;
}
You can see that when the document is open in the last table, row and cell, the text is changed, and the page number is 1, but really is 3 on the document.
I need to know the pagenumber of a element in a raddocument that are not linked to a RadRichTextBox.
I am using the view model structure, and i do the next.
I have the GenerateDocumentView.xaml and the GenerateDocumentViewModel.cs.
On the viewModel i have a Property called Document. This property is a String.
private string _Document;
public string Document
{
get { return _Document; }
set
{
if (value != _Document)
{
_Document = value;
OnPropertyChanged(() => this.Document);
}
}
}
Then i the viewModel, when is loaded i generate de RadDocument, and parse it to a string with the xamlprovider.
public override void OnNavigatedTo(Microsoft.Practices.Prism.Regions.NavigationContext navigationContext)
{
XamlFormatProvider xmlFP=new XamlFormatProvider();
//The createDocument function generate a RadDocument. In this part i generate all
// the radDocument, and is where i need to know the pagenumber of a element, to
// append it to a initial index.
this.Document = xmlFP.Export(GenerateOfferDocument.createDocument(ObjectToGenerate));
}
To bind the Document property of the viewmodel to the view i use this code in the view file.
<
telerikXaml:XamlDataProvider
x:Key
=
"DocumentProvider"
RichTextBox
=
"{Binding ElementName=editor}"
Xaml
=
"{Binding Path=Document,Mode=TwoWay}"
/>
<
KeylandControls:RichTextBox
Loaded
=
"UserControl_Loaded"
Grid.Row
=
"1"
DocumentLayoutModeChanged
=
"editor_DocumentLayoutModeChanged"
x:Name
=
"editor"
AllowDrop
=
"True"
Drop
=
"editor_Drop"
/>
Then, the view model generate the xaml string, and the xamlDataProvider, regenerates de RadDocument, and bind it into the Richtextbox called editor using the property Document.
When the document property is changed on the viewmodel, the editor shows the change, and when the text in the richtextbox is changed, the property Document of the viewModel is updated too.
Then, I dont have acces to the radDocument directly in the richtextbox, only to a RadDocument generated with the XamlFormatProvider.
Is posible to solve this issue?
You know other way to bind instead of the XamlFormat, the RadRichTextBox ?
I don't know if i can generate programability all the structure of the Grid Layout, the RadRichtextBox, and then acces to the raddocument.
Any Sugestion??
Thanks for all.

RadRichTextBox rRTB = new RadRichTextBox();
RadDocument document = rRTB.Document;
But not works. I dont know if the RadRichTextBox need to be children of something, or need to inicializate more properties
You should have no problems working with RadDocument only, without RadRcihTextBox. In order to apply the paging you should set the LayoutMode to Paged:
document.LayoutMode = DocumentLayoutMode.Paged;
document.Measure(RadDocument.MAX_DOCUMENT_SIZE);
document.Arrange(
new
RectangleF(PointF.Empty, document.DesiredSize));
Greetings,
Mike
the Telerik team

Thanks.
You are right, exporting the page numbers/headers and footers does not work with the current version of the control. The application showed only how you can add them visually to the control, but import and export of headers and footers will not be possible until they are natively implemented.
We have them scheduled for Q2 2011 and if all goes according to plan, some time in mid-July there should be headers, footers and page numbers with import and export.
I hope that answers your question.
Greetings,
Iva
the Telerik team

Unfortunately there is no easy way to accomplish proper printing of a RadRichTextBox just by setting it to the PageVisual property. It will look more like a screenshot, with one page visible and scrollbars showing.
RadRichTextBox has an implementation for both Native and HTML printing. The only requirement is that the control is added to the visual tree. You can then invoke the native printing as follows:
this
.richTextBox.Print(
"test"
, PrintMode.Native);
I hope this helps.
Iva
the Telerik team

Unfortunately there is no workaround at this point.
The good news is that considering our progress on this feature, we will probably be able to include the headers and footers in the Beta some time in mid-June. Even if we did not manage to implement their import and export by then, we might be able to squeeze in the native printing.
Iva
the Telerik team