Partial Solution for Justified Text

0 Answers 124 Views
PdfProcessing
Aidan
Top achievements
Rank 1
Aidan asked on 02 Dec 2022, 11:05 PM | edited on 05 Dec 2022, 05:07 PM

Hello,

This post is not a question.  I simply wanted to share my partial solution for those who may need it.

Below is a manual method for creating a Block with justified text in the PDFProcessing API.  This makes use of the Block object's Measure method and the WordSpacing property to adjust each line until the indicated width is filled. 

PLEASE NOTE:

  • While I tried to account for it, I have not tested this code for compatibility with multiple paragraphs or blank lines. 
  • It is intended for small portions of text and will not likely scale well with a full document.

Best Regards,

Aidan D.

TAGS FOR GOOGLE: Justify, Alignment

using System; using System.Collections.Generic; using Telerik.Windows.Documents.Fixed.Model.Editing; using Telerik.Windows.Documents.Fixed.Model.Fonts; classTextJustify { public static void InsertJustifiedBlock(FixedContentEditor PageEditor, string Text, int TextBoxWidth) { Block justifiedBlock = CreateJustifiedBlock(Text, TextBoxWidth, PageEditor.TextProperties.Font, PageEditor.TextProperties.FontSize); PageEditor.DrawBlock(justifiedBlock); } public static Block CreateJustifiedBlock(string Text, int TextBoxWidth, FontBase Font, double FontSize) { Block justifiedBlock = new Block(); double currentSpacing = 0; double spacingStep = 1; string currentLine; if (Text.Length > 0) { { var withBlock = justifiedBlock.TextProperties; // Default word spacing. Must be initialized to 0 before use. This defaults to Nothing/Null// and will not function correctly if not initialized. withBlock.WordSpacing = 0; withBlock.Font = Font; withBlock.FontSize = FontSize; } // We need a seperate block to use as a measuring tool using the same text properties as the justified block. Block measuringBlock = new Block(justifiedBlock); // Make sure the WordSpacing property is initialized to zero. // Break the text string into lines, adding as many full words as we can to each line.// This does not support hyphenation. List<string> lines = breakIntoLines(Text, TextBoxWidth, ref measuringBlock); for (int i = 0; i <= lines.Count - 1; i++) { currentLine = lines[i]; // For all except the last line, space out the words to fill the allowed space.if (i < (lines.Count - 1)) { do { // Set the word spacing before inserting the text. measuringBlock.TextProperties.WordSpacing = currentSpacing; // We need to reinsert the text each time the spacing changes since the spacing is only applied on insert measuringBlock.Clear(); measuringBlock.InsertText(currentLine); measuringBlock.Measure(); if (measuringBlock.DesiredSize.Width > TextBoxWidth && spacingStep != 0.01) { // Step back by one then narrow in at finer increments currentSpacing -= spacingStep; spacingStep /= 10; currentSpacing += spacingStep; continue; } currentSpacing += spacingStep; } while (measuringBlock.DesiredSize.Width > TextBoxWidth && spacingStep == 0.01); // Reduce by one step to bring it back into allowed width currentSpacing -= spacingStep; } // Add the line of text with the appropriate spacing.using (justifiedBlock.SaveTextProperties()) { justifiedBlock.TextProperties.WordSpacing = currentSpacing; justifiedBlock.InsertText(currentLine); justifiedBlock.InsertLineBreak(); } // Reset for next line currentSpacing = 0; spacingStep = 1; } } return justifiedBlock; } private static List<string> breakIntoLines(string Text, int maxLineWidth, ref Block measuringBlock) { List<string> sResults = new List<string>(); string[] lines = Text.Split(newstring[] { "\n", "\r\n" }, StringSplitOptions.None); foreach (string line in lines) { string[] words = line.Split(newstring[] { " " }, StringSplitOptions.RemoveEmptyEntries); sResults.Add(""); // Initialize the line. This may remain empty if the line is blank.for (int i = 0; i <= words.Length - 1; i++) { if (sResults[sResults.Count - 1].Length == 0) sResults[sResults.Count - 1] = words[i]; else { string testLine = string.Concat(sResults[sResults.Count - 1], " ", words[i]); measuringBlock.InsertText(testLine); measuringBlock.Measure(); if (measuringBlock.DesiredSize.Width <= maxLineWidth) // Replace with new, longer line sResults[sResults.Count - 1] = testLine; else// Start new line sResults.Add(words[i]); // Remove text from measuring block to reset for next word measuringBlock.Clear(); } } } return sResults; } }


Dimitar
Telerik team
commented on 05 Dec 2022, 10:48 AM

Hi Aidan,

Thank you for sharing your solution. I have added some Telerik points to your account.

Regards,

Dimitar

No answers yet. Maybe you can help?

Tags
PdfProcessing
Asked by
Aidan
Top achievements
Rank 1
Share this question
or