We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Text boxes formatting not working properly

There is a regression in Text boxes functionality.
Format change affects only the first paragraph in the text box.

            Workbook workbook = new Workbook();
            Worksheet worksheet = workbook.Worksheets.First();

            Shape tb = worksheet.Shapes.AddTextBox(1, 0, 1, 0, 200, 500);
            tb.Text =
                "Text paragraph 1" + Environment.NewLine +
                "Text paragraph 2" + Environment.NewLine +
                "Text paragraph 3" + Environment.NewLine +
                "Text paragraph 4" + Environment.NewLine
                ;
            tb.Font.Name = "Arial Narrow";
            tb.Font.Size = 25;

Result:

Clipboard Image (4).png (4.0 KB)

If text is changed after formatting, the formatting is fixed. It’s a w/a, but the issue needs to be fixed.

An update.
Saving to pdf, even the first paragraph is no good.
With more complicated formatting (I’ve checked in our real reports), even work around assigning text after formatting not working.
So the issue looks very urgent.

I’m also attaching Demo tool, showing the text boxes formatting problems I mentioned.

Aspose Demo — Text boxes formatting ignored.zip (106.7 KB)

@alex-rkn

We were able to observe this issue and logged it in our database for investigation and for a fix. Once, the issue is resolved or we have some other news for you, we will update you asap.

This issue has been logged as

  • CELLSNET-45661 - Text boxes formatting is not working properly in Excel and Pdf

C#

Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];

Shape tb = worksheet.Shapes.AddTextBox(1, 0, 1, 0, 200, 500);
tb.Text =
    "Text paragraph 1" + Environment.NewLine +
    "Text paragraph 2" + Environment.NewLine +
    "Text paragraph 3" + Environment.NewLine +
    "Text paragraph 4" + Environment.NewLine
    ;
tb.Font.Name = "Arial Narrow";
tb.Font.Size = 25;

workbook.Save("output.xlsx");
workbook.Save("output.pdf"); 

Download Link:
Ouput Excel and Pdf File.zip (16.9 KB)

@alex-rkn,

While we will be working over to fix the issue especially for rendering to PDF file format, could you try the following sample code segment that will work at least for Excel file rendering. I have tested and it works fine in the output Excel file:
e.g
Sample code:

 Workbook workbook = new Workbook();
            Worksheet worksheet = workbook.Worksheets[0];

            Shape tb = worksheet.Shapes.AddTextBox(1, 0, 1, 0, 200, 500);
            tb.Text =
                "Text paragraph 1" + Environment.NewLine +
                "Text paragraph 2" + Environment.NewLine +
                "Text paragraph 3" + Environment.NewLine +
                "Text paragraph 4" + Environment.NewLine
                ;
      
            TextParagraphCollection paragraphs = tb.TextBody.TextParagraphs;
            for (int i = 0; i < paragraphs.Count; i++)
            {
               TextParagraph p = paragraphs[i];
               p.Font.Name = "Arial Narrow";
               p.Font.Size = 25;               

            } 
            
            workbook.Save("e:\\test2\\out1.xlsx");

Hope, this helps a bit.

@alex-rkn

You need to access Font object, modify and re-assign it. It will then fix your issue. Please see the following code for your reference. Please also check the output Excel and Pdf files generated by the code.

Download Link:
Output Excel and Pdf.zip (14.5 KB)

C#

Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];

Shape tb = worksheet.Shapes.AddTextBox(1, 0, 1, 0, 200, 500);
tb.Text =
    "Text paragraph 1" + Environment.NewLine +
    "Text paragraph 2" + Environment.NewLine +
    "Text paragraph 3" + Environment.NewLine +
    "Text paragraph 4" + Environment.NewLine
    ;

//Access the Font object
Aspose.Cells.Font font = tb.Font;

//Modify it
font.Name = "Arial Narrow";
font.Size = 25;

//Re-assign
tb.Font = font;

workbook.Save("output.xlsx");
workbook.Save("output.pdf");

Thank you, Shakeel,
Re-assigning Font property helps! It’s no blocker for us any more.
I still believe you’re going to fix it?

@alex-rkn

MS Excel splits the text of the shape to several paragraphs and each paragraph has different font setting since MS Office 2007. We can only return the font of the first paragraph with Shape.Font. So, please set the Shape.Font after you change the font now.

Perfect. Thanks!