How to position an object like Table at runtime

Hello Team,

We have created a sample to test Aspose.Words. Here the table at the end of the document cuts and runs over the next page. We need when this happens the table which is at the bottom of the page should move to next page entirely. It should not cut over the pages.

One more thing the data this table contains is dynamic. Sometime the table will fit on the same page as the data is less, if the data is more then the table will run through the next page.

How to rectify this problem?

Regards,

Hi

Thanks for your inquiry. I think you can try using “Keep with Next” option of paragraphs. For example, please see the following code:

// 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”);

I also attached the output document.

Hope this helps.

Best regards.

Hello Alexey,

Thanks for your suggestion. We tried this with
fixed amount of text data, and it works fine.

We are facing problem with the actual data which we can not measure before hand and apply paragraph keep with next. The problem lies there.

As I earlier said when the data is in measurable quantity it works, but when you do not know whether you will receive 1 page text or just one line as input as the input text will be randomly picked up based on same math calculation for scores.

Kindly suggest whether in this case it possible to move the object to the next page.

best regards,

Hi

Thanks for your request. MS Word documents are flow documents and do not contain any information about its layout into lines and pages. So there is no way to determine when page starts or ends. Also, it is impossible to determine if some content overflow the page or not.

But, I think “Keep with Next” approach should work as expected in your case, you just should enable this option only for a table and disable it once the table ends.

Best regards.

Hello,

Thank you for your quick reply & suggestion.

As per Your suggestion using “Keep with next” will take each new table onto new page which is not matching to our requiremnets.

Our requirements are as follows.

1> If the page contain enough amount of space then table should be drawn on same page.

2> but if the table is not entirely fit into that single page it will automatically get drwan on next page.

Since it is difficult for us to find end of document during printing process.Is there any alternate way to achieve this ?

Is there any workaround solution for this ?

Waiting for your reply.

Thanks & Regards,

Dwarika.

Hi Dwarika,

Thanks for your request. Could you please provide me simple code or create simple application, which will allow me to reproduce the problem on my side. I will check the issue and provide you more information.

Best regards.

Hello,

The sample code is attached with this post.

The amount of data coming from outside is not fixed. since it is impossible for us to find the end of page during priting process.

It can be larger than page size or smaller than page size.

if it is samller than page size then it will get printed on same page or if it is larger than page size it will get printed on next page.

The data should not get divided into pages.( For e.g. In case of table if it small then it will get printed on same page or if it do not fit into same page then entire table will get printed automatically on next page.The table should not get divided into pages.)

Is it possoble through aspose to find wheather end of page is ocuured during printing process?

Thanks & Regards,

Dwarika.

Hi Dwarika,

Thank you for additional information. As I can see, the table is placed in the second page and is not divided into pages. Please attach also your output document and expected output.

Best regards.

Hello,

As i said in my earlier post the problem is explained again with some reference code & sample document.

I am currently using

Aspose word version 6.2.0.0

Aspose PDF 5.9.0.0

Please find the attached copy of word document.

In attached document there are 2 sections #1 & #2. For both section there is some text available at initial place & then table object is drawn.The size of table object is coming from outside as input parameter from user but for better understanding of problem & sample coding it is put as fixed or static.

Senario#1

After printing initial text, i have to print a table, the size of table is 4*4 which small as compare to page size or which do not goes beyond the current page so it is printed on same page.

Scenario #2

Here now the size of table is large.

So the table is started printing on same page & get divided into page 2 & 3

So my requirement is that i want intial text to be printed on 2 page but table should start printing on 3 rd page instead of 2

So the entire table will grab 3 page.

But i want this course of action to be done dynamically by sensing that wheather the end of page is ocurred or not ( i do not want to use any explicit pagebreak before or after table to achieve this.)

Is it possible for us to sense the end of page at the time of printing?

or

is there any alternative way to achieve this?

the sample code for this is as follows.

//path for license directory.

string path = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);

//Initialize Aspose license.

Aspose.Words.License lic = new Aspose.Words.License();

lic.SetLicense(path + @"\Aspose.Total.lic");

Aspose.Pdf.License lic1 = new Aspose.Pdf.License();

lic1.SetLicense(path + @"\Aspose.Total.lic");

//Create document & document builder object.

Document doc = new Document();

DocumentBuilder documentBuilder = new DocumentBuilder(doc);

//Start of Scanario #1

documentBuilder.Font.Bold = true;

