I need to find paragraph breaks and list breaks in the page and count number of lines before the page break. Then if not more than 5 lines left in the first page I don’t want to break that paragraph or list items. Could you please help me to build a solution for that??
To ensure a timely and accurate response, please attach the following resources here for testing:
Your input word document .
Please attach the expected output word document file that shows the desired behavior.
As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.
PS: To attach these resources, please zip and upload them.
@nethmi MS Word has a similar feature - Windows/Orphan Control, that prevents a single line of the paragraph from being displayed on a separate page from the remaining content at display time by moving the line onto the following page. Also, this option ensures that a consumer shall move the single line to the following page to prevent having one line on its own page.
But in your case you need “5 lines” Windows/Orphan Control. Unfortunately, there is option to control how many lines are allows to be on the separate page. So the only possible solution is to put a manual page break. It is quite difficult to work with document layout, since MS Word documents are flow documents and does not contain any information about document layout. the consumer application builds document layout on the fly.
Aspose.Words provides LayoutCollector and LayoutEnumerator classes which allow to work with document layout. In you case it is required split Run nodes in the paragraph into parts so each Run contain only one word. You can use the following method for splitting Run nodes into parts:
private static Run SplitRun(Run run, int position)
{
Run afterRun = (Run)run.Clone(true);
run.ParentNode.InsertAfter(afterRun, run);
afterRun.Text = run.Text.Substring(position);
run.Text = run.Text.Substring((0), (0) + (position));
return afterRun;
}
Then you can insert a temporary bookmarks between Run nodes to use them as a marker for determining coordinates:
Document doc = new Document(@"C:\Temp\in.docx");
LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);
// Determine coordinates of the bookmak.
enumerator.Current = collector.GetEntity(doc.Range.Bookmarks["test"].BookmarkStart);
Console.WriteLine(enumerator.Rectangle);
Comparing Y coordinate of the temporary bookmarks gives a hint where line break is, such way you can count number of lines in the paragraph. Then you can inert a paragraph break after each 5th line and then set “Keep lines together” option in the newly created paragraph, which will forbit the paragraph from breaking across pages.
You can determine the paragraphs that should be processed by checking start and end page index of paragraphs. If they are different paragraph spans several pages:
Could you please help me to do the below widows/orphans control option?
“MS Word has a similar feature - Windows/Orphan Control, that prevents a single line of the paragraph from being displayed on a separate page from the remaining content at display time by moving the line onto the following page. Also, this option ensures that a consumer shall move the single line to the following page to prevent having one line on its own page.”
@nethmi Yes, you code is correct. You can control window/orphan control using ParagraphFormat.WidowControl property.
But note it does not allow a single line of the paragraph to be placed on the separate page.
@nethmi Your code should work properly. However, you can simplify it like this:
Document doc = new Document(@"C:\Temp\in.docx");
foreach (Paragraph p in doc.GetChildNodes(NodeType.Paragraph, true))
p.ParagraphFormat.WidowControl = true;
doc.Save(@"C:\Temp\out.docx");
Also, please attach your input and output documents here for testing? we will check the files and provide you more information.
@nethmi Thank you for additional information. As I can see Window/Orphan control option is properly enabled for all paragraphs in your output document:
So code works properly. You can also check the option is set in the document.xml:
Then why still paragraphs are broken. According to that property paragraphs will be kept together without broken. But you can see my output. all paragraphs are broken
@nethmi Window/Orphan options does not allow a single line of the paragraph to be placed on a separate page, if this option is enabled there will be at least two lines of text on a separate page.
If you need to keep all lines of paragraph on one page, you should use Keep Lines Together option. https://reference.aspose.com/words/net/aspose.words/paragraphformat/keeptogether/
Not working. I tried it with one line as well. In the word paragraph properties I can see the Window/orphan option applied. but Output is like below. paragraph was broke. I don’t know why.
@nethmi This is expected behavior of this option. Here is quote from DOCX specification regarding this option:
When displaying a paragraph in a page, it is sometimes the case that the first line of that paragraph would display as the last line on one page, and all subsequent lines would display on the following page. This property ensures that a consumer shall move the single line to the following page as well to prevent having one line on its own page. As well, if a single line appears at the top of a page, a consumer shall move the preceding line onto the following page as well, to prevent a single line from being displayed on a separate page.
Here is the example. If option is not enabled the content will look like this:
if enable this option, the same document will look like this:
1 Like
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
Enables storage, such as cookies, related to analytics.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.