Half row is hidden when converting excel to pdf

Hi, I’m trying to convert some excel to pdf file, but when I set some cell for text wrap (IsTextWrapped = true) and I run the autofitrow method

var autoFitterOptions = new AutoFitterOptions()
                    {
                        IgnoreHidden = true,
                        FormatStrategy = CellValueFormatStrategy.DisplayString,
                        MaxRowHeight = 36,
                        AutoFitWrappedTextType = AutoFitWrappedTextType.Paragraph
                    };
                    activeWorksheet.AutoFitRow(row.Index, row.FirstCell.Column, row.LastCell.Column, autoFitterOptions);

seems like half of the first row is hidden.
anything I can do to push it inside? I’ve played with FormatStrategy but seems the best match was CellValueFormatStrategy.DisplayString
heres photo of what I see today:
image.png (16.3 KB)
as you can see, the email is cut in the middle

@zivdaniel can you please attach your source file?

Hi,
We have an example source code, but it’s a little long - attached.
if need additional info - please tell me.
output.zip (114.7 KB)

@zivdaniel,

Please try our latest version/fix: Aspose.Cells for .NET v23.3 (Releases | NuGet).

I guess you shared your input HTML file as text file. So, I opened your input.txt file into notepad manually and saved it as “input.html”. Then I tested it with Aspose.Cells for .NET v23.3 to save to PDF using the following sample code, it works fine, and the output PDF is Ok.
e.g.
Sample code:

Workbook workbook = new Workbook("g:\\test2\\input.html");
var autoFitterOptions = new AutoFitterOptions()
{
    IgnoreHidden = true,
    FormatStrategy = CellValueFormatStrategy.DisplayString,
    MaxRowHeight = 36,
    AutoFitWrappedTextType = AutoFitWrappedTextType.Paragraph
};

workbook.Worksheets[0].AutoFitRows(autoFitterOptions);

PdfSaveOptions options = new PdfSaveOptions();
options.AllColumnsInOnePagePerSheet = true;
workbook.Save("g:\\test2\\out1.pdf", options);

Please find attached the output PDF for your reference.
out1.pdf (180.3 KB)

In case, you still find any issue, kindly do provide a sample project (complete source code without compilation errors) to reproduce the issue on our end, we will check it soon.

Hi @amjad.sahi,

Please notice from the output - seems like the long text in the email column - is not wrapped.
image.png (6.4 KB)

The code you show me here is very similar to mine. expect
activeWorksheet.AutoFitRow(row.Index, row.FirstCell.Column, row.LastCell.Column, autoFitterOptions); - that since I want to autofit only the specific row and not the entier workbook( the max row height affecting the design and no need in case of no wrap needed)

this is the logic in my side for text:

  double minTextLengthToBrake = Convert.ToInt32(_configuration["WorkbookDefaultStyle:MinTextLengthToBrake"]);

        for (int i = 0; i < activeWorksheet.Cells.Rows.Count; i++)
        {
            var row = activeWorksheet.Cells.Rows[i];
            if (row == null || row.LastCell == null || row.FirstCell == null)
            {
                continue;
            }

            for (int columnI = 0; columnI < row.LastCell.Column; columnI++)
            {
                var cell = row.GetCellOrNull(columnI);

                if (cell == null
                    || cell.Type == CellValueType.IsNull
                    || cell.IsMerged)
                {
                    continue;
                }

                if (!string.IsNullOrWhiteSpace(cell.Value.ToString()))
                {
                    var style = cell.GetStyle();
                    style.HorizontalAlignment = TextAlignmentType.Left;
                    cell.SetStyle(style);
                    cell.Value = cell.Value.ToString().Trim();
                }

//=>  THIS IS THE SECTION FOR THE WORD WRAP
                if (cell.Value.ToString().Trim().Length >= minTextLengthToBrake)
                {
                    var style = cell.GetStyle();
                    style.IsTextWrapped = true;
                    cell.SetStyle(style);
                    var autoFitterOptions = new AutoFitterOptions()
                    {
                        IgnoreHidden = true,
                        FormatStrategy = CellValueFormatStrategy.DisplayString,
                        MaxRowHeight = 36,
                        AutoFitWrappedTextType = AutoFitWrappedTextType.Paragraph
                    };
                    activeWorksheet.AutoFitRow(row.Index, row.FirstCell.Column, row.LastCell.Column, autoFitterOptions);
                }
            }
        }

