tables with no page breaks

Last post 12-31-2009, 9:39 AM by alexey.noskov. 5 replies.
Sort Posts: Previous Next
  •  12-29-2009, 11:49 AM 214962

    tables with no page breaks .NET

    Hello.

    I need to use Aspose.Words to create some variable length text (coming from a database) and then a table having no page breaks in the middle of the table. Is there a way to calculate how much space is left on the page and calculate how much space my table will take so I will know if I have to force a page break?

    Thanks.

    Denise

     
  •  12-29-2009, 1:31 PM 214973 in reply to 214962

    Re: tables with no page breaks

    Attachment: Present (inaccessible)

    Hi

     

    Thanks for your request. MS Word document is flow document and does not contain any information about its layout into lines and pages. So there is no way to determine where page starts or ends and determine how much free space is available on the page.

    However, to achieve what you need you do not need to know how much space is available on page and how much space a table will take. I think, it will be enough to use “Keep With Next” option of paragraphs within a table. In this case, if there is not enough space on page for a table, the whole table will go to the next page. For instance, see the attached document.

     

    Best regards.


    Alexey Noskov
    Developer/Technical Support
    Aspose Auckland Team
     
  •  12-30-2009, 2:50 PM 215145 in reply to 214973

    Re: tables with no page breaks

    Thank you for your response.

    Can you possibly show me how to do this programmatically?

    I do not know if I should use the DocumentBuilder feature (such as builder.InsertCell()) or if I should use the Document, Section, Body, Paragraph, Run DOM features.

    Can you please explain this?

    Thank you.

    Denise

     
  •  12-31-2009, 2:06 AM 215202 in reply to 215145

    Re: tables with no page breaks

    Hi

     

    Thanks for your request. Here is a simple example:

     

    // Create empty docuemnt and DocumentBuilder

    Document doc = new Document();

    DocumentBuilder builder = new DocumentBuilder(doc);

     

    // Insert few paragraphs at the beggining of the document

    for (int i = 0; i < 20; i++)

        builder.Writeln("This is paragraphs for testing");

     

    // Enable "Keep with Next option"

    builder.ParagraphFormat.KeepWithNext = true;

     

    // Build some table

    builder.CellFormat.Width = 200;

    builder.CellFormat.Borders.LineStyle = LineStyle.Single;

    builder.CellFormat.Borders.Color = Color.Black;

    for (int i = 0; i < 30; i++)

    {

        for (int j = 0; j < 2; j++)

        {

            builder.InsertCell();

            builder.Write("This is test content of the table");

        }

        builder.EndRow();

    }

    builder.EndTable();

     

    // Save output document

    doc.Save(@"Test001\out.doc");

     

    Hope this helps.

     

    Best regards.


    Alexey Noskov
    Developer/Technical Support
    Aspose Auckland Team
     
  •  12-31-2009, 8:53 AM 215304 in reply to 215202

    Re: tables with no page breaks

    Thank you very much for your response. If the table needs to take more than one page, how can I repeat the header columns? I have tried this, following your example, but I can't get it working.

    Document doc = new Document();

    DocumentBuilder builder = new DocumentBuilder(doc);

    // Insert few paragraphs at the beggining of the document

    for (int i = 0; i < 20; i++)

    builder.Writeln("This is paragraphs for testing");

    // Enable "Keep with Next option"

    builder.ParagraphFormat.KeepWithNext = true;

    // Build some table

    builder.CellFormat.Width = 200;

    builder.CellFormat.Borders.LineStyle = LineStyle.Single;

    builder.CellFormat.Borders.Color = Color.Black;

    builder.InsertCell();

    builder.Write("Header Col1");

    builder.InsertCell();

    builder.Write("Header Col2");

    builder.EndRow();

    for (int i = 0; i < 100; i++)

    {

    for (int j = 0; j < 2; j++)

    {

    builder.InsertCell();

    builder.Write("This is test content of the table");

    }

    builder.EndRow();

    }

    if (builder.ParagraphFormat.PageBreakBefore == true)

    {

    builder.InsertCell();

    builder.Write("Header Col1");

    builder.InsertCell();

    builder.Write("Header Col2");

    builder.EndRow();

    }

    builder.EndTable();

     

    Thanks.

    Denise

     
  •  12-31-2009, 9:39 AM 215310 in reply to 215304

    Re: tables with no page breaks

    Hi

     

    Thanks for your inquiry. You should use HeadingFormat property. Please see the following link for more information,

    http://www.aspose.com/documentation/.net-components/aspose.words-for-.net-and-java/aspose.words.tables.rowformat.headingformat.html

     

    Also here is code example:

     

    //Create document and DocumentBuilder

    Document doc = new Document();

    DocumentBuilder builder = new DocumentBuilder(doc);

     

    //Create heading row that will be repeated on each page

    builder.RowFormat.HeadingFormat = true;

    builder.Bold = true;

    for (int i = 0; i < 5; i++)

    {

        builder.InsertCell();

        builder.Write(string.Format("heading_{0}", i));

    }

    builder.EndRow();

    builder.RowFormat.HeadingFormat = false;

    builder.Bold = false;

     

    //Generate body of table

    for (int rowIdx = 0; rowIdx < 100; rowIdx++)

    {

        for (int colIdx = 0; colIdx < 5; colIdx++)

        {

            builder.InsertCell();

            builder.Write(string.Format("value_of_{0}_{1}", rowIdx, colIdx));

        }

        builder.EndRow();

    }

    builder.EndTable();

     

    //Save output document

    doc.Save("out.doc");

     

    I hope this could help you.

     

    Best regards.


    Alexey Noskov
    Developer/Technical Support
    Aspose Auckland Team
     
View as RSS news feed in XML