0

Here is the layout of my page that I am exporting:

-------------------------------
' Text text text               '
'                              '
' Gridview                     '
'                              '
' Text text text               '
--------------------------------

Here is my table after rendering to HTML

<table cellspacing="0" id="mytest_gvMain" style="width:100%;border-collapse:collapse;">
  <tbody>
    <tr>
      <th scope="col">Item</th>
      <th scope="col">Description</th>
      <th scope="col">Quantity</th>
      <th scope="col">Unit</th>
      <th scope="col">Rate</th>
    </tr>
    <tr>
    </tr>
  </tbody>
</table>

As you see the gridview is in the middle of the page. Because the gridview can of any size expanding to different pages, I would like to set SplitLate = false, so the row stays together. Now some of the text in the last row is on page 1 and the some of it on the first row of page 2. Please have a look at this image Some data on last row of page 5 and some on first row of page 6 . Some data on last row of page 5 and some on first row of page 6. I have read some documents suggesting document.add(table) but that will add the table to the end of my document. Is there a way I set SplitLate on the gridview itself?

Based on Chris suggestion, I have updated my code to use xmlworker based on this link Cannot get CSS to work in iTextSharp (5.4.3) when making pdf.

 Using input As New MemoryStream(bytes, False)

    Dim ms As New MemoryStream()

    Dim document As New iTextSharp.text.Document(iTextSharp.text.PageSize.LETTER, 36, 36, 36, 36)
    Dim writer As PdfWriter = PdfWriter.GetInstance(document, ms)

    writer.CloseStream = False
    document.Open()

    Dim htmlContext As HtmlPipelineContext = New HtmlPipelineContext(Nothing)
    htmlContext.SetAcceptUnknown(True)
    htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory())
    Dim cssResolver As ICSSResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(False)
    cssResolver.AddCssFile(HttpContext.Current.Server.MapPath("/assets/css/pdf.css"), True)

    Dim pipeline As New CssResolverPipeline(cssResolver, New HtmlPipeline(htmlContext, New PdfWriterPipeline(document, writer)))
    Dim pdfworker As New XMLWorker(pipeline, True)
    Dim p As New XMLParser(True, pdfworker, New System.Text.UTF8Encoding)

    Try

        'p.AddListener(pdfworker)
        'p.Parse(input, Encoding.UTF8)

        XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, input, New FileStream(HttpContext.Current.Server.MapPath("~/assets/css/pdf.css"), FileMode.Open, FileAccess.Read))
    Catch

    Finally
        pdfworker.Close()

    End Try

    document.Close()
    ms.Position = 0

    Response.Buffer = True
    Response.Clear()
    Response.ContentType = "application/pdf"
    Response.AddHeader("content-disposition", "attachment; filename=preview.pdf")
    Response.BinaryWrite(ms.GetBuffer())
    Response.Flush()
End Using

Any help appreciated.

Community
  • 1
  • 1
loveprogramming
  • 538
  • 2
  • 5
  • 22
  • You'll need to post some of your code to show us what you're actually doing. While you're at it, render your GridView to HTML and post that HTML because [iTextSharp can't process `GridViews` in the first place](http://stackoverflow.com/a/25164258/231316). – Chris Haas Sep 30 '15 at 12:58
  • Ok, now I'm a little more confused. I assumed (possibly incorrectly) that you were doing GridView to HTML to PDF. However, the link that you provided is about manually walking a GridView and creating `PdfPTable` and `PdfPCell` objects. Which one are you doing? – Chris Haas Sep 30 '15 at 20:55
  • Sorry about that. To save your confusion, I think we can skip the generating PDF as this is working. I can generate the PDF fine. I am just having issue with the layout of the table that some data in a row expanding to another row on another page. Set splitlate seems to be the solution but I can't really set it my gridview as the table is not added dynamically like in many tutorials out there using document.add(table). Thanks. – loveprogramming Sep 30 '15 at 21:00
  • The link posted earlier, the developer actually created a clone table of the gridview and then he can set splitlate=false on the clone table and add it to the end of the document. Is there a way I can set splitlate=false on the gridview itself using the ID="mytest_gvMain"? – loveprogramming Sep 30 '15 at 21:07
  • Remember, you can do anything you want with the GridView because _iText doesn't understand it_. If you are going down the path that you linked to it is 100% up to you to translate each and every aspect of the GridView over to the iText abstractions such as `PdfPTable`. So if you want to set a special attribute on the GridView then it will be completely up to you to look for that special attribute when parsing. – Chris Haas Sep 30 '15 at 21:18
  • I do not want to clone my gridview. I want to set a special attribute on the gridview, how do I do that? mytest_gvMain.splitlate=false won't work obviously. – loveprogramming Sep 30 '15 at 21:22
  • I really do apologize but I'm just very unclear on what your problem is. If you were rendering the GridView to HTML then you could setup something to watch for custom attributes on the `` or whatever. However, the code that you've linked to shows us that you are not rendering HTML but instead you are manually creating iText abstractions. Because you are manually doing things it is all up to you. Just add something to the `Attributes` collection or watch for a specific ID and then add that logic to your manual `PdfPTable` creation code.
    – Chris Haas Sep 30 '15 at 21:33
  • I have updated my question again. It's ok if you are still not clear. Thanks anyway. – loveprogramming Sep 30 '15 at 21:38
  • Here's the thing, I actually understand what your ultimate goal is (I think). The problem is that you are mixing some abstractions. A `GridView` is a logical abstraction made by Microsoft. You are _rendering_ your `GridView` to HTML which physically produces a `` tag. I'm guessing you also have a "Print" button that creates a PDF. This "Print" button _does not get the HTML rendering of the `GridView`_, it only receives the logical `GridView`. If you were going down the `HTMLWorker` or `XmlWorker` path you would be concerned about the rendered HTML but you aren't doing that.
    – Chris Haas Sep 30 '15 at 21:48
  • @ChrisHaas I have updated my question to use xmlwoker. thanks. – loveprogramming Oct 01 '15 at 21:29

0 Answers0