Split large table in the multiple pages

2.4k views Asked by At

Input: two dimension matrix (20000x20000)
Output: pdf document with a table, contains matrix data

How can I split table horizontally and vertically in a few pages without scaling a font-size? Right now I can generate only one page document. Winnovative library just scales a font-size. I use HtmlToPdfElement object for now but I could use any other technique, because my input data is a matrix.

Func<string> generateRow = () =>
{
    var result = new StringBuilder(10000000);
    for (var i = 0; i < 100; i++)
    {
        result.Append("<tr>");
        for (var j = 0; j < 100; j++)
        {
            result.Append("<td>" + i + "," + j + "</td>");
        }
        result.Append("</tr>");
    }
    return result.ToString();
};

var htmlStr = "<html>" +
              "<head><style>td { border: 1px solid black; padding: 2px; } table { border-collapse: collapse; }</style></head>" +
              "<body><table style='border-width: 1px; border-color: black; border-style: solid;'>" +
              generateRow() +
              "</table></body></html>";

var document = new Document();
var page1 = document.Pages.AddNewPage(PageSize.A4, new Margins(10, 10, 10, 10));

var htmlToPdfElem = new HtmlToPdfElement(0, 0, PageSize.A4.Width - 20, PageSize.A4.Height - 20, htmlStr, null, -1, -1);
page1.AddElement(htmlToPdfElem);
document.Save("test.pdf");
1

There are 1 answers

1
mikalai On BEST ANSWER

I suppose there is no way to split horizontally - that could lead to undefined behaviour. Actually, the copmponent tries to render your html in a "virtual" browser. And browsers are kind of pages-unaware things.

So, in your case I'd suggest reviewing the width of the content you generate. For example, determine which columns slice should go to a current page.

string generateRow(int pageNo, int colunmsPerPage) =>
{  
    var i = colunmsPerPage;
    while(i-- > 0)
       //add single row with specified columns rage
       stringData += data[currentRow][colunmsPerPage * pageNo + i];
   return stringData;
};

Also you could specify large page size and rely on pdf reader's printing abilities.

Vertical split is done automatically - actually you disabled it when had specified desired height. If you pass "auto", content would be split by several pages automatically:

var htmlToPdfElem = new HtmlToPdfElement(0, 0, PageSize.A4.Width - 20, -1 /*auto*/, htmlStr, null);