Exporting HTML tables from EML body to PDF

We are using gembox to convert eml to pdf. When an eml file has an html body and html tables what is the best way to ensure the HTML table is fitted to the page and if necessary the page orientation is adjusted to the landscape.

I am doing something similar for excel attachments:

foreach (var sheet in spreadsheet.Worksheets)
{
    int columnCount = sheet.CalculateMaxUsedColumns();
    double totalWidth = 0;
    for (int i = 0; i < columnCount; i++)
    {
        var unitSize = Math.Round(sheet.Columns[i].GetWidth(LengthUnit.Inch));
        totalWidth += unitSize;
    }

    sheet.PrintOptions.LeftMargin = 0;
    sheet.PrintOptions.RightMargin = 0;
    sheet.PrintOptions.TopMargin = 0;
    sheet.PrintOptions.BottomMargin = 0;
    sheet.PrintOptions.Portrait = !(totalWidth > 8.25);
    sheet.PrintOptions.FitWorksheetWidthToPages = 1;
    sheet.PrintOptions.FitWorksheetHeightToPages = 0;
}

Tim Hayes

Hi Tim,

I presume that you’re loading the email’s body to DocumentModel, as shown in PDF Export example.

In that case, you could try increasing the page size so that the widest table can fit in it.
For example, something like this:

// Import the email's body to the document.
LoadBody(message, document);

document.GetPaginator(new PaginatorOptions() { UpdateTableWidth = true });
var section = document.Sections[0];

double maxTableWidth = 0;
foreach (TableWidth tableWidth in section.GetChildElements(true, ElementType.Table)
    .Cast<Table>()
    .Select(t => t.TableFormat.PreferredWidth)
{
    maxTableWidth = Math.Max(maxTableWidth, tableWidth.Value);
}

double pageWidth = section.PageSetup.PageWidth
    - section.PageSetup.PageMargins.Left
    - section.PageSetup.PageMargins.Right;

if (maxTableWidth > pageWidth)
    section.PageSetup.PageWidth = maxTableWidth
        + section.PageSetup.PageMargins.Left
        + section.PageSetup.PageMargins.Right;

// Save the document as PDF.
document.Save("Export.pdf");

I hope this helps.

Regards,
Mario

Mario,

I tried this code change and it seems to work however it dramatically slows down the conversion process. Is there a faster way to perform this operation?

Here is what I have implemented:

		if (section.GetChildElements(true, ElementType.Table).Any())
		{
			document.GetPaginator(new GemBox.Document.PaginatorOptions() { UpdateTableWidth = true });

			double maxTableWidth = 0;
			foreach (TableWidth tableWidth in section.GetChildElements(true, ElementType.Table)
				.Cast<Table>()
				.Select(t => t.TableFormat.PreferredWidth))
			{
				maxTableWidth = Math.Max(maxTableWidth, tableWidth.Value);
			}

			var pageWidth = section.PageSetup.PageWidth
							- section.PageSetup.PageMargins.Left
							- section.PageSetup.PageMargins.Right;

			if (maxTableWidth > pageWidth)
				section.PageSetup.PageWidth = maxTableWidth
											  + section.PageSetup.PageMargins.Left
											  + section.PageSetup.PageMargins.Right;
		}

Tim

Hi,

Can you send us your EML file so that we can investigate it?

Regards,
Mario