Posted 3 March 2021, 3:54 am EST - Updated 29 September 2022, 11:10 pm EST
Hello,
I found an inconsistent behavior of the TextLayout, when Split is not used.
When Split is used then it works as espected. The text is split to the remaining new TextLayout. See Page 1 and Page 4.
When Split and ObjectRects are not used then the Text is drawed outside the LayoutRectangle (red) and the resulting ContentRectangle (green) is much bigger, till the end of the Page. See Page 2.
When Split is not used but ObjectsRects is defined (without adding any Object), so it should have no effect at all, then the Text is not drawed ouside the LayoutRectangle but the ContentRectangle is still bigger than the LayoutRectangle. See Page 3.
What is incorrect in my code?
using System;
namespace Messerli.LayouterOne.Infrastructure.BasicPdf.Test.BugReport
{
/// <summary>
/// I found an inconsistent behavior of the TextLayout, when Split is not used.
/// When Split is used then it works as espected.The text is split to the remaining new TextLayout.See Page 1 and Page 4.
/// When Split and ObjectRects are not used then the Text is drawed outside the LayoutRectangle(red) and the
/// resulting ContentRectangle(green) is much bigger, till the end of the Page.See Page 2.
/// When Split is not used but ObjectsRects is defined(without adding any Object), so it should have no effect at all,
/// then the Text is not drawed ouside the LayoutRectangle but the ContentRectangle is still bigger than the LayoutRectangle. See Page 3.
/// </summary>
public class TextLayoutOutsideRectangle
{
public const float Resolution = 72f;
public const float Point = 1f;
public const float Inch = Point * Resolution;
public const float CM = Inch / 2.54f;
public const float MM = CM / 10;
GrapeCity.Documents.Pdf.GcPdfDocument Document;
GrapeCity.Documents.Text.TextFormat TextFormat1;
GrapeCity.Documents.Pdf.GcPdfGraphics Graphics;
GrapeCity.Documents.Pdf.Page Page;
System.Drawing.SizeF PageSize;
System.Drawing.RectangleF TextRect;
public static void ShowIssue(Object doc)
{
new TextLayoutOutsideRectangle((GrapeCity.Documents.Pdf.GcPdfDocument)doc).DrawPages();
}
TextLayoutOutsideRectangle(GrapeCity.Documents.Pdf.GcPdfDocument doc)
{
Document = doc;
TextFormat1 = new GrapeCity.Documents.Text.TextFormat()
{
Font = GrapeCity.Documents.Text.FontCollection.SystemFonts.FindFamilyName("Segoe UI", false, true),
FontSize = 48,
ForeColor = System.Drawing.Color.Black,
UseTypoMetrics = true, // cross platform independent
EnableFontHinting = true, // is default true
FontSizeInGraphicUnits = false, // typographic units
};
}
void AddPage()
{
Page = Document.NewPage();
Page.PaperKind = GrapeCity.Documents.Common.PaperKind.A4;
Page.Landscape = false;
PageSize = Page.GetRenderSize(Resolution, Resolution);
Graphics = Page.Graphics;
Graphics.Resolution = Resolution;
TextRect = new System.Drawing.RectangleF(0, 0, PageSize.Width, PageSize.Height);
TextRect.Inflate(-2.5f * CM, -2.7f * CM);
Graphics.DrawRectangle(TextRect, new GrapeCity.Documents.Drawing.Pen(System.Drawing.Color.Red, 2f * MM));
}
void DrawPage(bool useSplit, bool useObjectRect)
{
AddPage();
// create TextLayout
var rect = TextRect;
var text = new GrapeCity.Documents.Text.TextLayout(Graphics.Resolution)
{
MarginLeft = rect.X,
MarginTop = rect.Y,
MarginRight = 0,
MarginBottom = 0,
MaxWidth = rect.Right,
MaxHeight = rect.Bottom,
EllipsisCharCode = '.',
ParagraphAlignment = GrapeCity.Documents.Text.ParagraphAlignment.Near,
TextAlignment = GrapeCity.Documents.Text.TextAlignment.Leading,
//AlignmentDelayToSplit = useSplit,
//LastLineIsEndOfParagraph = true,
//HonorLastLineSpacing = true,
//AllowOverhangingWords = true,
//TrimmingGranularity = GrapeCity.Documents.Text.TrimmingGranularity.Character,
};
string lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer sodales iaculis dictum. Nam in porta dolor. Pellentesque neque orci, maximus in accumsan eget, tincidunt nec eros. Aenean vitae nisi vel purus maximus dignissim. Proin sagittis elementum pharetra. Mauris eu tellus pretium, luctus dui eget, pharetra velit. Nunc fermentum tortor ante, eget sodales sapien imperdiet ut.";
text.Append(string.Format("Page: {0}, Split: {1}, ObjectRect: {2}. ", Page.Index+1, useSplit, useObjectRect) + lorem, TextFormat1);
text.RecalculateGlyphs();
// Add Optional ObjectRectangles
if (useObjectRect)
{
// add object list. But without objects. It should have no effect.
text.ObjectRects = new System.Collections.Generic.List<GrapeCity.Documents.Text.ObjectRect>();
}
// Perform Layout
text.PerformLayout(false);
// Split if requested but ignore the resulting text.
if (useSplit)
{
text.Split(new GrapeCity.Documents.Text.TextSplitOptions(), out GrapeCity.Documents.Text.TextLayout _);
}
// Draw TextLayout
Graphics.DrawTextLayout(text, System.Drawing.PointF.Empty);
// Draw Rectangle of the resulting ContentRectangle
Graphics.DrawRectangle(text.ContentRectangle, new GrapeCity.Documents.Drawing.Pen(System.Drawing.Color.LightGreen, 1f * MM));
}
void DrawPages()
{
DrawPage(true, false);
DrawPage(false, false);
DrawPage(false, true);
DrawPage(true, true);
}
}
}
Thank You
Alberto