I am trying to print document using the method of CHtmlEditCtrl::PrintDocument()
as described here (Printing In MFC Application). Below is my test code where I plan to print a pretty big table.
void CMyView::OnFilePrint()
{
CHtmlEditCtrl PrintCtrl;
if (!PrintCtrl.Create(NULL, WS_CHILD, CRect(0, 0, 0, 0), this, 1))
{
ASSERT(FALSE);
return;
}
CComPtr<IHTMLDocument2> document;
PrintCtrl.GetDocument(&document);
WaitForComplete(document);
CString html =
_T("<!doctype html>")
_T("<html lang=\"en\">")
_T(" <head>")
_T(" <meta charset=\"utf-8\">")
_T(" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">")
_T(" <title>HTML5 Test Page</title>")
_T(" <style>")
_T(" @media print")
_T(" {")
_T(" td { overflow-x:auto }")
_T(" }")
_T(" </style>")
_T(" <style type=\"text/css\">")
_T(" thead { display:table-header-group }")
_T(" tfoot { display:table-footer-group; page-break-inside:avoid }")
_T(" tbody { display:table-row-group }")
_T(" td > div { display:block; page-break-inside:avoid }")
_T(" </style>")
_T(" </head>")
_T(" <body>")
_T(" <div id=\"top\" class=\"page\" role=\"document\">")
_T(" <header role=\"banner\">")
_T(" <p>This is header.</p>")
_T(" </header>")
_T(" <main role=\"main\">")
_T(" <section id=\"text\">")
_T(" <article id=\"text_tables\">")
_T(" <table>")
_T(" <thead>")
_T(" <tr>")
_T(" <th>")
_T(" header comes here for each page")
_T(" </th>")
_T(" </tr>")
_T(" </thead>")
_T(" <tfoot>")
_T(" <tr>")
_T(" <td>")
_T(" footer comes here for each page")
_T(" </td>")
_T(" </tr>")
_T(" </tfoot>");
_T(" <tbody>");
for (int i = 0; i < 200; ++i)
{
CString str;
str.Format(_T("%d"), i);
html = html + "<tr><td><div>" + str + "</div></td></tr>";
}
html = html + "</tbody></table></article></section></main>";
html = html + "<footer role=\"contentinfo\"><p>This is footer</p></footer></div></body></html>";
PrintCtrl.SetDocumentHTML(html);
WaitForComplete(document);
//PrintCtrl.PrintPreview();
PrintCtrl.PrintDocument();
}
The problem is for every line near the page bottom, there seems to be a mess of spacing. See attached picture below, note that 41 has more spacing from 40 and gets partially cut off. How does it happen and how to fix it?
I try adding repeated header and footer, below is part of the result that displaying error.
I try using "td {overflow-x:auto;}". The table footer of the first page is not shown correctly.