Overlapping Anchored Objects

Posted by: Alberto on 6 January 2021, 1:55 am EST

    • Post Options:
    • Link

    Posted 6 January 2021, 1:55 am EST - Updated 29 September 2022, 11:12 pm EST

    Hello,

    I’m using TextLayout and it’s method AppendAnchoredObject with the Option wrapText true, so that the Text goes around each Object. The issue I face is that when having several Anchored Objects then it can happen that they will overlap, so that one Object will hide another one.

    Is it possible to solve this with an Option or do I have to do that manually?

    A manual workaround I could image is

    • "

      Append the Text & AnchoredObjects to the TextLayout.

      RecalculateGlyphs.

      PerformLayout(false).

      Get the Position of each AnchoredObject and store in a List MyObjects.

      Verify that the Objects within MyObjects does not overlap each other. If they do, then manually define a new position of the Objects.

      Remove all AnchoredObjects from TextLayout.

      Add all MyObjects to TextLayout.ObjectRects.

      PerformLayout(false)

      DrawTextLayout.

      "

    Another idea to explicitly reposition the Anchored Objects is to tweak individually the MinLeft/Top, MaxRight/Bottom Properties. But I still have to manually calculate the position of each Object.

    See the attached Code for an example of overlapping Anchored Objects.

    
        public static class SupportReport
        {
            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;
    
            public static void OverlappingAnchoredObjects(Object docObject)
            {
                var doc = (GrapeCity.Documents.Pdf.GcPdfDocument)docObject;
                var page = doc.NewPage();
                page.PaperKind = GrapeCity.Documents.Common.PaperKind.A4;
                page.Landscape = false;
                var pageSize = page.GetRenderSize(Resolution, Resolution);
                var graphics = page.Graphics;
                graphics.Resolution = Resolution;
    
                var rect = new System.Drawing.RectangleF(2.5f * CM, 2.5f * CM, pageSize.Width - 5 * CM, pageSize.Height - 5 * CM);
                graphics.DrawRectangle(rect, new GrapeCity.Documents.Drawing.Pen(System.Drawing.Color.Pink, 0.1f * MM));
                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,
                    AlignmentDelayToSplit = true,
                    EllipsisCharCode = '.', 
                    ParagraphAlignment = GrapeCity.Documents.Text.ParagraphAlignment.Near,
                    TextAlignment = GrapeCity.Documents.Text.TextAlignment.Leading,
                    LastLineIsEndOfParagraph = true,
                };
                var textFormat1 = new GrapeCity.Documents.Text.TextFormat()
                {
                    Font = GrapeCity.Documents.Text.FontCollection.SystemFonts.FindFamilyName("Arial", true, false),
                    FontSize = 6,
                    ForeColor = System.Drawing.Color.Red,
                    UseTypoMetrics = true, // cross platform independent
                    EnableFontHinting = true, // is default true
                    FontSizeInGraphicUnits = false, // typographic units
                };
                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.";
                lorem += " ";
                text.Append(lorem + lorem + lorem, textFormat1);
    
                foreach (var size in new int[] { 1, 2, 3, 4, 5, 6 })
                {
                    text.Append(lorem, textFormat1);
                    text.Append(" Anchor:", textFormat1);
                    var anchor1 = text.AppendAnchoredObject(size * CM, size * CM, true);
                    anchor1.AttachToPreviousChar = true;
                    anchor1.MinLeft = 0;
                    anchor1.MaxRight = pageSize.Width;
                    anchor1.KeepInHorizontalBounds = true;
                    anchor1.MinTop = 0;
                    anchor1.MaxBottom = pageSize.Height;
                    anchor1.KeepInVerticalBounds = true;
                    anchor1.HorizontalOffset = 0 * CM;
                    anchor1.VerticalOffset = 0;
                    anchor1.HorizontalPosition = GrapeCity.Documents.Text.HorizontalPosition.CharRightOut;
                    anchor1.VerticalPosition = GrapeCity.Documents.Text.VerticalPosition.CharBottomIn;
                }
    
                text.Append(lorem + lorem + lorem, textFormat1);
                text.RecalculateGlyphs();
                text.PerformLayout(false);
                graphics.DrawTextLayout(text, System.Drawing.PointF.Empty);
    
                // Draw all Anchored Objects
                var rnd = new Random();
                foreach (var a in text.AnchoredObjects) {
                    var randomColor = System.Drawing.Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256));
                    graphics.DrawRectangle(a.ContentObjectRect.ToRectangleF(), new GrapeCity.Documents.Drawing.Pen(randomColor, 0.3f * MM));
                }
            }
            #endregion
        }
    
    

    Thank You

  • Posted 7 January 2021, 9:26 pm EST

    Hello Alberto,

    The AppendAnchoredObject will work on the Text only and if you are using multiple Anchored Objects then you need to explicitly set their positions so that they do not overlap each other.

    If you need any other help, please let me know.

    Regards,

    Prabhat Sharma.

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels