0

I have a user control which has a panel used to display the information which I print when needed. I have a grid view in which I get search result It has a link button clicking on which I want to open a pdf of that panel in the above one.

1
  • Showing the relevant code would help. Did you miss that last statement? Commented Jul 19, 2011 at 5:15

2 Answers 2

2

I have implemented this, and I hope someone else can do it as well. I have listed my code below.

    using iTextSharp.text;
    using iTextSharp.text.pdf;
    using iTextSharp.text.html;
    using iTextSharp.text.html.simpleparser;

Import the above DLL

string attachment = "attachment; filename=" + "File Name" + ".pdf";
    Response.ClearContent();
    Response.AddHeader("content-disposition", attachment);
    Response.ContentType = "application/pdf";
    StringWriter stw = new StringWriter();
    HtmlTextWriter htextw = new HtmlTextWriter(stw);
    htextw.AddStyleAttribute("font-size", "7pt");
    htextw.AddStyleAttribute("color", "Black");
    Page pg = new Page();
    HtmlForm frm = new HtmlForm();
    pg.EnableEventValidation = false;

    pg.RenderControl(htextw);
    Document document = new Document();
    document = new Document(PageSize.A4, 5, 5, 15, 5);
    FontFactory.GetFont("Arial", 50, iTextSharp.text.BaseColor.BLUE);
    PdfWriter.GetInstance(document, Response.OutputStream);
    document.Open();
    //This is how you can add text in div in a pdf
    Chunk c = new Chunk(TextHere + "\n", FontFactory.GetFont("Verdana", 15));
    Paragraph p = new Paragraph();
    p.Alignment = Element.ALIGN_LEFT;
    p.Add(c); p.Add(c1);
    // This is how you can generate table and can set proprties
    PdfPTable table = new PdfPTable(2);
    table.WidthPercentage = 100;
    //Bill No and Bill Date
    PdfPCell pdfcell1 = new PdfPCell(new Phrase("Text in TAble" + "TExt From Database"));
    pdfcell1.Border = iTextSharp.text.Rectangle.BOTTOM_BORDER | iTextSharp.text.Rectangle.TOP_BORDER;
    table.AddCell(pdfcell1);
    PdfPCell pdfcell = new PdfPCell(new Phrase("Text in TAble" + "TExt From Database"));
    pdfcell.Border = iTextSharp.text.Rectangle.BOTTOM_BORDER | iTextSharp.text.Rectangle.TOP_BORDER;
    pdfcell.HorizontalAlignment = Element.ALIGN_RIGHT;
    table.AddCell(pdfcell);

    document.Add(p);
    document.Add(table);
    document.Add(tablegrid);
    StringReader str = new StringReader(stw.ToString());
    HTMLWorker htmlworker = new HTMLWorker(document);
    htmlworker.Parse(str);

    document.Close();
    Response.Write(document);
Sign up to request clarification or add additional context in comments.

Comments

0

I think you might run into some trouble on this one. You can't do this all client-side, since JavaScript can not manipulate files (by design). So your options are:

  1. Full postback
  2. Ajax call

You could try an Ajax call to a custom handler (.ashx) file whick returns a response with the PDF file. I havn't tried this before, but it might work. How about opening another page with target="_blank" and it will run the server-side code and generate the PDF, while the original page remains where it was?

7 Comments

I can do this using server side code as well, can you help me with that
I think your best bet is to use one of the many compenents that do this already. Some are better than others and some are even free. Just have a quick search on Google and you'll find a handfull.
U didnt get my question my friend please read the question and then provide solution not just writting anything do this do that
I think you my friend don't understand the point of this Q&A site. No one here is going to write your code for you! I am pointing you in the right direction. If you have a specific problem, I'll be happy to help.
did u really get my problem, I want to show a user control in pdf dynamically. that control is not visible yet
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.