none
How to find out where are we on a page while creating it? RRS feed

  • Question

  • Hi,

    I'm creating a docx document based on user input, but I'm struggling with tables and it's headers since they break accross pages at 'undisireable' places (e.g. immediatelly after the table heading).

    Is there some way that I can find out where I'm at while generating document? Or (which would be even better) to specify how many rows should always go with the row that is table heading - if it can't fit that much, then it should transition to the next page.

     

    I am aware that this is just a document - it doesn't know how many pages are there, doesn't deal with formatting, how many rows does a paragraph take, etc, but is there some way (any way) that I can figure this out? Maybe there's some 'dummy' formatter that would provide such info? All of the information is there - page size, margins, formatting, etc... it seems unbelieveable that it would be that hard to get this info, yet it is...

    Friday, March 4, 2011 4:04 PM

Answers

  • Thanks Bruce!

     

    Actually, the page count is set only when opened in Word - he's the one who sets it based on it's formatting ("rendering") of the docx. So it's not viable in code while we're creating the page.

    As for my original problem, I solved it by using KeepNext property (quite nice actually, didn't know it could apply to tables as well). It does exactly what I need - set that on a row to 'glue' it to the next row. Great!

    As I've researched, there's actually no 'word previewer' that would get me the info I need ('where are we on a page')  - except ofcourse, creating it myself :).

     

    Thanks, 

    Veljko

    • Marked as answer by Bruce Song Friday, March 11, 2011 9:26 AM
    Friday, March 11, 2011 9:20 AM
  • Hi Veljkoz,

    Thank you for posting and we are glad to help with you.

    >>Or (which would be even better) to specify how many rows should always go with the row that is table heading - if it can't fit that much, then it should transition to the next page.

    I think you can compare the table heading height with word page size, and then decide whether to transition to the next page.

    >>how many pages are there, doesn't deal with formatting, how many rows does a paragraph take

    You can get the number of pages via Pages property. Take a look at this thread: http://social.msdn.microsoft.com/Forums/en-NZ/oxmlsdk/thread/d1d31ad5-4ce3-49db-801e-b81c59281572

    >> All of the information is there - page size, margins, formatting, etc... it seems unbelieveable that it would be that hard to get this info, yet it is...

    As far as I know, you can get page size and margin via the following code:

            MainDocumentPart mainDocument = wordDoc.MainDocumentPart;
            Body body = mainDocument.Document.Body;
            body.FirstOrDefault();
            SectionProperties secPro = body.Elements<SectionProperties>().First();
            PageSize size = secPro.Elements<PageSize>().First();
            PageMargin margin = secPro.Elements<PageMargin>().First();
            Console.WriteLine(size.Width + "," + size.Height);
            Console.WriteLine(margin.Left +","+margin.Top +","+margin.Right +","+margin.Bottom);
    
    

    As for formatting, which format would you want to get. Please take a look at this MSDN article about processing word document via open xml: http://msdn.microsoft.com/en-us/library/ff686712.aspx

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by veljkoz2 Friday, March 11, 2011 9:20 AM
    Friday, March 11, 2011 7:33 AM

All replies

  • Hi Veljkoz,

    Thank you for posting and we are glad to help with you.

    >>Or (which would be even better) to specify how many rows should always go with the row that is table heading - if it can't fit that much, then it should transition to the next page.

    I think you can compare the table heading height with word page size, and then decide whether to transition to the next page.

    >>how many pages are there, doesn't deal with formatting, how many rows does a paragraph take

    You can get the number of pages via Pages property. Take a look at this thread: http://social.msdn.microsoft.com/Forums/en-NZ/oxmlsdk/thread/d1d31ad5-4ce3-49db-801e-b81c59281572

    >> All of the information is there - page size, margins, formatting, etc... it seems unbelieveable that it would be that hard to get this info, yet it is...

    As far as I know, you can get page size and margin via the following code:

            MainDocumentPart mainDocument = wordDoc.MainDocumentPart;
            Body body = mainDocument.Document.Body;
            body.FirstOrDefault();
            SectionProperties secPro = body.Elements<SectionProperties>().First();
            PageSize size = secPro.Elements<PageSize>().First();
            PageMargin margin = secPro.Elements<PageMargin>().First();
            Console.WriteLine(size.Width + "," + size.Height);
            Console.WriteLine(margin.Left +","+margin.Top +","+margin.Right +","+margin.Bottom);
    
    

    As for formatting, which format would you want to get. Please take a look at this MSDN article about processing word document via open xml: http://msdn.microsoft.com/en-us/library/ff686712.aspx

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by veljkoz2 Friday, March 11, 2011 9:20 AM
    Friday, March 11, 2011 7:33 AM
  • Thanks Bruce!

     

    Actually, the page count is set only when opened in Word - he's the one who sets it based on it's formatting ("rendering") of the docx. So it's not viable in code while we're creating the page.

    As for my original problem, I solved it by using KeepNext property (quite nice actually, didn't know it could apply to tables as well). It does exactly what I need - set that on a row to 'glue' it to the next row. Great!

    As I've researched, there's actually no 'word previewer' that would get me the info I need ('where are we on a page')  - except ofcourse, creating it myself :).

     

    Thanks, 

    Veljko

    • Marked as answer by Bruce Song Friday, March 11, 2011 9:26 AM
    Friday, March 11, 2011 9:20 AM
  • Hi Veljko,

    Thank you for your timely feedback.

    You are welcome.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, March 11, 2011 9:26 AM