@Niebelschutz In your case you can use ParagraphFormat.PageBreakBefore property of Paragraph to make the table continue from the next page. Foe example see the following code:
Document doc = new Document(@"C:\Temp\in.docx");
// Get the table
Table table = (Table)doc.FirstSection.Body.GetChild(NodeType.Table, 0, true);
bool isFirst = true;
// Loop through the rows in the table.
foreach (Row row in table.Rows)
{
if (row.FirstCell.Range.Text == "1\a")
{
if (isFirst)
{
isFirst = false;
continue;
}
row.FirstCell.FirstParagraph.ParagraphFormat.PageBreakBefore = true;
}
}
doc.Save(@"C:\Temp\out.docx");
As I can see you are using mail merge to fill your document with data. in your case you can use mail merge with regions and IFieldMergingCallback to insert page break if the data matches some condition. For example see the following modified template and the code:
DataTable dt = new DataTable("Data");
dt.Columns.Add("VDE_NUM");
dt.Columns.Add("TLN_NACHNAME");
dt.Columns.Add("TLN_VORNAME");
dt.Columns.Add("TH1_AUSPRAEGUNG");
dt.Rows.Add("1", "Antrago 9699", "Andrea", "OLG S");
dt.Rows.Add("2", "Antrago 9707", "Andrea", "OLG S");
dt.Rows.Add("3", "Antrago 9712", "Andrea", "OLG S");
dt.Rows.Add("4", "Antrago 9722", "Andrea", "OLG S");
dt.Rows.Add("5", "Antrago 9732", "Andrea", "OLG S");
dt.Rows.Add("1", "Antrago 9713", "Andrea", "OLG S");
dt.Rows.Add("2", "Antrago 9716", "Andrea", "OLG S");
dt.Rows.Add("3", "Antrago 9719", "Andrea", "OLG S");
dt.Rows.Add("4", "Antrago 9734", "Andrea", "OLG S");
dt.Rows.Add("5", "Antrago 9736", "Andrea", "OLG S");
dt.Rows.Add("1", "Antrago 9705", "Andrea", "OLG S");
dt.Rows.Add("2", "Antrago 9711", "Andrea", "OLG S");
dt.Rows.Add("3", "Antrago 9726", "Andrea", "OLG S");
dt.Rows.Add("4", "Antrago 9737", "Andrea", "OLG S");
dt.Rows.Add("5", "Antrago 9738", "Andrea", "OLG S");
Document doc = new Document(@"C:\Temp\ModifiedTemplate.docx");
doc.MailMerge.FieldMergingCallback = new FieldMergingCallback();
doc.MailMerge.ExecuteWithRegions(dt);
doc.Save(@"C:\Temp\out.docx");
private class FieldMergingCallback : IFieldMergingCallback
{
public void FieldMerging(FieldMergingArgs args)
{
if (args.FieldName == "VDE_NUM" && args.FieldValue == "1")
{
if(!mIsFirst)
args.Field.Start.ParentParagraph.ParagraphFormat.PageBreakBefore = true;
mIsFirst = false;
}
}
public void ImageFieldMerging(ImageFieldMergingArgs args)
{
// Do nothing
}
private bool mIsFirst = true;
}
ModifiedTemplate.docx (99.3 KB)
out.docx (68.2 KB)