@zivdaniel,
We use your sample code for testing, and we can get the correct results through the latest version of Aspose.Cells 23.3. Please check the attachment. out.zip (202.5 KB)

Workbook workbook = new Workbook(filePath + "input.html");

//double minTextLengthToBrake = Convert.ToInt32(_configuration["WorkbookDefaultStyle:MinTextLengthToBrake"]);
double minTextLengthToBrake = 40;
Worksheet activeWorksheet = workbook.Worksheets[0];
for (int i = 0; i < activeWorksheet.Cells.Rows.Count; i++)
{
    var row = activeWorksheet.Cells.Rows[i];
    if (row == null || row.LastCell == null || row.FirstCell == null)
    {
        continue;
    }

    for (int columnI = 0; columnI < row.LastCell.Column; columnI++)
    {
        var cell = row.GetCellOrNull(columnI);

        if (cell == null
            || cell.Type == CellValueType.IsNull
            || cell.IsMerged)
        {
            continue;
        }

        if (!string.IsNullOrWhiteSpace(cell.Value.ToString()))
        {
            var style = cell.GetStyle();
            style.HorizontalAlignment = TextAlignmentType.Left;
            cell.SetStyle(style);
            cell.Value = cell.Value.ToString().Trim();
        }

        //=>  THIS IS THE SECTION FOR THE WORD WRAP
        if (cell.Value.ToString().Trim().Length >= minTextLengthToBrake)
        {
            var style = cell.GetStyle();
            style.IsTextWrapped = true;
            cell.SetStyle(style);
            AutoFitterOptions autoFitterOptions = new AutoFitterOptions()
            {
                IgnoreHidden = true,
                FormatStrategy = CellValueFormatStrategy.DisplayString,
                MaxRowHeight = 36,
                AutoFitWrappedTextType = AutoFitWrappedTextType.Paragraph
            };
            activeWorksheet.AutoFitRow(row.Index, row.FirstCell.Column, row.LastCell.Column, autoFitterOptions);
        }
    }
}

PdfSaveOptions options = new PdfSaveOptions();
options.AllColumnsInOnePagePerSheet = true;
workbook.Save(filePath + "out.pdf", options);

Can you try it again?

Hi,
I’ve upgraded my version and now I’m at 23.3.
please see that the row height for the entier pdf is changed.
the example code should not affect another rows expect what declaring in
activeWorksheet.AutoFitRow(row.Index, row.FirstCell.Column, row.LastCell.Column, autoFitterOptions);
what we should change?

also - the text position down a bit, but still seems covered.
image.png (11.1 KB)

any idea?

@zivdaniel,

Did you try the sample code (shared by @John.He) with latest version/fix, i.e., Aspose.Cells for .NET v23.3? Please try using latest version Aspose.Cells for .NET v23.3 with the sample code. Also, did you check the output PDF shared by @John.He? I checked it by opening into Adobe reader and it is fine tuned. See the screenshot (attached) to demonstrate what I can see for your highlighted issue (shared in your your screenshot), I think it is ok.
sc_shot1.png (96.6 KB)

If you still find the issue, kindly share your output PDF generated when using the sample code segment. We will check it soon.

Hi @amjad.sahi,

Yes, I’ve tried the new version 23.3 and the updated sample code @John.He provide me.

