@DevD2020,
We have produced a PDF (20.5.pdf (55.9 KB)) document by using the following code on our end:
private static void ConvertExcelToHtml()
{
DocumentBuilder builder = new DocumentBuilder();
builder.PageSetup.Orientation = Aspose.Words.Orientation.Landscape;
Workbook excelWorkbook = new Workbook(@"E:\Temp\PDF_Format_Issues\Excel_Error.xlsx");
// force margins and orientation to the excel workbook
Aspose.Cells.PageSetup ps = null;
bool visibleSheet = false;
var lastSheetIndex = excelWorkbook.Worksheets.Where(x => x.IsVisible).LastOrDefault()?.Index ?? 0;
// set the options so that we export the active worksheet one at a time, and only the print area
Aspose.Cells.HtmlSaveOptions opts = new Aspose.Cells.HtmlSaveOptions(Aspose.Cells.SaveFormat.Html);
opts.ExportActiveWorksheetOnly = true;
opts.ExportPrintAreaOnly = true;
var printOptions = new ImageOrPrintOptions();
var wsMax = excelWorkbook.Worksheets.Count();
for (int wsIdx = 0; wsIdx < wsMax; wsIdx++)
{
var ws = excelWorkbook.Worksheets[wsIdx];
//Set auto fit columns in existed worksheet which solves PDF formatting issues for not recognizing column width
AutoFitterOptions options = new AutoFitterOptions();
options.AutoFitMergedCells = true;
ws.AutoFitColumns(options);
ws.AutoFitRows();
// set this sheet as active so that it is the one exported
excelWorkbook.Worksheets.ActiveSheetIndex = ws.Index;
if (ws.IsVisible)
{
visibleSheet = true;
ps = ws.PageSetup;
var pageBreaks = ws.GetPrintingPageBreaks(printOptions);
int p = 0;
foreach (var pb in pageBreaks)
{
// Aspose will not export columns/rows that are that do not have data and are the last column/row.
var endCol = pb.EndColumn <= ws.Cells.MaxDataColumn ? pb.EndColumn : ws.Cells.MaxDataColumn;
var endRow = pb.EndRow <= ws.Cells.MaxDataRow ? pb.EndRow : ws.Cells.MaxDataRow;
// stops horizontally positioned empty pages from attempting to render
if (pb.StartColumn <= endCol)
{
// set the print area before getting the html
ps.PrintArea = string.Format("{0}:{1}", CellsHelper.CellIndexToName(pb.StartRow, pb.StartColumn), CellsHelper.CellIndexToName(endRow, endCol));
using (var stream = new MemoryStream())
{
// insert the excel sheet html into the word document
excelWorkbook.Save(stream, opts);
builder.InsertHtml(System.Text.Encoding.UTF8.GetString(stream.ToArray()));
File.WriteAllText(@"E:\Temp\PDF_Format_Issues\html_" + p + ".html", System.Text.Encoding.UTF8.GetString(stream.ToArray()));
// insert a page break between each image, but not after the last one
if (!(ws.Index == lastSheetIndex && p == pageBreaks.Length - 1))
{
builder.InsertBreak(BreakType.PageBreak);
}
}
}
p++;
}
}
}
builder.Document.Save(@"E:\Temp\PDF_Format_Issues\20.5.docx");
builder.Document.Save(@"E:\Temp\PDF_Format_Issues\20.5.pdf");
}
We can see that Table column cells have incorrect widths in rendered PDF. For the sake of correction, we have logged this problem in our issue tracking system. The ID of this issue is WORDSNET-20379. We will further look into the details of this problem and will keep you updated on the status of correction. We apologize for your inconvenience.