documentBuilder.Writeln(“Scenario #1: Table Fit into Page”);

documentBuilder.Font.Bold = false;

documentBuilder.InsertBreak(BreakType.LineBreak);

//Some text available on page.

documentBuilder.Writeln(“At the Nokia World 09 underway in Stuttgart, Nokia seems to be telling the world that it STILL is the world’s number one mobile phone manufacturer even though its dominance might have been challenged by relative new comers.”);

documentBuilder.Write(“Whenever we say newcomer, the first thing that comes to the mind is undoubtedly, Apple. When Apple introduced the iPhone back in 2007, it was a revolutionary device right from the design to its UI”);

documentBuilder.Write("Two years after its introduction, many devices have tried to be an iPhone killer but none of them really managed to cause any dent into the sales of the iPhone, let alone kill it. However, by 2009 ");

documentBuilder.Write(“we have a relative flood of new devices that are actually capable of doing things way better than the iPhone and they look good whilst doing it as well, a key iPhone feature! So, we now have the Android with the Sense UI on HTC”);

documentBuilder.Write(" Palm with its extremely slick WebOS and Nokia with Series 60 Version 5! What? Series 60? ");

//Clear the paragraph formatting for new paragraph.

documentBuilder.InsertBreak(BreakType.ParagraphBreak);

documentBuilder.ParagraphFormat.ClearFormatting();

documentBuilder.InsertBreak(BreakType.LineBreak);

//Print text in BOLD format.

documentBuilder.Font.Bold = true;

documentBuilder.Writeln(“Table fit into remaining Page”);

documentBuilder.StartTable();

//Creating table with 4 * 4 matrix.

documentBuilder.Writeln(“Table with Only 4 rows & 4 cols”);

documentBuilder.Font.Bold = false;

//Initialise the table parameter.

//Consider that row & col data is not fix or static

//It is coming from outside.( so it is dynamic )

//Based on these two parameters the size of table will get configured.

//Lets assume the input is 4*4 table from outside.

int row =4;

int col =4;

//Printing exact table.

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

{

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

{

documentBuilder.InsertCell();

}

documentBuilder.EndRow();

}

documentBuilder.EndTable();

//Insert page break to go to next page.

documentBuilder.InsertBreak(BreakType.PageBreak);

documentBuilder.ParagraphFormat.ClearFormatting();

//Start of Scenario #2

documentBuilder.Font.Bold = true;

documentBuilder.Writeln(“Scenario #2: Table do not fit into Page & divide into pages.”);

documentBuilder.Font.Bold = false;

documentBuilder.InsertBreak(BreakType.LineBreak);

//Some text available at start.

documentBuilder.Writeln(“At the Nokia World 09 underway in Stuttgart, Nokia seems to be telling the world that it STILL is the world’s number one mobile phone manufacturer even though its dominance might have been challenged by relative new comers.”);

documentBuilder.Write(“Whenever we say newcomer, the first thing that comes to the mind is undoubtedly, Apple. When Apple introduced the iPhone back in 2007, it was a revolutionary device right from the design to its UI”);

documentBuilder.Write("Two years after its introduction, many devices have tried to be an iPhone killer but none of them really managed to cause any dent into the sales of the iPhone, let alone kill it. However, by 2009 ");

documentBuilder.Write(“we have a relative flood of new devices that are actually capable of doing things way better than the iPhone and they look good whilst doing it as well, a key iPhone feature! So, we now have the Android with the Sense UI on HTC”);

documentBuilder.Write(" Palm with its extremely slick WebOS and Nokia with Series 60 Version 5! What? Series 60? ");

//Clear the paragraph formatting for new paragraph.

documentBuilder.InsertBreak(BreakType.ParagraphBreak);

documentBuilder.ParagraphFormat.ClearFormatting();

documentBuilder.InsertBreak(BreakType.LineBreak);

//Print text in BOLD format.

documentBuilder.Font.Bold = true;

documentBuilder.Writeln(“Table do not fit into remaining page”);

documentBuilder.StartTable();

//creating table with 50 * 4 matrix.

documentBuilder.Writeln(“Table with Only 50 rows & 4 cols”);

documentBuilder.Font.Bold = false;

//Table formation.

int newRow = 50;

int newCol = 4;

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

{

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

{

documentBuilder.InsertCell();

}

documentBuilder.EndRow();

}

documentBuilder.EndTable();

//Save document & open into aspose word.

doc.Save(“Sample.doc”);

Waiting for your reply ASAP.

Thanks & Regards,

Dwarika

Hi Dwarika,

Thank you for additional information, As I already mentioned earlier, there is no way to determine where page starts or ends using Aspose.Words. You can leave your comments and suggestions regarding this feature here:

https://blog.aspose.com/2008/12/01/where-on-a-page-is-my-paragraph

Best regards.

Hello Team,

In continuation with our post, we want to provide you some more information on Paragraph formatting we used in our code.

We need that when ever the text is spanning across more than two pages….(and we actually want that text to fit in first page or it should move to second page automatically)…we used following parameters one by one,

KeepTogether - True if all lines in the paragraph are to remain on the same page.

KeepWithNext - True if the paragraph is to remains on the same page as the paragraph that follows it.

PageBreakBefore - True if a page break is forced before the paragraph.

WidowControl - True if the first and last lines in the paragraph are to remain on the same page as the rest of the paragraph.

We tried with KeepTogether , KeepWithNext , WidowControl one by one, but there is no difference in the output.

The above we found from Your suggestions are awaited. Determine available space on current page

Attached output for your reference. ExpectedWordDoc.doc is the one we want to create at runtime. WordDoc.doc is one where we can not see any difference in spite of setting above mentioned parameters.

Regards,
Dwarika

Hi Dwarika,

Thank you for additional information. You can achieve what you need by setting AllowBreakAcrossPages option of table row to false. For example see the following code and attached document.

// Create document and DocumentBuidler.

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

// Here we insert some content before table.

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

builder.Writeln(“This is some text before table”);

// Here we insert table.

builder.InsertCell();

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

builder.CellFormat.Width = 500;

builder.RowFormat.AllowBreakAcrossPages = false;

// Insert some text into the table cell.

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

builder.Writeln(“This is some text in the table”);

builder.EndRow();

builder.EndTable();

// Save output document.

doc.Save(@“Test001\out.doc”);

Hope this helps.

Best regards.

Thanks very much for your reply. We have tried with the suggestion and it works well with Table Objects.

We have one more query.

Now what is happening is; if the first object is Text (Plain Text) and second object is Image on the page, if there is some content already present on the page, the Text object remains on the current page while Image object goes to second page.

We want both these objects either fit on first page or move on to next page.

How can we achieve this. Awaiting your reply.

Regards,

Dwarika

Hi Dwarika,

Thanks for your request. There can be two options:

  1. Text and image is in the same paragraph. In such case, I think, you can use “Keep Lines Together” option to achieve what you need.

  2. Text and image is in different paragraphs. In this case, “Keep with Next” option should help you.

Anyways, I think, you should experiment with “Keep Lines Together” and “Keep with Next” options.

Best regards.

Hello,

  1. Text and image is in different paragraphs. In this case, “Keep with Next” option should help you.

As per your suggestion to use “Keep with Next” for Text and Image in a different paragraph, we tried using but still it is not working for us as expected.

Attached code sample, actual and expected output for your reference.

Regards, Dwarika

Hi Dwarika,

Thank you for additional information. “Keep with Next” option does not work in your case because there is only one paragraph in your document with LineBreaks.

Best regards,

Thanks for your reply.

If you take a look at sample code; before inserting image I have used following statements,

documentBuilder.InsertBreak(BreakType.LineBreak); - for a single line between the text and the image.

documentBuilder.InsertBreak(BreakType.ParagraphBreak); - for a new paragraph to start

I suppose BreakType.ParagraphBreak will actually create a new paragraph. Therefore now in my Sample code I have two paragraphs one with text and other with Image. And also I am using “keep with next” on first paragraph which will enable second paragraph content to remain with the first paragraph.

Please take a look at sample code one more time and let us know.

Regards, Dwarika

Hi Dwarika,

Thank you for additional information. I see that in your code, but if you take a look at your document, you will see that there is only one paragraph. Maybe you have attached wrong document.

Also, there is no sense in “Keep with Next” option if set it for all paragraphs. As you understand, it is impossible to keep all paragraphs on one page (some times there is just no enough space).

Also, I wondering if you really need to keep so long text on the same page with and image. I suppose in general, you need to keep only short description of an image and images on the same page. In this case, “Keep with Next” option will satisfy your needs.

Best regards,

Hello Alexey,

Below is the same code posted to Aspose on 30 Sept 2010.

This sample code does contain ParagraphBreak before inserting image into the document. Please take a look at below code

string path = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);

//Initialize Aspose license. Aspose Word version 9.0.0.0

Aspose.Words.License lic = new Aspose.Words.License();

lic.SetLicense(path + @"\Aspose.Total.lic");

Aspose.Pdf.License lic1 = new Aspose.Pdf.License();

lic1.SetLicense(path + @"\Aspose.Total.lic");

Document doc = new Document();

DocumentBuilder documentBuilder = new DocumentBuilder(doc);

Run run;

Aspose.Words.Font font;

//set the page set-up

PageSetup pageSetup = documentBuilder.CurrentSection.PageSetup;

pageSetup.PaperSize = PaperSize.A4; //set the page size.

documentBuilder.ParagraphFormat.ClearFormatting();

//set font

run = new Run(doc);

font = run.Font;

#region Code for Text and Image

//documentBuilder.ParagraphFormat.KeepTogether = true;

documentBuilder.ParagraphFormat.SpaceBefore = 50;

documentBuilder.ParagraphFormat.KeepWithNext = true;

documentBuilder.Font.Size = 14;

documentBuilder.Font.Name = “Arial”;

documentBuilder.ParagraphFormat.LeftIndent = 20;

documentBuilder.InsertHtml(“That is a lot of dosh, and when added to a long term incentive plan of five times annual salary, could amount to total annual reward as they say in the remuneration business, of as much as £11m. Blimey”);

documentBuilder.InsertHtml(“That is a lot of dosh, and when added to a long term incentive plan of five times annual salary, could amount to total annual reward as they say in the remuneration business, of as much as £11m. Blimey”);

documentBuilder.InsertHtml(“That is a lot of dosh, and when added to a long term incentive plan of five times annual salary, could amount to total annual reward as they say in the remuneration business, of as much as £11m. Blimey”);

documentBuilder.InsertHtml(“That is a lot of dosh, and when added to a long term incentive plan of five times annual salary, could amount to total annual reward as they say in the remuneration business, of as much as £11m. Blimey”);

documentBuilder.InsertHtml(“That is a lot of dosh, and when added to a long term incentive plan of five times annual salary, could amount to total annual reward as they say in the remuneration business, of as much as £11m. Blimey”);

documentBuilder.InsertBreak(BreakType.LineBreak);

documentBuilder.InsertBreak(BreakType.LineBreak);

documentBuilder.InsertHtml(“That is a lot of dosh, and when added to a long term incentive plan of five times annual salary, could amount to total annual reward as they say in the remuneration business, of as much as £11m. Blimey”);

documentBuilder.InsertHtml(“That is a lot of dosh, and when added to a long term incentive plan of five times annual salary, could amount to total annual reward as they say in the remuneration business, of as much as £11m. Blimey”);

documentBuilder.InsertHtml(“That is a lot of dosh, and when added to a long term incentive plan of five times annual salary, could amount to total annual reward as they say in the remuneration business, of as much as £11m. Blimey”);

documentBuilder.InsertHtml(“That is a lot of dosh, and when added to a long term incentive plan of five times annual salary, could amount to total annual reward as they say in the remuneration business, of as much as £11m. Blimey”);

documentBuilder.InsertHtml(“That is a lot of dosh, and when added to a long term incentive plan of five times annual salary, could amount to total annual reward as they say in the remuneration business, of as much as £11m. Blimey”);

documentBuilder.InsertBreak(BreakType.LineBreak);

documentBuilder.InsertBreak(BreakType.ParagraphBreak);

documentBuilder.InsertImage(@“D:\Projects\CreateMultiCandidateMOO\CreateMultiCandidateMOO\bin\Debug\nature.jpg”);

#endregion

doc.Save(“Test29Sep.doc”);

StartWord(“Test29Sep.doc”);

 
So when we set documentBuilder.ParagraphFormat.KeepWithNext = true; on the first paragraph and we introduce new paragraph to insert image, the image neither fitting on first page nor the entire thing “Text+mage” are moving to second page.
Please comment.
Regards,
Dwarika
 

            

Hi Dwarika,

Thanks for your request. You should note that in this case you have a lot of content on the page and then insert an image. If the image is quite large and cannot be placed on the same page with text it is pushed to the next page. Also, could you please answer the question I asked in my previous answer – Do you really need to keep so long text on the same page with and image? If so, why do you need this? If image does not fit the available space on the page, what would you like to do, crop the image or move it to the next page?

Best regards,