locked
PDF convert RRS feed

  • Question

  • User407600577 posted

    string attachment = "attachment; filename=abc.pdf";
                Response.ClearContent();
                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);
                document.Open();

     

    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


    Hi,

    You can take help from these sites:

    http://www.mikesdotnetting.com/Article/86/iTextSharp-Introducing-Tables

    http://www.mikesdotnetting.com/Article/82/iTextSharp-Adding-Text-with-Chunks-Phrases-and-Paragraphs

    :) 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>
    <table border='1'>
    <tr><td>PAGE 2</td></tr>
    </table>
    <table border='1'>
    <tr><td>PAGE 3</td></tr>
    </table>
    <!-- START REPEATER, not sure what kind of HTML you have... -->    
    <table border='1'>
    <tr><td>PAGE 4</td></tr>
    </table>
    <p>
    a paragraph
    </p>
        ";
        Document document = new Document();
        PdfWriter.GetInstance(document, context.Response.OutputStream);
        document.Open();
    /* 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) {
            ++pageNo;
          }
          document.Add(element); 
          if (pageNo <= 3) document.NewPage();
        } 
        document.Close();  
      }
      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