问题
From aspx page, I dynamically add paragraphs to a word document using OpenXml SDK. In this case, a page break within a paragraph is not allowed. So in case a paragraph starts in middle of page 1 and extends to page2 then it should actually start in Page2. However, if it ends in the same page it is okay.
How to achieve this? Is there a way to set in th document that page break is not allowed within a paragraph? Any input is highly appreciated.
回答1:
In general you can not use the open xml sdk to determine where in a page an element will be displayed, since open xml has no concepts of pages.
Pages are determined by the client application consuming the open xml document. You can however specify that a paragraph's lines are kept together.
<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:pPr>
<w:keepLines />
</w:pPr>
<w:bookmarkStart w:name="_GoBack" w:id="0" />
<w:r>
<w:lastRenderedPageBreak />
<w:t>Most controls offer a choice of using the look from the current theme or using a format that you specify directly. To change the overall look of your document, choose new your document.</w:t>
</w:r>
<w:bookmarkEnd w:id="0" />
</w:p>
w:keepLines in the above examples paragraph properties is the key to making sure a paragraph is not split up between pages, below is the open xml required to generate the above paragrpah:
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml;
namespace GeneratedCode
{
public class GeneratedClass
{
// Creates an Paragraph instance and adds its children.
public Paragraph GenerateParagraph()
{
Paragraph paragraph1 = new Paragraph();
ParagraphProperties paragraphProperties1 = new ParagraphProperties();
KeepLines keepLines1 = new KeepLines();
paragraphProperties1.Append(keepLines1);
BookmarkStart bookmarkStart1 = new BookmarkStart(){ Name = "_GoBack", Id = "0" };
Run run1 = new Run();
LastRenderedPageBreak lastRenderedPageBreak1 = new LastRenderedPageBreak();
Text text1 = new Text();
text1.Text = "Most controls offer a choice of using the look from the current theme or using.";
run1.Append(lastRenderedPageBreak1);
run1.Append(text1);
BookmarkEnd bookmarkEnd1 = new BookmarkEnd(){ Id = "0" };
paragraph1.Append(paragraphProperties1);
paragraph1.Append(bookmarkStart1);
paragraph1.Append(run1);
paragraph1.Append(bookmarkEnd1);
return paragraph1;
}
}
}
来源:https://stackoverflow.com/questions/8165115/openxml-how-to-identify-whether-the-paragraph-extends-to-next-page