PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sun Jun 30, 2024 6:20 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: Wed Sep 17, 2008 1:12 pm 
Offline

Joined: Wed Sep 17, 2008 12:28 pm
Posts: 5
Location: Munich
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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Sep 18, 2008 8:07 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3100
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri Sep 19, 2008 12:41 am 
Offline

Joined: Wed Sep 17, 2008 12:28 pm
Posts: 5
Location: Munich
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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri Sep 19, 2008 8:09 am 
Offline

Joined: Wed Aug 13, 2008 7:45 am
Posts: 7
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.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri Sep 19, 2008 1:25 pm 
Offline

Joined: Wed Sep 17, 2008 12:28 pm
Posts: 5
Location: Munich
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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Sep 21, 2008 11:03 pm 
Offline

Joined: Wed Sep 17, 2008 12:28 pm
Posts: 5
Location: Munich
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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon Sep 22, 2008 12:13 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3100
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Sep 24, 2008 5:01 pm 
Offline

Joined: Wed Sep 17, 2008 12:28 pm
Posts: 5
Location: Munich
Great, that works.. thank you very much!


Top
 Profile  
Reply with quote  
PostPosted: Tue Jul 02, 2013 9:04 am 
Offline

Joined: Tue Jul 02, 2013 8:51 am
Posts: 1
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);
...


Top
 Profile  
Reply with quote  
PostPosted: Thu Jan 22, 2015 3:31 pm 
Offline

Joined: Thu Jan 22, 2015 3:29 pm
Posts: 5
Yes that solved the problem.

Thanks


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 95 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group