Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions PdfSharpCore/Drawing.Layout/XTextFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,16 +219,22 @@ public void DrawString(string text, XFont font, XBrush brush, XRect layoutRectan
}

int count = _blocks.Count;
foreach (var line in lines)
int lineCount = lines.Length;
for (var lineIndex = 0; lineIndex < lineCount; lineIndex++)
{
var line = lines[lineIndex];
var lineBlocks = line as Block[] ?? line.ToArray();
if (Alignment == XParagraphAlignment.Justify)

// If alignment is Justify, leave last paragraph lines to be Left justified
if (Alignment == XParagraphAlignment.Justify && lineIndex != lineCount - 1 && lineBlocks.First().Alignment == XParagraphAlignment.Default)
{
var locationX = dx;
var gapSize = (layoutRectangle.Width - lineBlocks.Select(l => l.Width).Sum())/ (lineBlocks.Count() - 1);
var gapSize = (layoutRectangle.Width - lineBlocks.Select(l => l.Width).Sum()) /
(lineBlocks.Count(b => b.Width != 0) - 1);
foreach (var block in lineBlocks)
{
_gfx.DrawString(block.Text.Trim(), font, brush, locationX, dy + lineBlocks.First().Location.Y, XStringFormats.TopLeft);
if(block.Width != 0)
_gfx.DrawString(block.Text.Trim(), font, brush, locationX, dy + lineBlocks.First().Location.Y, XStringFormats.TopLeft);
locationX += block.Width + gapSize;
}
}
Expand Down
50 changes: 48 additions & 2 deletions SampleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,59 @@ public static void Main(string[] args)

// For checking purposes
renderer.DrawRectangle(translucentBrush, rect);


////////////////

// Use a new formatter to get rid of any cached values from above
formatter = new XTextFormatter(renderer);
formatter.AllowVerticalOverflow = true;
formatter.Alignment = XParagraphAlignment.Justify;
formatter.VerticalAlignment = XVerticalAlignment.Top;


// Test justified text without line breaks
text = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non sapien leo. Sed lacinia velit ex, id auctor tellus varius a. Cras scelerisque in risus vitae hendrerit. Duis venenatis felis in lacinia vestibulum. Proin mauris ex, efficitur nec tincidunt in, imperdiet eget risus. Nulla porttitor mollis pellentesque.";

// Define the area to draw the text
rect = new XRect(0, 0, 310, 200);
// Get the actual layout rectangle
rect = formatter.GetLayout(text, font, brush, rect);
// Move it to the desired location
rect.Location = new XPoint(40, 150);

// Draw the string with justify alignment
formatter.DrawString(text, font, brush, rect);
// For checking purposes
renderer.DrawRectangle(translucentBrush, rect);

////////////////

// Test justified text with line breaks
// First paragraph should be laid out identically to the one above
text = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non sapien leo. Sed lacinia velit ex, id auctor tellus varius a. Cras scelerisque in risus vitae hendrerit. Duis venenatis felis in lacinia vestibulum. Proin mauris ex, efficitur nec tincidunt in, imperdiet eget risus. Nulla porttitor mollis pellentesque.

Maecenas mollis sollicitudin felis at imperdiet. Duis dignissim purus quis interdum mattis. Nam sit amet quam quis enim hendrerit tincidunt. Aliquam euismod metus justo, non lobortis risus vehicula in. Pellentesque tempus, leo at placerat interdum, diam lectus gravida purus, id placerat justo quam nec mauris.";

// Define the area to draw the text
rect = new XRect(0, 0, 310, 200);
// Get the actual layout rectangle
rect = formatter.GetLayout(text, font, brush, rect);
// Move it to the desired location
rect.Location = new XPoint(40, 280);

// Draw the string with justify alignment
formatter.DrawString(text, font, brush, rect);
// For checking purposes
renderer.DrawRectangle(translucentBrush, rect);




SaveDocument(document, outName);

System.Console.WriteLine("Done!");
} // End Sub Main


} // End Class Program


Expand Down