2

I am using iTextSharp to convert html contents inside repeater control to pdf(contents comes from database). everything is OK but every repeated content should come in new pdf page not two repeated data in same page.. for example i am binding student registration information from database to a repeater control and i want to convert the contents to pdf but registration data of two students appears in same pdf page which should not happen. multiple records should come separately in new pages not in same page

code to convert repeater contents to pdf

    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=Registrations.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();

    HtmlTextWriter hw = new HtmlTextWriter(sw);
    this.Repeater1.RenderControl(hw);

    StringWriter sw2 = new StringWriter();
    HtmlTextWriter hw1 = new HtmlTextWriter(sw2);

    StringReader sr = new StringReader(sw.ToString().Replace("\r", "").Replace("\n\n", "").Replace("  ", ""));

    Document pdfDoc = new Document(iTextSharp.text.PageSize.A4, 40f, 40f, 100f, 95f);

     HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.NewPage();
    pdfDoc.Open();     
    htmlparser.Parse(sr);
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.End(); 
  • If you want to parse different HTML snippets so that each snippet takes a separate page, then your question is a duplicate of [iTextSharp how to use HtmlWorker to covert html to pdf with pagination](http://stackoverflow.com/questions/27814701/itextsharp-how-to-use-htmlworker-to-covert-html-to-pdf-with-pagination). **PLEASE DO NOT** use `HtmlWorker` anymore. It has been abandoned. It is no longer supported. – Bruno Lowagie Jan 08 '15 at 15:18
  • Or, if your HTML is a single file with page breaks, then you should know that page breaks are not (and will never be) supported by `HTMLWorker` because `HTMLWorker` has been abandoned in favor of XML Worker. – Bruno Lowagie Jan 08 '15 at 15:33
  • I got your point but I would like to know. is there any html tags that forces itextsharp to jump into new page.. a tag that i can place inside seperatorTemplate of a repeater control so that I can separate unique records in individual pdf page.. – Sandeep Khati Jan 09 '15 at 16:12
  • Yes, the CSS attributes `page-break-before` and `page-break-after` are supported in XML Worker (not in `HTMLWorker`). – Bruno Lowagie Jan 09 '15 at 16:16
  • I've made you an example that shows how these attributes work. – Bruno Lowagie Jan 09 '15 at 16:36

2 Answers2

1

If you want to introduce page-breaks into your HTML, you are using the wrong class. HTMLWorker has never been suited to convert HTML+CSS to PDF. Moreover, the class has been abandoned in favor of XML Worker.

Please take a look at the HtmlPageBreaks example:

public void createPdf(String file) throws IOException, DocumentException {
    // step 1
    Document document = new Document();
    // step 2
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
    // step 3
    document.open();
    // step 4
    XMLWorkerHelper.getInstance().parseXHtml(writer, document,
            new FileInputStream(HTML));
    // step 5
    document.close();
}

In this example, we parse the following HTML file: page_breaks.html

Take a close look at the opening tag of the first <table>:

<table style="border: solid 1pt; page-break-after: always" cellspacing="0">

There's a page-break-after attribute with value 'always' (the only value that is currently supported).

Now take a close look at the opening tag of the last <h1>:

<h1 style="page-break-before: always">

There's a page-break-before attribute with value 'always'.

I have made this example to demonstrate the use of these CSS attributes: they cause iText to trigger a new page as shown in the resulting PDF: page_breaks.pdf

Note that this is only supported in the more recent iText versions. You may require to purchase a commercial license if you are using iText and XML Worker outside the context of the AGPL.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
0

I had multiple html strings which I appended with "pagebreak" as a text in between like,

Dim sb As New StringBuilder()
sb.Append(htmlstring1.Text)
sb.Append("pagebreak")
sb.Append(htmlstring2.Text)
sb.Append("pagebreak")

I wanted each html string to be rendered on a new page.To identify end of a page or start of a new page I appended a "pagebreak" text at the end of the html string.

following is the code to split the html string.

Dim myString As String = sb.ToString()
Dim mySplit As String = "pagebreak"
Dim myResult() As String = myString.Split(New String() {mySplit}, StringSplitOptions.None)

We will start with the pdf creation,

Dim pdfDoc As New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F)
        Dim htmlparser As New HTMLWorker(pdfDoc)
        Using memoryStream As New MemoryStream()
            Dim writer As PdfWriter = PdfWriter.GetInstance(pdfDoc, memoryStream)
            pdfDoc.Open()
            For Each r As String In myResult
                Dim sr As New StringReader(r)
                htmlparser.Parse(sr)
                pdfDoc.NewPage()
                sr.Dispose()
            Next
            pdfDoc.Close()
            Dim bytes As Byte() = memoryStream.ToArray()
            memoryStream.Close()
            Response.Clear()
            Response.ContentType = "application/pdf"
            Response.AddHeader("Content-Disposition", "attachment;filename=Report.pdf")
            Response.Buffer = True
            Response.Cache.SetCacheability(HttpCacheability.NoCache)
            Response.BinaryWrite(bytes)
            Response.[End]()
            Response.Close()
        End Using

Hope this helps!!

Disha teli
  • 41
  • 3