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 have made various Grid-s and arranged them column-wise in another Grid.

All Grids are working fine and I exported that final Grid into PDF format.

The entire Grid appeared in a single page after exporting, but I want to Export some Grid-s to one page and the remaining `Grid-s are another page.

I tried the following code but it that doesn't work.

grid1 = Grid[{{Style["Life Alerts", Bold, 20], SpanFromLeft, 
 SpanFromLeft}, {Style["Alert", Bold, 15], Style["<>", Bold, 15], 
 Style["%Life", Bold, 15], 
 Style["OnDay", Bold, 15]}, {Style["L1", Bold, 15], 
 Style["<", Bold, 15], Style["90", Bold, 15], 
 Style["0.35", Bold, 15]}, {Style["L2", Bold, 15], 
 Style[">", Bold, 15], Style["90", Bold, 15], 
 Style["0.35", Bold, 15]}, {Style["L3", Bold, 15], 
 Style[">", Bold, 15], Style["90", Bold, 15], 
 Style["0.35", Bold, 15]}}, Alignment -> {{Left}, Center}, 
  Background -> {None, {Yellow}}, Frame -> True, 
  FrameStyle -> Directive[Thickness[4], Black], Spacings -> {1, 1}, 
    Dividers -> {{}, {True, True, True}}];

Mathematica graphics

  (*Exporting code*)
  Export[SystemDialogInput["FileSave",".Pdf"], (Grid[{{grid1}, {grid1}}])]

Can anybody help me?

share|improve this question
add comment

1 Answer

If I understood your question correctly, you may want something like this:

grid1 = 
  Grid[{{Style["Life Alerts", Bold, 20], SpanFromLeft, 
     SpanFromLeft}, {Style["Alert", Bold, 15], Style["<>", Bold, 15], 
     Style["%Life", Bold, 15], 
     Style["OnDay", Bold, 15]}, {Style["L1", Bold, 15], 
     Style["<", Bold, 15], Style["90", Bold, 15], 
     Style["0.35", Bold, 15]}, {Style["L2", Bold, 15], 
     Style[">", Bold, 15], Style["90", Bold, 15], 
     Style["0.35", Bold, 15]}, {Style["L3", Bold, 15], 
     Style[">", Bold, 15], Style["90", Bold, 15], 
     Style["0.35", Bold, 15]}}, Alignment -> {{Left}, Center}, 
   Background -> {None, {Yellow}}, Frame -> True, 
   FrameStyle -> Directive[Thickness[4], Black], Spacings -> {1, 1}, 
   Dividers -> {{}, {True, True, True}}];

With[{
  listOfGrids = {grid1, grid1}
  },
 Export[SystemDialogInput["FileSave", ".Pdf"], 
  CreateDocument[
   Riffle[listOfGrids, Cell["", "PageBreak", PageBreakBelow -> True]],
    Visible -> False]]
 ]

The export method is the same as in this question.

Edit

In response to the comment, it looks like you're encountering some of the ugly formatting problems that are caused by the incompatible mixture of different coordinate systems used by Mathematica. Since Grid is a formatting construct that adjusts itself to its environment, it's really hard to freeze the shape of a grid in place for further manipulation and in particular for Export.

Instead of worrying about the fragility of the lengths scales in Grid, I would suggest that you'll save a lot of time and effort by converting each grid in your problem to a graphics object before doing anything else with it. To do this in a way that prevents any changes in relative dimensions, I would do the following with your test grid:

First I define a helper function toPDF that converts any argument to PDF within the notebook, and apply it to grid1 from above. Then we export the grid of grids, but only after making sure that all the components (which are all grid here, but are now Graphics objects) have the same size in pixels, which I call itemWidth:

toPDF[x_] := 
 First[ImportString[ExportString[Magnify[x, .8], "PDF"], "PDF"]]

grid1 = toPDF[grid1];

With[{
  listOfGrids = {Grid[{{grid1, grid1, grid1, grid1}, {grid1, grid1, 
       grid1, grid1}, {grid1, grid1, grid1, grid1}, {grid1, grid1, 
       grid1, grid1}}], grid1},
  itemWidth = 90
  },
 Export["grids.Pdf",
  CreateDocument[
   Riffle[
    listOfGrids /. Graphics[x__] :> Show[
       Graphics[x],
       ImageSize -> itemWidth
       ],
    Cell["", "PageBreak", PageBreakBelow -> True]], Visible -> False]
  ]
 ]

Now the result of the export is a PDF file grid.pdf with two pages where all component grids are exactly the same size. I chose itemWidth = 90 by hand so that it ends up small enough to allow four grids side by side on the output page, assuming letter size paper. You have to change that length scale depending on your needs.

The itemWidth is set by a replacement rule in which I look for Graphics objects and Show them with the desired size. Instead of this global replacement rule, you could also fine-tune the width of different component tables directly at the time when you define them, e.g., by setting

grid2 = Show[toPDF[grid2], ImageSize -> 100];

etc. Then you can leave out the replacement /. Graphics[x__] :> Show[Graphics[x],ImageSize -> itemWidth] above.

This is of course a less than optimal solution because I have to convert all component grids to graphics using toPDF. But that's just my standard response to problems with changing relative lengths (or fonts). It's just simpler than trying adjust a myriad of other lengths in the Grids to get the desired result in the final export, because I find it almost impossible to predict how Export changes the appearance of a Grid in general.

share|improve this answer
    
your code working fine...for testing,I wrote this code listOfGrids={Grid[{{grid1,grid1,grid1,grid1},{grid1,grid1,grid1,grid1},{grid1,g‌​rid1,grid1,grid1},{grid1,grid1,grid1,grid1}}],grid1} instead of your code like listOfGrids={grid1,grid1}.it's not working,actuvally listOfGrids have 2 elements but 5 pages are created after Exporting.how can I solve this? –  subbu Dec 28 '12 at 9:40
add comment

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.