# Controlling page breaks

I'm writing a bunch of cells to a new notebook nb and am trying to control the pagebreaks. That is, I want a pagebreak after each Length[listOef] number of cells. But sometimes, I get two pagebreaks, depending on which random permutatation of listOef.

listOef


contains "aantalVragen" number of cells given by the user.

Then I use the following code:

nb = CreateDocument[Null, Magnification -> 1.5, ShowCellTags -> False,
PageHeaders -> {{None, None, None}, {None, None, None}},
ShowCellLabel -> True];

(*Initialize exported notebook*)
SelectionMove[nb, Next, Cell];
NotebookDelete[nb];

allPermutations = Table[RandomPermutation[aantalVragen], {aantalToetsen}];

Do[
NotebookWrite[nb, Cell["Toets 1." <> ToString[toetsNummer], "Title",
PageBreakAbove -> True]];
NotebookWrite[nb, Cell[datumToets, "Text"]];
NotebookWrite[nb, Cell["Wiskunde", "Text", TextAlignment -> Right]];
NotebookWrite[nb, Cell["G. Hollander", "Text", TextAlignment -> Right]];

(* Test toetsNummer *)
Do[
NotebookWrite[nb, Permute[listOef, allPermutations[[toetsNummer]]][[vraagNummer]]];
SelectionMove[nb, Previous, Cell];
SetOptions[NotebookSelection[nb],
CellLabel -> "Vraag " <> ToString[vraagNummer] <> "."];
SelectionMove[nb, After, Notebook])
,
{vraagNummer, aantalVragen}];

**SelectionMove[nb, Previous, Cell];
SetOptions[NotebookSelection[nb], PageBreakBelow -> False];
SelectionMove[nb, Next, Cell];**
,
{toetsNummer, aantalToetsen}]



Why does I sometimes get two pagebreaks? I then check if the last cell of the page has a pagebreak with the code

SelectionMove[nb,Previous,Cell];
Options[NotebookSelection[nb]]


at the end of every inner Do[] (which is for every page), but I get an empy list of options, so I don't know if there is a pagebreak or not.

Also, I'm not quite sure of the part of the code in ** Bold **: I'm trying to suppress pagebreaks at the end of a page, as I'm doing that with

 NotebookWrite[nb,
Cell["Toets 1." <> ToString[toetsNummer], "Title", PageBreakAbove -> True]];


at the beginning of the code. But that doesn't seem to prevent double page breaks, sometimes.

-
Sometimes you get two pagebreaks? What inputs give you two pagebreaks? The ... section doesn't seem necessary either. –  Timothy Wofford Sep 10 '13 at 12:26
I don't know which inputs give me two pagebreaks, that's the issue. Which ... section do you say isn't necessary? Thanks! –  Gabriel Sep 10 '13 at 20:53
I got caught by the 5 min comment edit limit. The section you seemed unsure about is not necessary. The program seemed to work fine on my computer, though I had to make assumptions about datumToets and other undefined symbols. Could you edit your code so we don't have to make so many assumptions? –  Timothy Wofford Sep 10 '13 at 21:24
Sure! Can I start a new discussion, or should I make an answer, as to get the correct formatting of the code? –  Gabriel Sep 10 '13 at 21:39