diff --git a/PdfSharpCore/Drawing.Layout/XTextFormatter.cs b/PdfSharpCore/Drawing.Layout/XTextFormatter.cs index c336705..749139c 100644 --- a/PdfSharpCore/Drawing.Layout/XTextFormatter.cs +++ b/PdfSharpCore/Drawing.Layout/XTextFormatter.cs @@ -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; } } diff --git a/SampleApp/Program.cs b/SampleApp/Program.cs index 2a04942..ca9d3bd 100644 --- a/SampleApp/Program.cs +++ b/SampleApp/Program.cs @@ -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