PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri Jun 28, 2024 4:12 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Tue Jun 02, 2009 5:02 pm 
Offline

Joined: Tue Jun 02, 2009 4:51 pm
Posts: 1
Hi,

I am building a table. Can I autofit text into cells? For example, if I randomly have text, "I like to eat at McGruderalpostermus.". Is it possible to fit this into a cell without the knowledge of it being there before hand? I have looked at many classes in MigraDoc and have no found a solution.

thanks,
Dave


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Jun 03, 2009 4:36 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3100
Location: Cologne, Germany
Hi!

You set the width of the column - MigraDoc determines the height automatically depending on the text you put in.

BTW: MigraDoc will not break words like "McGruderalpostermus" unless you insert some soft hyphens.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Fri Jul 24, 2009 5:23 am 
Offline

Joined: Fri Oct 24, 2008 12:24 pm
Posts: 13
Location: Brisbane, Australia
I have a partial solution to this as shown below. It works for me but I have had a few problems where the table and/or Cell concerned has Borders defined (please feel free to correct/adjust :lol: )

I use this when I am contructing a table cell and inserting the text. Therefore in advance I know the Cell object and the Font which I will be rendering with

Code:
var style = document.Styles["Normal"];
style.Font.Size = 5.0;
tm = new TextMeasurement(document.Styles["Normal"].Font.Clone());


I then add the text to the cell as follows

Code:
string someText = "I like to eat at McGruderalpostermus.";
cell.AddParagraph(AdjustIfTooWideToFitIn(cell, someText));


and the code that peforms the width adjustment is as follows

Code:
private static string AdjustIfTooWideToFitIn(Cell cell, string text)
{
    Column column = cell.Column;
    Unit availableWidth = column.Width - column.Table.Borders.Width - cell.Borders.Width;

    var tooWideWords = text.Split(" ".ToCharArray()).Distinct().Where(s => TooWide(s, availableWidth));

    var adjusted = new StringBuilder(text);
    foreach (string word in tooWideWords)
    {
        var replacementWord = MakeFit(word, availableWidth);
        adjusted.Replace(word, replacementWord);
    }

    return adjusted.ToString();
}

private static bool TooWide(string word, Unit width)
{
    float f = tm.MeasureString(word, UnitType.Point).Width;
    return f > width.Point;
}

/// <summary>
/// Makes the supplied word fit into the available width
/// </summary>
/// <returns>modified version of the word with inserted Returns at appropriate points</returns>
private static string MakeFit(string word, Unit width)
{
    var adjustedWord = new StringBuilder();
    var current = string.Empty;
    foreach (char c in word)
    {
        if (TooWide(current + c, width))
        {
            adjustedWord.Append(current);
            adjustedWord.Append(Chars.CR);
            current = c.ToString();
        }
        else
        {
            current += c;
        }
    }
    adjustedWord.Append(current);

    return adjustedWord.ToString();
}


Here is an example of the above in use
Attachment:
File comment: Example of word adjustment in column 3. Column 1 shows the unadulterated text
CellWidthAdjust.JPG
CellWidthAdjust.JPG [ 7.63 KiB | Viewed 16922 times ]


Top
 Profile  
Reply with quote  
PostPosted: Mon Jul 27, 2009 9:31 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3100
Location: Cologne, Germany
A soft hyphen can be inserted by hitting Alt+0173.

For static text: Change the string to
Code:
string someText = "I like to eat at Mc-Gru-de-ral-pos-ter-mus.";

(with Alt+0173 instead of '-') and MigraDoc will wrap the text at the allowed positions (showing a hyphen at the end of the line).

For non-static text: inserting soft-hyphens between syllables instead of CRs seems like a better and more readable solution.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon May 26, 2014 7:21 am 
Offline

Joined: Fri May 23, 2014 3:27 pm
Posts: 2
Orestone wrote:
I have a partial solution to this as shown below...

Thanks a bunch! This worked very nicely :)


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 12 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