PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sun Jun 30, 2024 4:21 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: Mon Sep 10, 2012 1:48 pm 
Offline

Joined: Tue Apr 24, 2012 2:28 pm
Posts: 7
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


Top
 Profile  
Reply with quote  
PostPosted: Tue Sep 11, 2012 9:00 am 
Offline
PDFsharp Guru
User avatar

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

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Sep 11, 2012 10:27 am 
Offline

Joined: Tue Apr 24, 2012 2:28 pm
Posts: 7
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


Top
 Profile  
Reply with quote  
PostPosted: Tue Sep 11, 2012 11:42 am 
Offline
PDFsharp Guru
User avatar

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

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Sep 11, 2012 3:19 pm 
Offline

Joined: Tue Apr 24, 2012 2:28 pm
Posts: 7
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;
        }


Top
 Profile  
Reply with quote  
PostPosted: Tue Jun 24, 2014 6:41 am 
Offline

Joined: Tue Jun 24, 2014 6:34 am
Posts: 3
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:


Top
 Profile  
Reply with quote  
PostPosted: Tue Jun 24, 2014 9:41 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3100
Location: Cologne, Germany
hsahay wrote:
Sorry about the silly question.. :| :oops:
Which editor? Which language?

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

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Fri Jan 23, 2015 10:16 pm 
Offline

Joined: Thu Jan 22, 2015 3:29 pm
Posts: 5
oGFX.MeasureString(instring, font).Width


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


Top
 Profile  
Reply with quote  
PostPosted: Mon Jan 26, 2015 9:05 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3100
Location: Cologne, Germany
pchandra wrote:
what unit is the returned double value Point, Inch, Cm or mm ???
Points.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Jan 26, 2015 2:38 pm 
Offline

Joined: Thu Jan 22, 2015 3:29 pm
Posts: 5
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: No registered users and 23 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:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group