2

When I try to export a Grid object with a White background, I get a gray border around 3 of the 4 sides in the resulting pdf:

grid4pdf = Grid[{{"a", "b", "c"}, {"d", "e", "f"}, {"g", "h", "i"}}, Background -> White]
Export["thepdfhasborders.pdf", grid4pdf]

I get this as the output pdf:

(the light gray is the Adobe Acrobat Reader background). Any idea why the dark gray border shows up in the pdf, and how I can get rid of it? I typically use the ReverseColor.nb stylesheet, but this behavior persists even in a Default.nb stylesheet notebook.

Thanks in advance!

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
Jaffe42
  • 193
  • 9

1 Answers1

3

Please read up the end: there are white borders with the default stylesheet, and I consider that as a bug in Export!

I don't get the gray borders with Mathematica 12.3.1 on Windows 10 x64:

grid4pdf = 
 Grid[{{"a", "b", "c"}, {"d", "e", "f"}, {"g", "h", "i"}}, 
  Background -> White]
Export["thepdfhasborders.pdf", grid4pdf] // SystemOpen

screenshot

But there is something interesting that I noticed in the FrontEnd when I selected the Grid object in the output by the mouse. Notice how the FrontEnd draws the blue border around 3 of the 4 sides of the selection:

screenshot2

Since it is the FrontEnd who generates the exported PDF file, I suspect it may be related to the issue you observe. The following experiment supports this:

grid4pdf = 
  Grid[{{"█", "█", "█"}, {"█", "█", "█"}, {"█", "█", "█"}}, 
   Background -> Red];
Image[image = Rasterize[grid4pdf, Background -> Yellow], 
 Magnification -> 6]
BorderDimensions[image]

output

Notice the yellow borders around 3 of the 4 sides in the rasterized version of the grid. In the PDF with the default stylesheet we get white borders:

Export["thepdfhasborders.pdf", grid4pdf] // SystemOpen

screenshot

Since you mention that you use a custom stylesheet, I suspect that the gray color of the borders may be related to the Background setting in that stylesheet. If you have set your stylesheet as the default one by changing the global DefaultStyleDefinitions option, you can temporarily return it back using the method from this answer:

CurrentValue[$FrontEndSession, DefaultStyleDefinitions] = "Default.nb"
FrontEndTokenExecute["ToggleShowExpression"]
Export["thepdfhasborders.pdf", grid4pdf] // SystemOpen

Or you can try the method from this answer.

Another method is to export a Cell expression with explicit Background option:

Export["thepdfhasborders.pdf", 
  Cell[BoxData[ToBoxes[grid4pdf]], "Output", 
   Background -> White]] // SystemOpen

P.S. I consider the addition of the borders as a bug in Export since even explicit addition of the options CellFrameMargins -> 0, CellFrame -> False, ShowCellBracket -> False doesn't remove the borders. Please report it to the support.

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
  • Thank you so much Alexey! Changing the FrontEnd options via your first code snippet was an effective workaround for me. I've also reported to support as per your suggestion. Thanks again!! – Jaffe42 Aug 23 '21 at 14:23
  • @Jaffe42 Glad to help. Thank you for reporting! – Alexey Popkov Aug 23 '21 at 17:21