Sandeep Khati Sandeep Khati - 1 month ago 17x
ASP.NET (C#) Question

converting repeater control to pdf using itextsharp every repeated contents should come in new page

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");
StringWriter sw = new StringWriter();

HtmlTextWriter hw = new HtmlTextWriter(sw);

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);


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;
    // step 4
    XMLWorkerHelper.getInstance().parseXHtml(writer, document,
            new FileInputStream(HTML));
    // step 5

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.