Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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]

enter image description here

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]

enter image description here

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:-

enter image description here

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]]
share|improve this question

1 Answer 1

up vote 2 down vote accepted

Try this: copy as bitmap using the GUI menu and paste into an input cell, the resulting image reports a slightly different pixel size than the raster object. The difference is so subtle I'm not sure if thats what you are after or not.

If that works I'm not sure how to automate the procedure..

Edit: This does indeed do what I suspect you need, resulting in an image that preserves the formatted linewrap. Now it is really bugging me I cant see how to programatically select the graphic. The copy should be some incarnation of

FrontEndTokenExecute[FrontEnd`SelectedNotebook[], "CopySpecial", "MGF"]
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.