PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Table problem: How to remove the space between two columns?
https://forum.pdfsharp.net/viewtopic.php?f=2&t=480
Page 1 of 1

Author:  paeppi [ Wed Sep 17, 2008 1:12 pm ]
Post subject:  Table problem: How to remove the space between two columns?

Hi,

I create a Table which has two colums. Every cell gets a background color by setting cell.Format.Shading.Color to some color.

The table printed out by this action has a ~ 0.5 cm space between the Columns, though I checked every Format in debugger (the one of the table, the row, the columns, and the cells) and all numeric (unit) values are 0. How do I remove the space, or better change it to be only 0.1 cm?

Here is the code creating the table:

Code:
PdfDocument outputPdf = new PdfDocument();
PdfPage page0 = outputPdf.AddPage();

Document doc = new Document();
Section tableSection = doc.AddSection();

Table myTable = tableSection.AddTable();

Column column1 = myTable.AddColumn(Unit.FromCentimeter(10));
Column column2 = myTable.AddColumn(Unit.FromCentimeter(5));
column1.Format.Alignment = ParagraphAlignment.Left;
column2.Format.Alignment = ParagraphAlignment.Right;

string color1 = "0xd5dadc";
string color2 = "0xb2b9be";
bool colorAlter = true;

for(int i = 0; i < 10; i++)
{
    Cell cell;
    Row row = myTable.AddRow();

    cell = row.Cells[0];
    cell.Format.Shading.Color = Color.FromRgbColor((byte)255, Color.Parse(colorAlter ? color1 : color2));
    cell.AddParagraph("Row " + i + ", Cell 1");

    cell = row.Cells[1];
    cell.Format.Shading.Color = Color.FromRgbColor((byte)255, Color.Parse(colorAlter ? color1 : color2));
    cell.AddParagraph("Row " + i + ", Cell 2");
    colorAlter = !colorAlter;
}

DocumentRenderer docRenderer = new MigraDoc.Rendering.DocumentRenderer(doc);
docRenderer.PrepareDocument();
XGraphics gfx = XGraphics.FromPdfPage(page0);
docRenderer.RenderPage(gfx, 1, PageRenderOptions.All);

outputPdf.Save("TablesTest1.pdf");
Process.Start("TablesTest1.pdf");


Here is, in case you are lazy and don't want to run the code yourself, a short snapshot of what the output looks like:

Image

Thanks for your answer,
paeppi

Author:  Thomas Hoevel [ Thu Sep 18, 2008 8:07 am ]
Post subject: 

Hi!

Try setting the Color for the Row.
IIRC you only have to set the color once for the row and not for every cell.
Colors are inherited. A cell has its own color - or uses the Row's color - or uses the Column's color (or was it the other way around?). Maybe there's even a table color as a last resort,

You can set the Color for Cells to highlight them. You can also set the paragraph shading of the text in the cell to highlight something.

But here you only have to set the row color.

Author:  paeppi [ Fri Sep 19, 2008 12:41 am ]
Post subject: 

Hi,

thanks for your fast reply. :wink:

I tried all three, coloring the background of the columns, of the table and of the rows, and nothing seems to be able to remove the white space between the two columns. I guess the white space there is just not part of anything in the table.

My source looks now like this (myTable1 is the one with the column colors, myTable2 the one with the color in the table):

Code:
PdfDocument outputPdf = new PdfDocument();
PdfPage page0 = outputPdf.AddPage();

Document doc = new Document();
Section tableSection = doc.AddSection();

/* Table containing Columns with background color */
Table myTable1 = tableSection.AddTable();
Column table1_column1 = myTable1.AddColumn(Unit.FromCentimeter(10));
Column table1_column2 = myTable1.AddColumn(Unit.FromCentimeter(5));
table1_column1.Format.Shading.Color = Color.FromRgbColor((byte)255, Color.Parse("0xaaeeaa"));
table1_column2.Format.Shading.Color = Color.FromRgbColor((byte)255, Color.Parse("0xaaeeaa"));

Row table1_rowWithoutColor = myTable1.AddRow();
table1_rowWithoutColor.Cells[0].AddParagraph("Row without color, Cell 1");
table1_rowWithoutColor.Cells[1].AddParagraph("Row without color, Cell 2");

string color1 = "0xd5dadc";
string color2 = "0xb2b9be";
bool colorAlter = true;
for(int i = 0; i < 5; i++)
{
    Row row = myTable1.AddRow();
    row.Format.Shading.Color = Color.FromRgbColor((byte)255, Color.Parse(colorAlter ? color1 : color2));
    row.Cells[0].AddParagraph("Row " + i + ", Cell 1");
    row.Cells[1].AddParagraph("Row " + i + ", Cell 2");
    colorAlter = !colorAlter;
}

/* placeholder */
tableSection.AddParagraph("\n\n");

/* Table with Background color */
Table myTable2 = tableSection.AddTable();
myTable2.Format.Shading.Color = Color.FromRgbColor((byte)255, Color.Parse("0xeeeeee"));

