AutoFitRows and AutoFitColumn Not Handling Large Text

Hi Aspose Support Team,

I am facing issues with AutoFitColumn(i) and AutoFitRows(true) in Aspose.Cells for .NET when exporting to PDF, especially when working with large amounts of wrapped text.

AutoFitColumn Issue:
When I apply AutoFitColumn(i) to a cell containing very large text with wrapping enabled, the text gets clipped on the right side.

AutoFitRows Issue:
For cells with large vertical content (many lines of wrapped text), I am using AutoFitRows(true). However, the text gets clipped vertically and does not continue onto the next page in the PDF. It seems like the rendering engine does not break or overflow content to the next page — instead, it just cuts it off.

code sample
foreach (Worksheet sheet in workbook.Worksheets)
{
if (_type == SaveFormat.Xlsx || _type == SaveFormat.Pdf)
{
for (int i = 0; i < ReportFields.Count; i++)
{
ReportField reportField = ReportFields[i];

					if (reportField.Width.HasValue)
					{

						sheet.Cells.SetColumnWidthPixel(i, reportField.Width.Value);
					}
					else if (_type == SaveFormat.Xlsx || _type == SaveFormat.Pdf)
					{
						sheet.AutoFitColumn(i);
					}
				}

				if (_type == SaveFormat.Pdf)
					sheet.PageSetup.Orientation = PageOrientationType.Landscape;

				sheet.AutoFitRows(true);
				sheet.FreezePanes(1, 0, 1, ReportFields.Count);
			}
		}

pdfs
a (14).pdf (14.5 KB)

a (13).pdf (14.7 KB)

@royal.monteiro

It sounds like you’re experiencing issues with the AutoFitColumn and AutoFitRows methods in Aspose.Cells for .NET when dealing with large amounts of wrapped text. Here’s a structured approach to resolve these issues:

Cause:

The problems you are encountering with AutoFitColumn and AutoFitRows are likely due to the timing of when these methods are called in relation to setting the cell’s text and formatting. If the methods are invoked before the content is fully set, they may not function as expected.

Solution:

  1. AutoFitColumn Issue:

    • Ensure that the AutoFitColumn method is called after setting the cell’s text and formatting. If the text is set after calling AutoFitColumn, it may not adjust correctly.
    • Utilize the AutoFitterOptions class to customize the autofitting behavior. Set options like AutoFitMergedCellsType and AutoFitWrappedTextType to better handle your specific case.
  2. AutoFitRows Issue:

    • Similar to the column issue, ensure that the row height adjustment is performed after all content is set. If the content is dynamic, call AutoFitRows after all data is populated.
    • Use the AutoFitterOptions to specify how you want the rows to behave with wrapped text. For example, set OnlyAuto to true to only adjust rows that haven’t been manually set.

Next Steps:

Here’s a minimal code snippet that demonstrates how to use AutoFitRows and AutoFitColumns with options:

using Aspose.Cells;

var workbook = new Workbook();
var sheet = workbook.Worksheets[0];

// Add sample data
sheet.Cells["A1"].PutValue("This is a long text that needs to be wrapped and autofitted.");
sheet.Cells["A1"].SetStyle(new Style { IsTextWrapped = true });

// Create AutoFitterOptions
var options = new AutoFitterOptions
{
    AutoFitMergedCellsType = AutoFitMergedCellsType.EachLine,
    AutoFitWrappedTextType = AutoFitWrappedTextType.Paragraph
};

// Autofit columns and rows
sheet.AutoFitColumns(options);
sheet.AutoFitRows(options);

// Save the workbook
workbook.Save("output.pdf", SaveFormat.Pdf);

This code sets up a worksheet, adds a long text with wrapping enabled, and then applies autofitting with specific options. Adjust the options as needed based on your requirements.

Please try implementing these suggestions, and let me know if this resolves your issues or if you have any further questions!

still i am facing the same issue i used AutoFitterOptions as well and textwrapping i have done before also

@royal.monteiro
The three posts you have created consecutively all belong to the same issue. We will reply to you in this new post. Additionally, your sample code contains unknown properties. Would you like to provide runnable test code and sample files? We will check it soon.

@royal.monteiro
The width and height of cells are limited. If your text is too long to fully display. When adjusting column width or row height alone cannot display all the text, you can merge cells to display all the text.

Regarding the limitations of Excel, please refer to the following document.

i dont have runnable code because code is too long
if the text is more than 1000 characters when we export into pdf it will not give us entire text as you can see in pdf
and vertically same issue it will not create new page it will just make the text to clipped off

is there any limitations in pdf while exporting?

@royal.monteiro
The text you set is too long and has exceeded the cell display limit. If you need to display all text, please display the text in the merged cells. Regarding how to merge cells, please refer to the following document.

Can you provide details about the display limitations of a cell? I do not want to merge cells. My requirements are:

  1. If the text is very long, I want it to wrap within the same page instead of being clipped off horizontally.
  2. If the text is very long vertically (i.e., spans multiple lines), I want it to move to the next page instead of being clipped off.

@royal.monteiro
Regarding the limitations of Excel, please refer to the following document.

Feature            	        Maximum limit
Column width	            255 characters
Row height	                409 points

@royal.monteiro

If the text is displayed in a merged cell, and when exporting the file to PDF, the text exceeds the page size of the PDF, the text will appear on the next page as you expect.

image.png (19.1 KB)
image.png (10.0 KB)

@John.He
In the first image, you can see that the text is printed in a single cell but is not fully visible—it appears to be hidden. Why it is hidden?

In the second image, there is also only one cell where the text is printed. However, since the text is very long vertically, it is not fully visible and is getting hidden. Why it is hidden?

@royal.monteiro
Thank you for your feedback. Your observation is correct. PDF itself does not have a text length limit. But in Excel, cells have column width and row height restrictions. When you view a file in Excel, the text you see is only partial text. This is consistent with the PDF export result. Only display the visible portion of the text.

@royal.monteiro
Aspose.Cells follows the rules and specifications of Excel. If you print and preview the file in Excel, you will find that the preview result is consistent with the PDF output result.

I wanted to confirm — is this a limitation in Aspose.Cells?

Also, is there any solution or workaround to make sure the full text is printed properly in the PDF, both when the content is very long horizontally or vertically?

Right now, in both cases, some of the content gets hidden or clipped.
My requirement is to show the complete content clearly in the PDF.

@royal.monteiro1,

This is not an issue with Aspose.Cells but rather a limitation imposed by MS Excel. Aspose.Cells processes Excel files into its object model and then converts them to PDF. Therefore, any restrictions forced by MS Excel for its native file formats will also apply during the PDF rendering process. Please note (as we told you earlier) that there are certain limitations (of chars/points) regarding the width of a column and the height of a row, which we need to adhere to and cannot exceed beyond their defined limits, so a certain numbers of words/chars can be displayed in a cell.

Handling large amounts of data in a single cell can be quite challenging. As suggested earlier, you might consider merging several cells and adjusting the width of the merged cell to a specific value while setting its height to the maximum possible limit. Once done, you can insert your data into the merged cell to ensure it displays properly. You could test this approach in MS Excel to see if it meets your requirements. Alternatively, instead of placing all the information in a single cell, you might find it more effective to distribute the data across multiple adjacent cells. This method can help prevent your content from being clipped.

@royal.monteiro1 ,

If the column width is larger than page width and text wrap is set, the text will not be clipped in horizontal direction while rendering to pdf. Please use a long meaningful text(e.g. a long article) to test instead of repeated text. Using repeated text, it is not clear to distinguish the text is clipped in horizontal or vertical direction.