PDF convert RRS feed

  • Question

  • User407600577 posted

    string attachment = "attachment; filename=abc.pdf";
                Response.AddHeader("content-disposition", attachment);
                Response.ContentType = "application/pdf";
                StringWriter stw = new StringWriter();
                HtmlTextWriter htextw = new HtmlTextWriter(stw);
                Document document = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
                PdfWriter.GetInstance(document, Response.OutputStream);


    i m using above code to generate PDF for my report ....

    i have problem in formating the PDF

    in my report thr r 3 HTML Tables and 1 repeater control for all these i m binding a data in report format as per requirement now i want the report in same format....

    for e.g. every page contain a 1 table data so my PDF is also 4 page PDF


    just help me for this...........

    waiting for  ur reply...........





    Monday, September 27, 2010 7:08 AM

All replies

  • User113288491 posted


    You can take help from these sites:



    :) If Help Make as Answer.

    Monday, September 27, 2010 9:10 AM
  • User1520641890 posted

    parse the entire string/HTML content from the three HTML tables and the repeater, something like this:

    <%@ WebHandler Language="C#" Class="checkTable" %>
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Web;
    using iTextSharp.text;
    using iTextSharp.text.html.simpleparser;
    using iTextSharp.text.pdf;
    public class checkTable : IHttpHandler {
      public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "application/pdf";
        string html = @"
    <!-- START 3 HTML TABLES, three separate pages -->    
    <table border='1'>
    <tr><td>PAGE 1</td></tr>
    <table border='1'>
    <tr><td>PAGE 2</td></tr>
    <table border='1'>
    <tr><td>PAGE 3</td></tr>
    <!-- START REPEATER, not sure what kind of HTML you have... -->    
    <table border='1'>
    <tr><td>PAGE 4</td></tr>
    a paragraph
        Document document = new Document();
        PdfWriter.GetInstance(document, context.Response.OutputStream);
    /* iterate over all elements */
        List<IElement> objects = HTMLWorker.ParseToList(
          new StringReader(html), null
    /* get PdfPTable objects => HTML <table> */
        int pageNo = 0;
        foreach (IElement element in objects) {
          PdfPTable tbl = element as PdfPTable;
          if (tbl != null) {
          if (pageNo <= 3) document.NewPage();
      public bool IsReusable {
        get { return false; }

    note the 'html' variable above contains a single HTML string. not sure if the three tables you're referring to above are raw HTML, but for the repeater you need to do something like the following.  not tested, but i'm sure you can figure out how to dump everything into one string.

    // string for repeater contents
    StringBuilder html = new StringBuilder();
    StringWriter stringWriter = new StringWriter(html);
    HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);      
     * do whatever...including html.Insert() OR html.Append()
     * to get a string to use in place of 'html' variable
     * in previous code snippet

    Monday, September 27, 2010 10:21 AM