开发者

iTextSharp: table row gets pushed to new page if it doesn't fit on the current one

开发者 https://www.devze.com 2023-03-15 02:58 出处:网络
I\'m using iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(TextReader reader, StyleSheet style) to convert an html table into a pdf doc. Some rows contain a lot of data, and may not fit on th

I'm using iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(TextReader reader, StyleSheet style) to convert an html table into a pdf doc. Some rows contain a lot of data, and may not fit on the current page, so iTextSharp creates a ne开发者_Python百科w page and places the row there. If the row doesn't fit on the next page, it splits it correctly.

Is there a way to tell it to not use these page breaks? Here is what it looks like:

iTextSharp: table row gets pushed to new page if it doesn't fit on the current one


The trick that worked for me is to inspect the results of ParseToList() and look for any elements that are of type PdfPTable. If you see one set its SplitLate property to False. Here's some VB that you should be able to convert to C# fairly easily:

Dim Elements = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(SR, Nothing)
For Each El In Elements
    If TypeOf El Is PdfPTable Then
        DirectCast(El, PdfPTable).SplitLate = False
    End If
    Doc.Add(El)
Next


Chris Haas answer is great - it worked for me. Here is one in C#:

        string template = File.ReadAllText(@"C:\my_template.html");
        var htmlText = Engine.Razor.RunCompile(template, Guid.NewGuid().ToString(), model: GetViewModel());

        TextReader reader = new StringReader(htmlText);
        var document = new Document(PageSize.A4, 30, 30, 30, 30);

        using (var stream = new MemoryStream())
        {
            PdfWriter.GetInstance(document, stream);

            document.Open();
            var pages = HTMLWorker.ParseToList(reader, new StyleSheet());
            foreach (var page in pages)
            {
                if (page is PdfPTable)
                {
                    (page as PdfPTable).SplitLate = false;
                }
                document.Add(page as IElement);
            }

            document.Close();

            File.WriteAllBytes(@"C:\my_template.pdf", stream.ToArray());
        }


I'm pretty sure table rows are "atomic", and will be CROPPED if they overflow a given page.

Does the PDF from the above image continue that row on the next page?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号