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

Avoiding paragraph line breaks in table
https://forum.pdfsharp.net/viewtopic.php?f=2&t=2134
Page 1 of 1

Author:  Klitgaard [ Mon Sep 10, 2012 1:48 pm ]
Post subject:  Avoiding paragraph line breaks in table

Hi there,
I am using Migradoc and am very impressed by the feature set. Great tool - also for a beginner like me.
I have created a table and am filling data into it with dataTable.Rows[lineNo].Cells[cellNo].AddParagraph(myLabel);
But Migradoc is more clever than me now. It is essential for me that the text does not wrap, which Migradoc very cleverly seems to do automatically if the text is too long.
I would much like it to just truncate the text (possibly with a "..." in the end if possible). Is that possible ?
Thanks a lot in advance,
Lars

Author:  Thomas Hoevel [ Tue Sep 11, 2012 9:00 am ]
Post subject:  Re: Avoiding paragraph line breaks in table

Hi, Lars!
Klitgaard wrote:
I would much like it to just truncate the text (possibly with a "..." in the end if possible). Is that possible?
This is not yet possible (and not yet on the wishlist).
So it's up to you to shorten the text and add the "...".

Author:  Klitgaard [ Tue Sep 11, 2012 10:27 am ]
Post subject:  Re: Avoiding paragraph line breaks in table

Hi Thomas,
Thanks a lot for a fast and precise answer. I do realize that MigraDoc offers very sophisticated functionality and I am the coward trying to avoid it :-)
When adding the columns to the table, I specify the column width, which I therefore know (I guess I could use Cells[i].Column.Width as well), but how can I get the length of a text string ? The Paragraph doesn't seem to have a width atttribute ?
Sorry for the maybe stupid question... - and thanks a lot again,
Lars

Author:  Thomas Hoevel [ Tue Sep 11, 2012 11:42 am ]
Post subject:  Re: Avoiding paragraph line breaks in table

I'd use XGraphics.MeasureString() to measure text. This is a PDFsharp function, so I'd create a dummy PdfDocument with a dummy PdfPage to get an XGraphics object to make the measurements.
Probably you'll have to use column width minus left and right padding (space for borders &c.) minus width of "..." (using either three dots "..." or Unicode character "…" (Alt+0133)).

Using the Bisection method, only few iterations should be required. If full text is too long and half text too short, try 75%. And then try 62.5% or 87.5% and so forth.
For narrow columns, brute force may not make a big difference ...

Author:  Klitgaard [ Tue Sep 11, 2012 3:19 pm ]
Post subject:  Re: Avoiding paragraph line breaks in table

Hi Thomas,
Cool. Thanks a lot for your help. I struggled a bit mixing the PDFSharp and MigraDoc worlds, but below is my working code solving the problem - in case others could benefit from it.
(Note: If you call this multiple times, you might want to move the first 3 lines of dummy-creation out of the routine).
Thanks again,
Lars (LarsBoKlitgaard@gmail.com)

Code:
      private Paragraph AddTextToCell(string instring, Cell cell, Unit fontsize)
        {
            PdfDocument pdfd = new PdfDocument();
            PdfPage pg = pdfd.AddPage();
            XGraphics oGFX = XGraphics.FromPdfPage(pg);
            Unit maxWidth = cell.Column.Width-(cell.Column.LeftPadding+cell.Column.RightPadding);
            Paragraph par;
            XFont font = new XFont(document.Styles["Table"].Font.Name, fontsize);
            if (oGFX.MeasureString(instring, font).Width < maxWidth.Value)
            {
                par = cell.AddParagraph(instring);
            }
            else // String does not fit - start the truncation process...
            {
                int stringlength = instring.Length;
                for (int i = 0; i<3; i++)
                {
                    if (oGFX.MeasureString(instring.Substring(0, stringlength) + '\u2026', font).Width > maxWidth.Value)
                        stringlength -= (int)Math.Ceiling(instring.Length * Math.Pow(0.5f, i));
                    else
                        if (i<2)
                            stringlength += (int)Math.Ceiling(instring.Length * Math.Pow(0.5f, i));
                }
                par = cell.AddParagraph(instring.Substring(0, stringlength) + '\u2026');
            }
            par.Format.Font.Size = fontsize;
            return par;
        }

Author:  hsahay [ Tue Jun 24, 2014 6:41 am ]
Post subject:  Re: Avoiding paragraph line breaks in table

Can you please tell me the code to insert soft hyphens? I used the code given above replacing the '\u2026' escape sequence with '\xad' and I'm getting a NullReferenceException. :?
Sorry about the silly question.. :| :oops:

Author:  Thomas Hoevel [ Tue Jun 24, 2014 9:41 am ]
Post subject:  Re: Avoiding paragraph line breaks in table

hsahay wrote:
Sorry about the silly question.. :| :oops:
Which editor? Which language?

For C#:
Code:
string shy = "\xAD";

Author:  pchandra [ Fri Jan 23, 2015 10:16 pm ]
Post subject:  Re: Avoiding paragraph line breaks in table

oGFX.MeasureString(instring, font).Width


what unit is the returned double value Point, Inch, Cm or mm ???

Author:  Thomas Hoevel [ Mon Jan 26, 2015 9:05 am ]
Post subject:  Re: Avoiding paragraph line breaks in table

pchandra wrote:
what unit is the returned double value Point, Inch, Cm or mm ???
Points.

Author:  pchandra [ Mon Jan 26, 2015 2:38 pm ]
Post subject:  Re: Avoiding paragraph line breaks in table

Thanks

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