ststeiger / PdfSharpCore

Port of the PdfSharp library to .NET Core - largely removed GDI+ (only missing GetFontData - which can be replaced with freetype2)
Other
1.07k stars 238 forks source link

Text do not fit inside Table Cell after migration to MigraDocCore #118

Open PatrykPlewaOfficial opened 4 years ago

PatrykPlewaOfficial commented 4 years ago

After migration to PdfSharpCore / MigraDocCore I have noticed that text do no fit anymore in table cell. The code stayed untouched.

MigraDocCore-text-fit-issue

As you can see, MigraDocCore generated PDF document has some extra top cell spacing/margin/distance, which results in pruning some part of text with cell border.

Project:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="MigraDocCore.DocumentObjectModel" Version="0.1.1" />
    <PackageReference Include="MigraDocCore.Rendering" Version="0.1.0" />
    <PackageReference Include="PdfSharpCore" Version="1.2.6" />
    <PackageReference Include="PdfSharpCore.Charting" Version="0.1.0" />
  </ItemGroup>
</Project>

Repro Code:

Document document = new Document();
Section section = document.AddSection();

Table table = section.AddTable();
table.Borders.Width = 0.25;
for (int i = 0; i < 4; i++)
{
    var column = table.AddColumn();
    column.Format.Alignment = ParagraphAlignment.Center;
}

Row row = table.AddRow();
row.HeadingFormat = false;
row.Format.Alignment = ParagraphAlignment.Center;
row.Format.Font.Bold = false;
row.Cells[0].AddParagraph("Payment Date From:");
row.Cells[0].Format.Font.Bold = true;

row = table.AddRow();
row.HeadingFormat = false;
row.Format.Alignment = ParagraphAlignment.Center;
row.Format.Font.Bold = false;
row.Cells[0].AddParagraph("Entered by");
row.Cells[0].Format.Font.Bold = true;

row = table.AddRow();
row.HeadingFormat = false;
row.Format.Alignment = ParagraphAlignment.Center;
row.Format.Font.Bold = false;
row.Cells[0].AddParagraph("Data sorted by");
row.Cells[0].Format.Font.Bold = true;

// ...

I don't know if there is some kind of breaking change which adds a default top margin or it's an issue.

ststeiger commented 4 years ago

It may be an issue that has been fixed in PdfSharp after MigraDocCore has branched from MigraDoc.NET. Or it may be incorrect font handling.

PatrykPlewaOfficial commented 4 years ago

Previously worked on PdfSharp assembly version 1.50.4589.0

PatrykPlewaOfficial commented 4 years ago

My current workaround for this is:

table.BottomPadding = Unit.FromMillimeter(1.6);
adamxi commented 3 years ago

Experiencing the same issue. I've drawn borders around the paragraph and it's clear that the font is simply simply being calculated too high:

image

cchillen commented 3 years ago

I am also experience the same issue.

jafin commented 3 years ago

@PatrykPlewaOfficial

I ran the code above, on PDFSharpCore 1.2.19 MigraDocCore.Rendering 0.1.3 and it appears to match the expected?

Perhaps has been fixed? If so consider closing the issue

image

[Fact]
        public void VerifyMarginPadding()
        {
            var document = new Document();
            var section = document.AddSection();

            var table = section.AddTable();
            table.Borders.Width = 0.25;
            for (var i = 0; i < 4; i++)
            {
                var column = table.AddColumn();
                column.Format.Alignment = ParagraphAlignment.Center;
            }

            var row = table.AddRow();
            row.HeadingFormat = false;
            row.Format.Alignment = ParagraphAlignment.Center;
            row.Format.Font.Bold = false;
            row.Cells[0].AddParagraph("Payment Date From:");
            row.Cells[0].Format.Font.Bold = true;

            row = table.AddRow();
            row.HeadingFormat = false;
            row.Format.Alignment = ParagraphAlignment.Center;
            row.Format.Font.Bold = false;
            row.Cells[0].AddParagraph("Entered by");
            row.Cells[0].Format.Font.Bold = true;

            row = table.AddRow();
            row.HeadingFormat = false;
            row.Format.Alignment = ParagraphAlignment.Center;
            row.Format.Font.Bold = false;
            row.Cells[0].AddParagraph("Data sorted by");
            row.Cells[0].Format.Font.Bold = true;

            // Create a renderer for the MigraDoc document.
            const bool unicode = false;
            var pdfRenderer = new PdfDocumentRenderer(unicode)
            {
                Document = document
            };
            pdfRenderer.RenderDocument();
            pdfRenderer.PdfDocument.Save("C:\\temp\\table.pdf");
        }