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

Finding Page Number Containing Control

14 Answers 324 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Jim
Top achievements
Rank 1
Jim asked on 22 Jan 2011, 04:08 PM
Hello there, I was wondering if anyone knew a quick way of taking an element, say a paragraph, section or span, and then determining the page number that this element is sitting on when the document is working in paged mode.  I basically have a need to create a table of contents so it would be useful for me to be able to find all the page breaks in my document and use this to dynamically produce one.  If anyone knows a way to achieve such a thing I would be most appreciative.

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

Sort by
0
Iva Toteva
Telerik team
answered on 27 Jan 2011, 02:08 PM
Hi Jim,

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
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Rob
Top achievements
Rank 1
answered on 07 Feb 2011, 06:35 PM
Hi.

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.

 

0
Iva Toteva
Telerik team
answered on 10 Feb 2011, 07:38 PM
Hi Rob,

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
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Rob
Top achievements
Rank 1
answered on 14 Feb 2011, 12:00 PM
Hi again.

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.
0
Iva Toteva
Telerik team
answered on 17 Feb 2011, 02:04 PM
Hello 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.

Best wishes,
Iva
the Telerik team
0
Rob
Top achievements
Rank 1
answered on 17 Feb 2011, 05:09 PM

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.

0
Rob
Top achievements
Rank 1
answered on 17 Feb 2011, 05:34 PM
I am tralling to do that with the rad Document.

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
0
Mike
Telerik team
answered on 22 Feb 2011, 11:25 AM
Hello Rob,

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;
Then when you have finished changing the document structure you have to measure and arrange it in order to get the elements flow on different pages:
document.Measure(RadDocument.MAX_DOCUMENT_SIZE);
document.Arrange(new RectangleF(PointF.Empty, document.DesiredSize));
Was this what you needed? Let us know if this helped.

Greetings,
Mike
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Tomas
Top achievements
Rank 1
answered on 08 May 2011, 08:17 AM
Hi, I tried your SilverlightApplication1 project attached in this thread.  This project adds a header and footer into RichEditbox as user controls.  They work fine until trying to export to PDF where they dont show up in the exported document.  Is there anything I can do to make header and footer show up when exporting to PDF?
Thanks.
0
Iva Toteva
Telerik team
answered on 09 May 2011, 04:56 PM
Hello Tomas,

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
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Angeli
Top achievements
Rank 1
answered on 23 May 2011, 10:31 AM
i'm using the native printing where i print the PageVisual element but the output isn't what i wanted. is there any way for the Paged layout of the RadRichTextBox to be preserved but without the blue background when exported? i'm using the CodeBehindDocument.zip demo
0
Iva Toteva
Telerik team
answered on 26 May 2011, 05:49 PM
Hi Angeli,

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.


All the best,
Iva
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Angeli
Top achievements
Rank 1
answered on 27 May 2011, 04:27 AM
I would want the headers & footers to appear on every page.. is there any temporary solution to this until the next release?
0
Iva Toteva
Telerik team
answered on 01 Jun 2011, 07:43 AM
Hi Angeli,

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.

Best wishes,
Iva
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
RichTextBox
Asked by
Jim
Top achievements
Rank 1
Answers by
Iva Toteva
Telerik team
Rob
Top achievements
Rank 1
Mike
Telerik team
Tomas
Top achievements
Rank 1
Angeli
Top achievements
Rank 1
Share this question
or