Question

In my application I create a word document with several paragraphs. One of them is a table that might be large enough to expand on a second or third page of the document. Here is my creation code for this table:

Table oTable;
Range wrdRng = aDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
wrdRng.Bold = 0;
wrdRng.Font.Size = 9;
int inTableRows = dtRechPos.Rows.Count + 2;
oTable = aDoc.Tables.Add(wrdRng, inTableRows, 4, ref oMissing, ref oMissing);
oTable.Range.ParagraphFormat.SpaceAfter = 2;
oTable.Cell(1, 1).Range.Text = "Name, Anschrift";
oTable.Cell(1, 2).Range.Text = "Geb. Datum";
oTable.Cell(1, 3).Range.Text = "Versich. Nr.";
oTable.Cell(1, 4).Range.Text = "Betrag";
oTable.Rows[1].Range.Font.Bold = 1;
oTable.Rows[1].Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;
oTable.Rows[1].Borders.OutsideLineWidth = WdLineWidth.wdLineWidth050pt;

int curRow = 1;

String sqlMwstProzent = "SELECT PROZENT FROM MWST WHERE MWSTID = " + inMwstId;
decimal mwstProzent = (decimal)DB.execScalar(sqlMwstProzent);
String curWährung = Settings.getGlobVarStr("Währung") == String.Empty ? CRechnung.STD_WÄHRUNG : Settings.getGlobVarStr("Währung");

foreach (System.Data.DataRow drPos in dtRechPos.Rows)
{
    curRow += 1; 

    if (drPos["TEXT"].ToString().Equals(CRechnung.KK_TEXT_MONATL))
    {
        oTable.Cell(curRow, 1).Range.Text = String.Format("{0}, {1}\v{2}\v{3} {4}",
        drPos["NAME"], drPos["VORNAME"], drPos["STRASSE"], drPos["PLZ"], drPos["ORT"]);
        oTable.Cell(curRow, 2).Range.Text = String.Format("{0}", ((DateTime)drPos["GEBDATUM"]).ToShortDateString());
        oTable.Cell(curRow, 3).Range.Text = drPos["VERSICHNR"].ToString();
        oTable.Cell(curRow, 4).Range.Text = String.Format("{0:0.00} {1}", drPos["BRUTTO"], curWährung);                        
    }
    else if (drPos["TEXT"].ToString().Equals(CRechnung.KK_TEXT_EINMAL))
    {
        oTable.Cell(curRow - 1, 2).Range.Text += String.Format("{0}", CRechnung.KK_TEXT_EINMAL);
        oTable.Cell(curRow - 1, 4).Range.Text += String.Format("{0:0.00} {1}", drPos["BRUTTO"], curWährung);
    }
    else
    {
        oTable.Cell(curRow, 1).Merge(oTable.Cell(curRow, 3));
        oTable.Cell(curRow, 1).Range.Text = String.Format("{0} x {1} {2}", drPos["ANZAHL"], drPos["TEXT"], curWährung);
        oTable.Cell(curRow, 2).Range.Text = String.Format("{0:0.00} {1}", drPos["BRUTTOSUMME"], curWährung);                        
    }                                       
}

Now I want to find a way to detect wether a new page will be reached. If so I want to add something like 'continue on page 2'. Is that possible?

Was it helpful?

Solution

Would it be allowable to just check for page length and if Pages.Count == 2 then add the text or whatever after the fact?

Outside the foreach block, have a variable that tracks the current page count. In the foreach block, create the row, then check the page count. If newPageCount > oldPageCount then execute some code to insert the continuation message.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top