Free Support Forum - aspose.com

Cells Formatting Problem

Hello,
I am using the following code to convert excel to pdf:
var sourcePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, “test.xlsx”);
var targetPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, “result.pdf”);
var excel = new Workbook(sourcePath);
excel.Save(targetPath, new Aspose.Cells.PdfSaveOptions { OnePagePerSheet = true });
excel.Dispose();
However I am getting this result:
image.png (23.5 KB)
instead of:
image.png (10.1 KB)
In other words, I need to (1) expand the last column width, (2) wrap the text, and (3) increase the row height so that all the text fits within the table column.
How can I do that with your library?
Thank you.
I’ve attached the original excel document for your testing.excel.zip (8.4 KB)

@mpogorelov,
We are working on this query and will share our feedback soon.

@mpogorelov,
Thank you for your patience. Please give a try to the following sample code and share the feedback.

Workbook workbook = new Workbook("excel.xlsx");
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Cells.SetColumnWidthPixel(5, 400);

Style style = workbook.CreateStyle();
style.IsTextWrapped = true;
StyleFlag styleFlagHeader = new StyleFlag();
styleFlagHeader.WrapText = true;
worksheet.Cells.Columns[5].ApplyStyle(style, styleFlagHeader);
worksheet.AutoFitRows();

int maxRow = worksheet.Cells.MaxRow;
int maxCol = worksheet.Cells.MaxColumn;
worksheet.VerticalPageBreaks.Clear();
worksheet.VerticalPageBreaks.Add(maxRow, maxCol);
workbook.Save("excel.pdf", new PdfSaveOptions() { OnePagePerSheet = true });

It almost works.
On a different excel document, I got this output:
image.png (80.7 KB)
problem areas has red lines.
I’ve attached the document.test.zip (16.9 KB)
As you can see, there is more text in Tag column. The final solution should work for both cases when there is little text and a lot of text the column and row should adjust.
thank you

@mpogorelov,
I have tried to set the auto row height in MS Excel but it also set the same as shown in the attached image. Could you please test this behavior in MS Excel and share the feedback. Aspose.Cells is creating similar output as created by MS Excel.
Screen Shot 2020-02-13 at 7.07.33 AM.jpg (815.1 KB)

if I do these 3 things in excel: wrap text, auto fit column width and auto fit column height, it looks right:
image.png (68.0 KB)

@mpogorelov,
Could you please modify the sample code a little bit by adding worksheet.AutoFitColumns(); and share the feedback about the output PDF.

Workbook workbook = new Workbook("test 2.xlsx");
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Cells.SetColumnWidthPixel(5, 400);

Style style = workbook.CreateStyle();
style.IsTextWrapped = true;
StyleFlag styleFlagHeader = new StyleFlag();
styleFlagHeader.WrapText = true;
worksheet.Cells.Columns[5].ApplyStyle(style, styleFlagHeader);
worksheet.AutoFitColumns();//NOTE THIS LINE
worksheet.AutoFitRows();


int maxRow = worksheet.Cells.MaxRow;
int maxCol = worksheet.Cells.MaxColumn;
worksheet.VerticalPageBreaks.Clear();
worksheet.VerticalPageBreaks.Add(maxRow, maxCol);
workbook.Save("test 2.pdf", new PdfSaveOptions() { OnePagePerSheet = true });

it looks good, thank you

@mpogorelov,
Good to know that your issue is sorted out by the suggested line of code. Feel free to contact us at any time if you need further help or have some other issue or queries, we will be happy to assist you soon.