Overlap issue when inserting Image then inset Textbox shape

Dear Team,
We are using the below block of code to insert image and textbox shape to a word file using Aspose.Words versrion 23.7.0, but it is working with one word file and not working with other word file.
what need to be done here is adding image in the middle after last paragraph then add textbox shape below under of this image, but the problem is Textbox shape is overlap image and comes on top of the image.
attaching the working one and not working one for your reference
I appreciate your support understanding why code is not working for such case
Files:
Files.zip (55.0 KB)
Code:

var coverLetterPath = corr.Documents.OrderBy(d => d.LastModifiedOn).Last(d => d.TypeId == (byte)FileTypes.CoverFile).DocumentPath;
WordDocument doc = new WordDocument(coverLetterPath);
doc.UpdateFields();
foreach (Field field in doc.Range.Fields)
{
    if (field.Type == FieldType.FieldDocProperty)
    {
        if (field.Result.ToString().StartsWith("Error"))
        {
            field.Remove();
        }
    }
}
doc.Save(coverLetterPath);
doc = new WordDocument(coverLetterPath);
doc.LayoutOptions.TextShaperFactory = Aspose.Words.Shaping.HarfBuzz.HarfBuzzTextShaperFactory.Instance;
var corrPath = _configuration.GetSection("SiteSettings").GetSection("UploadFolder").Value;
corrPath = Path.Combine(corrPath, corr.Id.ToString());
if (!Directory.Exists(corrPath)) Directory.CreateDirectory(corrPath);
var docs = new List<DocumentDto>();
int orginalPageCount = doc.PageCount;

await AddDummyImage(corr, doc);
string wordpath = coverLetterPath.Replace(".docx", "withdummy.docx");
doc.Save(wordpath);
 private async Task AddDummyImage(CorrespondenceDto corr, WordDocument doc)
{
    var dummySignaturePath = @"D:\Source\wwwroot\images\auth\Image.jpg";

    DocumentBuilder builder = new DocumentBuilder(doc);
    builder.MoveToDocumentEnd();
    builder.InsertBreak(BreakType.ParagraphBreak);
    builder.CurrentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
    builder.Font.Underline = Underline.None;
    var img = builder.InsertImage(dummySignaturePath, 110, 45);
    img.RelativeVerticalPosition = RelativeVerticalPosition.Page;
    img.VerticalAlignment = VerticalAlignment.Center;
    builder.InsertBreak(BreakType.ParagraphBreak);
    var shape = builder.InsertShape(ShapeType.TextBox, 290, 88);
    shape.RelativeVerticalPosition = RelativeVerticalPosition.Page;
    shape.VerticalAlignment = VerticalAlignment.Center;
    shape.Stroked = false;
    shape.TextBox.FitShapeToText = true;
    shape.AllowOverlap = false;
    shape.LastParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
    builder.MoveTo(shape.LastParagraph);
    builder.ParagraphFormat.SpaceAfter = 0;
    builder.Font.Name = "Calibri (Body)";
    builder.Font.Size = 11;
    builder.Font.Bold = true;
    builder.Font.SizeBi = 11;
    builder.Font.BoldBi = true;
    builder.Font.Bidi = true;
    builder.Writeln(" الأسم ");
    builder.ParagraphFormat.SpaceAfter = 8;
    builder.Writeln(" المسمى ");
    builder.ParagraphFormat.SpaceAfter = 0;
    builder.Writeln(" name ");
    builder.Writeln(" Job Title ");
    doc.AutomaticallyUpdateStyles = true;
    doc.UpdatePageLayout();
    doc.UpdateFields();
}

@atalal The problem occurs because in the problematic document line spacing rule is set to exactly. You should reset it to at least to make the code work for the problematic document. Please see the following modified code:

private static void AddDummyImage(Document doc)
{
    var dummySignaturePath = @"C:\Temp\img.png";

    DocumentBuilder builder = new DocumentBuilder(doc);
    builder.MoveToDocumentEnd();
    // Reset paragraph LineSpacingRule
    builder.ParagraphFormat.LineSpacingRule = LineSpacingRule.AtLeast;
    builder.InsertBreak(BreakType.ParagraphBreak);
    builder.CurrentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
    builder.Font.Underline = Underline.None;
    var img = builder.InsertImage(dummySignaturePath, 110, 45);
    img.RelativeVerticalPosition = RelativeVerticalPosition.Page;
    img.VerticalAlignment = VerticalAlignment.Center;
    builder.InsertBreak(BreakType.ParagraphBreak);
    var shape = builder.InsertShape(ShapeType.TextBox, 290, 88);
    shape.RelativeVerticalPosition = RelativeVerticalPosition.Page;
    shape.VerticalAlignment = VerticalAlignment.Center;
    shape.Stroked = false;
    shape.TextBox.FitShapeToText = true;
    shape.AllowOverlap = false;
    shape.LastParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
    builder.MoveTo(shape.LastParagraph);
    builder.ParagraphFormat.SpaceAfter = 0;
    builder.Font.Name = "Calibri (Body)";
    builder.Font.Size = 11;
    builder.Font.Bold = true;
    builder.Font.SizeBi = 11;
    builder.Font.BoldBi = true;
    builder.Font.Bidi = true;
    builder.Writeln(" الأسم ");
    builder.ParagraphFormat.SpaceAfter = 8;
    builder.Writeln(" المسمى ");
    builder.ParagraphFormat.SpaceAfter = 0;
    builder.Writeln(" name ");
    builder.Writeln(" Job Title ");
    doc.AutomaticallyUpdateStyles = true;
    doc.UpdatePageLayout();
    doc.UpdateFields();
}

@alexey.noskov thanks for your quick response, it is working fine with me
You are a guru!

1 Like