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

Text wrapping in a table with borders
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3030
Page 1 of 1

Author:  mapoholic [ Wed Jan 21, 2015 2:08 pm ]
Post subject:  Text wrapping in a table with borders

I am probably doing something completely wrong as I can't think that this should be that difficult!
Here's my problem (using Migradoc and pdfsharp):
I am creating a table and this tables must have borders (not just external but 'gridlines' as well)
The issue that some of the text in the cell is larger than the cell width and it wraps - and that's fine.
Problem is that the borders (despite if I use borders for the whole table or just for each row or each column, DO NOT detect this so I am ending with tables like this- when using:
Code:
tabBlds.Format.Borders.Width = 0.5;

Attachment:
tab1.png
tab1.png [ 19.41 KiB | Viewed 10938 times ]

or this when using:
Code:
Column column = tabBlds.AddColumn("4.5cm");
column.Format.Borders.Right.Width = 1.0;
column = tabBlds.AddColumn("2.5cm");
column.Format.Borders.Right.Width = 1.0;
column = tabBlds.AddColumn("2.5cm");
column.Format.Borders.Right.Width = 1.0;
column = tabBlds.AddColumn("2.5cm");
column.Format.Borders.Right.Width = 1.0;
column = tabBlds.AddColumn("4cm");
column.Format.Borders.Right.Width = 1.0;
column = tabBlds.AddColumn("3cm");
column.Format.Borders.Right.Width = 0;


Attachment:
tab2.png
tab2.png [ 18.06 KiB | Viewed 10938 times ]


I have event tried to add some 'invisible' paragraphs (just text in white) so all cells have the same number of lines and that sort of works for the header row but not for the rest as I am reading data from a database for this and not sure how long the text will.

Surely there must be an easier way??
Many thanks in advance,
Pano

Author:  Thomas Hoevel [ Wed Jan 21, 2015 2:44 pm ]
Post subject:  Re: Text wrapping in a table with borders

Hi!

I presume the root of the problem is with the code you are not showing.

Our application works fine and the Invoice sample doesn't show this behavior either.
http://www.pdfsharp.net/wiki/Invoice-sample.ashx

Author:  mapoholic [ Wed Jan 21, 2015 3:15 pm ]
Post subject:  Re: Text wrapping in a table with borders

Most probably! Ok, I will try to make a sample project and show you the full code I am using. But as far as you are aware there is no 'trick' to this- right? It should just work and respect the text wrapping?

Author:  mapoholic [ Wed Jan 21, 2015 4:40 pm ]
Post subject:  Re: Text wrapping in a table with borders

Ok, so here's some sample code- what am I doing wrong?

Code:
Document document = new Document();

            PageSetup myPageSetup = new PageSetup();
            myPageSetup = document.DefaultPageSetup.Clone();
            myPageSetup.FooterDistance = "0.1cm";
            myPageSetup.LeftMargin = Unit.FromCentimeter(0.5);
            myPageSetup.BottomMargin = Unit.FromCentimeter(1.0);
            myPageSetup.RightMargin = Unit.FromCentimeter(0.5);
            myPageSetup.TopMargin = Unit.FromCentimeter(5.5);
            DefineStyles();

            Section section = document.AddSection();
            Table tabBlds = section.AddTable();
            tabBlds.Format.Font.Size = "9";
            tabBlds.Format.Alignment = ParagraphAlignment.Center;
            //tabBlds.Format.Borders.Width = 1.0;
            tabBlds.Rows.LeftIndent = 0;
            tabBlds.LeftPadding = 0;
            tabBlds.RightPadding = 0;
            tabBlds.TopPadding = 0;
            tabBlds.BottomPadding = 0;
            Column column = tabBlds.AddColumn("4cm");
            column.Format.Borders.Right.Width = 1.0;
            column = tabBlds.AddColumn("2.5cm");
            column.Format.Borders.Right.Width = 1.0;
            column = tabBlds.AddColumn("2.5cm");
            column.Format.Borders.Right.Width = 1.0;
            column = tabBlds.AddColumn("2.0cm");
            column.Format.Borders.Right.Width = 1.0;
            column = tabBlds.AddColumn("4cm");
            column.Format.Borders.Right.Width = 1.0;
            column = tabBlds.AddColumn("3.0cm");
            column.Format.Borders.Right.Width = 0;

            Row row = tabBlds.AddRow();
            row.HeadingFormat = true;
            row.Format.Alignment = ParagraphAlignment.Center;

            row.Cells[0].AddParagraph("This text will fit in two lines - now!");
            //Paragraph p1 = new Paragraph();
            //p1.AddFormattedText("INVISIBLE TEXT");
            //p1.Format.Font.Color = Color.FromCmyk(0.0, 0.0, 0.0, 0.0);
            //row.Cells[0].Add(p1);

            row.Cells[1].AddParagraph("One liner 1");
            //row.Cells[1].Add(p1.Clone());
            //row.Cells[1].Add(p1.Clone());

            row.Cells[2].AddParagraph("One liner 2");
            //row.Cells[2].Add(p1.Clone());
            //row.Cells[2].Add(p1.Clone());

            row.Cells[3].AddParagraph("One liner 3");
            //row.Cells[3].Add(p1.Clone());
            //row.Cells[3].Add(p1.Clone());

            row.Cells[4].AddParagraph("One liner 4");
            //row.Cells[4].Add(p1.Clone());
            //row.Cells[4].Add(p1.Clone());

            row.Cells[5].AddParagraph("This text is long enough to fit in three lines");

            row = tabBlds.AddRow();
            row.HeadingFormat = false;
            row.Format.Alignment = ParagraphAlignment.Center;
            row.Format.Borders.Top.Width = 1.0;

            row.Cells[0].AddParagraph("One liner 1");
            row.Cells[1].AddParagraph("This text will fit in two lines - now!");
            row.Cells[2].AddParagraph("This text is long enough to fit in three lines");
            row.Cells[3].AddParagraph("One liner 2");
            row.Cells[4].AddParagraph("One liner 3");
            row.Cells[5].AddParagraph("One liner 4");
           

            tabBlds.SetEdge(0, 0, tabBlds.Columns.Count, tabBlds.Rows.Count, Edge.Box, BorderStyle.Single, 1.0, Color.Empty);

            PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(true);
            // Set the MigraDoc document
            pdfRenderer.Document = document;

            // Create the PDF document
            pdfRenderer.RenderDocument();

            // Save the PDF document...
            string filename = string.Empty;

            // I don't want to close the document constantly...
            filename = "test-" + Guid.NewGuid().ToString("N").ToUpper() + ".pdf";
            pdfRenderer.Save("C:\\Development\\Geoktima\\out_pdf\\" + filename);


And here's the output:
Attachment:
tab3.png
tab3.png [ 12.46 KiB | Viewed 10935 times ]

Author:  Thomas Hoevel [ Thu Jan 22, 2015 12:16 pm ]
Post subject:  Re: Text wrapping in a table with borders

OK, I get the same table you get.
I don't have a solution yet.

Author:  Thomas Hoevel [ Thu Jan 22, 2015 12:31 pm ]
Post subject:  Re: Text wrapping in a table with borders

I think you do nothing wrong.

When you set the border for each cell, the table will look right:
Code:
            for (int i = 0; i < 5; ++i)
                row.Cells[i].Borders.Right.Width = 1;


Setting the border for each cell should IMHO have the same effect, but lines are too short.

Author:  mapoholic [ Fri Jan 23, 2015 2:09 pm ]
Post subject:  Re: Text wrapping in a table with borders

Yip- you are right. This works with all borders as well AND using padding (row and column borders don't when you specify padding).
Having said that in the end I used:
Code:
tabBlds.Format.Borders.Width = 1.0


Which does it in one go and seems to respect wrapped lines and padding. But many thanks again for your feedback. Cell border(-ing) will come in handy when trying to 'fine-tune' the borders

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