I am trying to ascertain how large a justified text block will be when printed by rasterizing it in the "Printout"
style environment. However, Rasterize
is reverting to the "Working"
environment. How can I obtain a raster of the graphic as it appears in "Printout"
style?
The following code illustrates the problem. Note the text differs in size in the two environments: In the "Printout"
environment the text occupies eight lines, but in the rasterised "Working"
version it takes up only seven lines.
I am using text height prediction for page layouts.
SetOptions[EvaluationNotebook[], ScreenStyleEnvironment -> "Printout"];
lorem = StringTake[ExampleData[{"Text", "LoremIpsum"}], {198, 922}];
{wd, ht} = {564, 120};
box = Graphics[{Green, Rectangle[{0, 0}, {wd, ht}],
Black, Inset[TextCell[lorem, LineSpacing -> {0, 16},
TextJustification -> 1], {0, 0}, {Left, Bottom},
{wd, Automatic}]},
BaseStyle -> {FontFamily -> "Times", FontSize -> 13},
PlotRange -> {{0, wd}, {0, ht}}, ImageSize -> wd]
Rasterize[box]
The notebook screen environment can be restored with the following statement:
SetOptions[EvaluationNotebook[], ScreenStyleEnvironment -> Inherited]
Edit
Further to george2079's suggestion I made an automated procedure to produce a rasterised "Printout"
graphic. It is subsequently processed in a separate procedure (not shown here) to ascertain the height of the text box required for print output.
createPrintBox[text_] := Module[{wd = 564, ht = 120, nb},
nb = CreateDocument[ExpressionCell[
Graphics[{Green, Rectangle[{0, 0}, {wd, ht}],
Black, Inset[TextCell[text, LineSpacing -> {0, 16},
TextJustification -> 1], {0, 0}, {Left, Bottom},
{wd, Automatic}]},
BaseStyle -> {FontFamily -> "Times", FontSize -> 13},
PlotRange -> {{0, wd}, {0, ht}}, ImageSize -> wd], "Print"],
ScreenStyleEnvironment -> "Printout", Visible -> False];
SelectionMove[nb, Next, Cell];
FrontEndExecute[FrontEndToken[nb, "CopySpecial", "MGF"]];
SelectionMove[nb, After, Cell];
FrontEndExecute[FrontEndToken[nb, "Paste"]];
SelectionMove[nb, All, Cell];
cellcontent = NotebookRead[nb];
NotebookClose[nb];
output = ToExpression[First@cellcontent];
Rasterize[Show[output, ImageSize -> wd]]]
lorem = StringTake[ExampleData[{"Text", "LoremIpsum"}], {198, 922}];
printoutbox = createPrintBox[lorem]
As you can see, this is a useable raster image showing the correct "Printout"
style, justified text height of eight lines.
In contrast, here is the seven line "Working"
screen-style raster produced by the code below:-
lorem = StringTake[ExampleData[{"Text", "LoremIpsum"}], {198, 922}];
{wd, ht} = {564, 120};
Rasterize[Graphics[{Green, Rectangle[{0, 0}, {wd, ht}],
Black, Inset[TextCell[lorem, LineSpacing -> {0, 16},
TextJustification -> 1], {0, 0}, {Left, Bottom},
{wd, Automatic}]},
BaseStyle -> {FontFamily -> "Times", FontSize -> 13},
PlotRange -> {{0, wd}, {0, ht}}, ImageSize -> wd]]