PDFsharp & MigraDoc Foundation https://forum.pdfsharp.net/ |
|
Migra Doc - Autofit text into table cells? https://forum.pdfsharp.net/viewtopic.php?f=2&t=747 |
Page 1 of 1 |
Author: | dherer [ Tue Jun 02, 2009 5:02 pm ] |
Post subject: | Migra Doc - Autofit text into table cells? |
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 |
Author: | Thomas Hoevel [ Wed Jun 03, 2009 4:36 pm ] |
Post subject: | |
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. |
Author: | Orestone [ Fri Jul 24, 2009 5:23 am ] |
Post subject: | Re: Migra Doc - Autofit text into table cells? |
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 ![]() 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 [ 7.63 KiB | Viewed 16936 times ] |
Author: | Thomas Hoevel [ Mon Jul 27, 2009 9:31 am ] |
Post subject: | Re: Migra Doc - Autofit text into table cells? |
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. |
Author: | mdonatas [ Mon May 26, 2014 7:21 am ] |
Post subject: | Re: Migra Doc - Autofit text into table cells? |
Orestone wrote: I have a partial solution to this as shown below... Thanks a bunch! This worked very nicely ![]() |
Page 1 of 1 | All times are UTC |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |