Hello!
I have justified a report using methods below, they basically add spaces between words to justify the text of a report.
The problem is that in the development environment it works fine but in the UAT environment seems that method MeasureString it's not working as expected. There is a difference between calling " System.Drawing.Graphics::FromHwnd(System.IntPtr::Zero)" form Dev and from UAT?
Report from UAT:
Report from DEV:
-- Edited
Development is a Cloud-hosted environment and UAT is a tier-2 self-served environment.
---
Font font = new Font('Times New Roman', 11);
using (System.Drawing.Graphics g = System.Drawing.Graphics::FromHwnd(System.IntPtr::Zero))
{
str stringFormated = formatClass.justifyParagraph(textToBeFormated, widthTextPDF, 0, font);
}
protected str justifyLine(str _text, int _width, Font _font = defaultFont)
{
str test = 'test';
List wordsList = new List(Types::String);
wordsList = strSplit(_text, spaceChar);
if (wordsList.elements() < 2)
{
return _text;
}
// The last word does not count as it does not have spaces on the right
int numberOfWords = WordsList.elements() - 1;
SizeF sizeTextWithoutSpaces = defaultGraphics.MeasureString(strReplace(_text, spaceChar, ''), _font);
int wordsWidth = round(sizeTextWithoutSpaces.Width, 1);
// Width of the hairSpace
SizeF sizeTestPlusSpace = defaultGraphics.MeasureString(test + hairSpaceChar, _font);
SizeF sizeTestWithoutSpace = defaultGraphics.MeasureString(test, _font);
real hairSpaceCharWidth = sizeTestPlusSpace.Width - sizeTestWithoutSpace.Width;
// Average spacing between each word
int averageSpace = ((_width - wordsWidth) / numberOfWords) / hairSpaceCharWidth;
// Aditional spaces to add
real adjustSpace = (_width - (wordsWidth + (averageSpace * numberOfWords * hairSpaceCharWidth)));
str spaces = '';
str adjustedWords = '';
for (int h = 0; h < averageSpace; h++)
{
spaces += hairSpaceChar;
}
ListIterator iterator = new ListIterator(wordsList);
while (iterator.more())
{
adjustedWords += iterator.value() + spaces;
//Adjust the spacing if there's a reminder
if (adjustSpace > 0)
{
adjustedWords += hairSpaceChar;
adjustSpace -= hairSpaceCharWidth;
}
iterator.next();
}
//Removes extra spaces at the end
str resultWithoutSpace = subStr(adjustedWords, 1, strLen(adjustedWords) - averageSpace);
return resultWithoutSpace;
}
public str justifyParagraph(str _text, int _controlWidth, int _listSymbolLength, Font _font = defaultFont)
{
str result;
List paragraphsList = new List(Types::String);
paragraphsList = strSplit(_text, returnBreakLine);
ListIterator iterator = new ListIterator(paragraphsList);
ListIterator wordIterator;
List words = new List(Types::String);
str paragraph;
str line;
int paragraphWidth;
str wordTmp;
str resultStr;
str listSymbol;
boolean isFirstLine = true;
while (iterator.more())
{
paragraph = iterator.value();
line = '';
wordTmp = '';
SizeF size = defaultGraphics.MeasureString(paragraph, _font);
paragraphWidth = round(size.Width, 1);
if (paragraphWidth > _controlWidth)
{
//Get all paragraph words, add a normal space and calculate when their sum exceeds the constraints
words = strSplit(paragraph, spaceChar);
wordIterator = new ListIterator(words);
if (wordIterator.more())
{
line = wordIterator.value() + spaceChar;
}
wordIterator.next();
while (wordIterator.more())
{
wordTmp = wordIterator.value();
str tmpLine = line + (wordTmp + spaceChar);
SizeF tmpLineSize = defaultGraphics.MeasureString(tmpLine, _font);
if (round(tmpLineSize.Width, 1) > _controlWidth)
{
if (_listSymbolLength && isFirstLine)
{
[listSymbol, line] = this.getListSymbol(line, _listSymbolLength);
}
//Max lenght reached. Justify the line and step back
resultStr = this.justifyLine(strRTrim(line), _controlWidth, _font);
if (_listSymbolLength)
{
resultStr = this.addTabSpace(isFirstLine, listSymbol) + resultStr;
}
result += resultStr + returnBreakLine;
line = '';
isFirstLine = false;
}
else
{
//Some capacity still left
line += (wordTmp + spaceChar);
wordIterator.next();
}
}
//Adds the remainder if any
if (line != '')
{
if (_listSymbolLength)
{
line = this.addTabSpace(isFirstLine, listSymbol) + line;
}
result += line + returnBreakLine;
isFirstLine = true;
}
}
else
{
if (_listSymbolLength)
{
if (isFirstLine)
{
[listSymbol, paragraph] = this.getListSymbol(paragraph, _listSymbolLength);
}
paragraph = this.addTabSpace(isFirstLine, listSymbol) + paragraph;
}
result += paragraph + returnBreakLine;
isFirstLine = true;
}
iterator.next();
}
return strRTrim(result);
}
protected container getListSymbol(str _text, int _listSymbolLength)
{
str listSymbol = subStr(_text, 1, _listSymbolLength);
str textResult = subStr(_text, _listSymbolLength + 1, strLen(_text));
textResult = strLTrim(textResult);
return [listSymbol, textResult];
}
protected str addTabSpace(boolean _isFirst, str _listSymbol)
{
if (_isFirst)
{
return _listSymbol + strRep(spaceChar, 4);
}
else
{
return strRep(spaceChar, 4 + strLen(_listSymbol)) + hairSpaceChar;
}
}
Thanks,
Anna