Column table2_column1 = myTable2.AddColumn(Unit.FromCentimeter(10));
Column table2_column2 = myTable2.AddColumn(Unit.FromCentimeter(5));
Row table2_rowWithoutColor = myTable2.AddRow();

table2_rowWithoutColor.Cells[0].AddParagraph("Row without color, Cell 1");
table2_rowWithoutColor.Cells[1].AddParagraph("Row without color, Cell 2");

DocumentRenderer docRenderer = new MigraDoc.Rendering.DocumentRenderer(doc);
docRenderer.PrepareDocument();
XGraphics gfx = XGraphics.FromPdfPage(page0);
docRenderer.RenderPage(gfx, 1, PageRenderOptions.All);

outputPdf.Save("TablesTest1.pdf");
Process.Start("TablesTest1.pdf");


And here is what the result looks like:

Image

Author:  garethterrace [ Fri Sep 19, 2008 8:09 am ]
Post subject: 

this is, I think, a borders issue.

Set the tbl.Borders.Visible = false; or the row.borders.visible = false;

or the cell, or one of them!

that should get rid of it I think.

Author:  paeppi [ Fri Sep 19, 2008 1:25 pm ]
Post subject: 

Hi,

no, it's not a border issue. I've set every border, the .Border and .Format.Border to Visible = false:

Code:
PdfDocument outputPdf = new PdfDocument();
PdfPage page0 = outputPdf.AddPage();

Document doc = new Document();
Section tableSection = doc.AddSection();

/* Table with Background color */
Table myTable2 = tableSection.AddTable();
myTable2.Format.Shading.Color = Color.FromRgbColor((byte)255, Color.Parse("0xdddddd"));
myTable2.Borders.Visible = false;
myTable2.Format.Borders.Visible = false;

Column table2_column1 = myTable2.AddColumn(Unit.FromCentimeter(10));
Column table2_column2 = myTable2.AddColumn(Unit.FromCentimeter(5));
table2_column1.Borders.Visible = false;
table2_column1.Format.Borders.Visible = false;
table2_column2.Borders.Visible = false;
table2_column2.Format.Borders.Visible = false;
Row table2_rowWithoutColor = myTable2.AddRow();

table2_rowWithoutColor.Borders.Visible = false;
table2_rowWithoutColor.Format.Borders.Visible = false;

table2_rowWithoutColor.Cells[0].AddParagraph("Row without color, Cell 1");
table2_rowWithoutColor.Cells[1].AddParagraph("Row without color, Cell 2");

table2_rowWithoutColor.Cells[0].Borders.Visible = false;
table2_rowWithoutColor.Cells[0].Format.Borders.Visible = false;
table2_rowWithoutColor.Cells[1].Borders.Visible = false;
table2_rowWithoutColor.Cells[1].Format.Borders.Visible = false;

DocumentRenderer docRenderer = new MigraDoc.Rendering.DocumentRenderer(doc);
docRenderer.PrepareDocument();
XGraphics gfx = XGraphics.FromPdfPage(page0);
docRenderer.RenderPage(gfx, 1, PageRenderOptions.All);

outputPdf.Save("TablesTest1.pdf");
Process.Start("TablesTest1.pdf");


Result:
Image

Author:  paeppi [ Sun Sep 21, 2008 11:03 pm ]
Post subject: 

Any further ideas? I am completely lost, I've no ideas left how to solve the issue.

Maybe someone should move it to the Bug Reports Forum?

Regards,
paeppi

Author:  Thomas Hoevel [ Mon Sep 22, 2008 12:13 pm ]
Post subject: 

Hi!

Set row.Shading.Color, cell.Shading.Color, &c.

You only set the shading of the text in the cells - therefore the gap!

Set only the shading of the rows and you're done.

Author:  paeppi [ Wed Sep 24, 2008 5:01 pm ]
Post subject: 

Great, that works.. thank you very much!

Author:  Lakerfield [ Tue Jul 02, 2013 9:04 am ]
Post subject:  Re: Table problem: How to remove the space between two colum

Set the padding to 0 on the table object, to remove the space between columns

Code:
table.Borders.Width = 0;
table.LeftPadding = 0;
table.RightPadding = 0;

If the padding isn't set then they change to default values while rendering the table

Code:
namespace MigraDoc.DocumentObjectModel.Visitors
...
  public abstract class VisitorBase : DocumentObjectVisitor
...
    internal override void VisitTable(Table table)
    {
      Document document = table.Document;

      if (table.leftPadding.IsNull)
        table.leftPadding = Unit.FromMillimeter(1.2);
      if (table.rightPadding.IsNull)
        table.rightPadding = Unit.FromMillimeter(1.2);
...

Author:  pchandra [ Thu Jan 22, 2015 3:31 pm ]
Post subject:  Re: Table problem: How to remove the space between two colum

Yes that solved the problem.

Thanks

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/