there are few regressions in this version/code.

  1. row height is changed and is not consistent with the rest of the pdf. (section table header, first row -when short or long the hight is different, when very long is also another size- and the last row of the table.
  2. the long email - is still cut in the middle from the top border.
  3. the date time format changed from 12H display to 24H. which should not happen.
    image.png (21.9 KB)

attached 3 version

  1. valid hights with word wrap issue (old version) old version with word wrap issue.pdf (53.9 KB)
  2. with version upgrade version upgraded.pdf (159.8 KB)
  3. version upgrade + updated code. version upgrade + updated code.pdf (137.9 KB)

here’s the updated code -

//double minTextLengthToBrake = Convert.ToInt32(_configuration["WorkbookDefaultStyle:MinTextLengthToBrake"]);
double minTextLengthToBrake = 40;
var activeWorksheet = workbook.Worksheets[0];
        for (int i = 0; i < activeWorksheet.Cells.Rows.Count; i++)
        {
            var row = activeWorksheet.Cells.Rows[i];
            if (row == null || row.LastCell == null || row.FirstCell == null)
            {
                continue;
            }

            for (int columnI = 0; columnI < row.LastCell.Column; columnI++)
            {
                var cell = row.GetCellOrNull(columnI);

                if (cell == null
                    || cell.Type == CellValueType.IsNull
                    || cell.IsMerged)
                {
                    continue;
                }

                if (!string.IsNullOrWhiteSpace(cell.Value.ToString()))
                {
                    var style = cell.GetStyle();
                    style.HorizontalAlignment = TextAlignmentType.Left;
                    cell.SetStyle(style);
                    cell.Value = cell.Value.ToString().Trim();
                }
                if (cell.Value.ToString().Trim().Length >= minTextLengthToBrake)
                {
                    var style = cell.GetStyle();
                    style.IsTextWrapped = true;
                    cell.SetStyle(style);
                    AutoFitterOptions autoFitterOptions = new AutoFitterOptions()
                    {
                        IgnoreHidden = true,
                        FormatStrategy = CellValueFormatStrategy.DisplayString,
                        MaxRowHeight = 36,
                        AutoFitWrappedTextType = AutoFitWrappedTextType.Paragraph
                    };
                    activeWorksheet.AutoFitRow(row.Index, row.FirstCell.Column, row.LastCell.Column, autoFitterOptions);
                }
            }
        }

save format:

  public void PrintingSetup(Worksheet activeWorksheet)
    {
        var pageSetup = activeWorksheet.PageSetup;
        pageSetup.PaperSize = PaperSizeType.PaperA4;
        pageSetup.SetFitToPages(1, 0);
    }

saving to stream

public byte[] SaveToStream(WorkbookDesigner designe)
    {
        using var stream = new MemoryStream();
        var pdfSaveOptions = GetPdfSaveOption();
        designer.Workbook.Save(stream, pdfSaveOptions);

        return stream.ToArray();

        static PdfSaveOptions GetPdfSaveOption() => new()
        {
            OptimizationType = PdfOptimizationType.MinimumSize,
            AllColumnsInOnePagePerSheet = true,
        };
    }

@zivdaniel
Could you share a sample project with a template file?

@zivdaniel,
About date time issue, please change the following code

cell.Value = cell.Value.ToString().Trim();

to:

cell.Value = cell.StringValue;

would you like to try it?

@zivdaniel

For the issue:

the long email - is still cut in the middle from the top border.

We can’t reproduce the issue on our end. As @simon.zhao required, please share us a runnable project with source input file to reproduce the issue.
Please add the following console output before and after autofit, and share us the output on your side.

Console.WriteLine($"Row[{row.Index}] height before autofit: {activeWorksheet.Cells.GetRowHeight(row.Index)}");
activeWorksheet.AutoFitRow(row.Index, row.FirstCell.Column, row.LastCell.Column, autoFitterOptions);
Console.WriteLine($"Row[{row.Index}] height after autofit: {activeWorksheet.Cells.GetRowHeight(row.Index)}");

Please note, after comparing the content of the three pdf files, it seems that the first pdf(old version) you shared is from a different input file.

If you have any other issues, please share us output files with detail screenshots to highlight the issues.