empira / PDFsharp

PDFsharp and MigraDoc Foundation for .NET 6 and .NET Framework
https://docs.pdfsharp.net/
Other
398 stars 91 forks source link

MigraDoc: LeftPadding seems to be negated when drawing Table #122

Closed helluvamatt closed 1 month ago

helluvamatt commented 1 month ago

There seems to be an issue with using LeftPadding on a Table or Column with MigraDoc. I am using the "Core" build.

Expected Behavior

The generated PDF should contain a table with a single cell which spans the full width of the page (minus margins) and contains some centered text.

Actual Behavior

The generated PDF contains a table with a single cell that is rendered off the left side of the page.

Example: bug.pdf

Steps to Reproduce the Behavior

Use the following code to generate a PDF which reproduces this bug. You may also need code to setup fonts and styles to use the fonts.

Document document = new();

Section section = document.AddSection();
section.PageSetup.PageFormat = PageFormat.Letter;
section.PageSetup.LeftMargin = Unit.FromInch(0.5);
section.PageSetup.RightMargin = Unit.FromInch(0.5);
section.PageSetup.TopMargin = Unit.FromInch(0.5);
section.PageSetup.BottomMargin = Unit.FromInch(0.5);

Table table = section.AddTable();
Column column = table.AddColumn(Unit.FromInch(7.5)); // 8.5 - 0.5 - 0.5 (Letter size - left margin - right margin)
column.LeftPadding = Unit.FromInch(1);
column.RightPadding = Unit.FromInch(1);
Row row = table.AddRow();
row.TopPadding = 10; // pt
row.BottomPadding = 10; // pt
Cell cell = table[0, 0];
cell.Shading.Color = Colors.LightGray;
cell.Borders.Style = BorderStyle.Single;
cell.Borders.Color = Colors.Red;
cell.Format.Alignment = ParagraphAlignment.Center;
cell.AddParagraph("This text should be centered horizontally on the page.");

// Render document to PDF
PdfDocumentRenderer renderer = new()
{
    Document = document
};
renderer.RenderDocument();
renderer.PdfDocument.Save("path/to/bug.pdf");
ThomasHoevel commented 1 month ago

The behavior of MigraDoc tables is exactly as we see it with MS Word tables.

I admit the behavior of LeftPadding was counter-intuitive at first, but it is a feature, not a bug.

helluvamatt commented 1 month ago

Damn. I'm used to CSS where every CSS framework/reset sets box-sizing: border-box; when the default is content-box which is this exact behavior. Nothing to see here, move along citizen.

ThomasHoevel commented 3 weeks ago

I'd like to mention that maybe the desired effect can also be achieved using a Paragraph with border and background color instead of using a table.