Solved

iTextSharp

Posted on 2016-09-06
3
41 Views
Last Modified: 2016-09-06
Hi,

I am trying to use iTextSharp to generate PDF from ASP.NET. I am trying to make this to work.

http://www.aspsnippets.com/Articles/Export-ASPNet-Panel-contents-to-PDF-using-iTextSharp.aspx

I am getting
CS0246: The type or namespace name 'StringWriter' could not be found (are you missing a using directive or an assembly reference?)
Line 73:         Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
Line 74:         Response.Cache.SetCacheability(HttpCacheability.NoCache);
Line 75:         StringWriter sw = new StringWriter();
Line 76:         HtmlTextWriter hw = new HtmlTextWriter(sw);
Line 77:         pnlPerson.RenderControl(hw);

Open in new window


Can EEs give me some directions?  thanks
0
Comment
Question by:mcrmg
  • 2
3 Comments
 

Author Comment

by:mcrmg
ID: 41786245
this is my code
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="test.aspx.cs" Inherits="_test" %>



<!DOCTYPE html>  
<html  
    xmlns="http://www.w3.org/1999/xhtml">  
    <head id="Head1" runat="server">  
        <title></title>  
    </head>  
    <body>  
        
            <div >
                <asp:Panel ID="pnlPerson" runat="server">
                    <table><tr><td>this is a test</td></tr></table>        
                        
                </asp:Panel>
                
                <asp:Button ID="btnGenerate" runat="server" Text="Generate PDF" OnClick="btnGenerate_Click" /> 
                <asp:Button ID="btnExport" runat="server" Text="Export" OnClick="btnExport_Click" />
            </div>  
            
            
        
    </body>  
</html>  

Open in new window



using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

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


public partial class _test : System.Web.UI.Page
{



    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write ("hello");
    
    }






    protected void btnGenerate_Click(object sender, EventArgs e)
    {
        try
        {
            Document pdfDoc = new Document(PageSize.A4, 25, 10, 25, 10);
            PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            pdfDoc.Open();
            //Paragraph Text = new Paragraph("This is test file");
            //pdfDoc.Add(Text);
            pdfWriter.CloseStream = false;

            PdfPTable table = new PdfPTable(3);
            PdfPCell cell = new PdfPCell(new Phrase("Header spanning 3 columns"));
            cell.Colspan = 3;
            cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
            table.AddCell(cell);
            table.AddCell("Col 1 Row 1");
            table.AddCell("Col 2 Row 1");
            table.AddCell("Col 3 Row 1");
            table.AddCell("Col 1 Row 2");
            table.AddCell("Col 2 Row 2");
            table.AddCell("Col 3 Row 2");
            pdfDoc.Add(table);


            pdfDoc.Close();
            Response.Buffer = true;
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=Example.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Write(pdfDoc);
            Response.End();
        }
        catch (Exception ex)
        { Response.Write(ex.Message); }
    }




    protected void btnExport_Click(object sender, EventArgs e)
    {
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        pnlPerson.RenderControl(hw);
        StringReader sr = new StringReader(sw.ToString());
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
        htmlparser.Parse(sr);
        pdfDoc.Close();
        Response.Write(pdfDoc);
        Response.End();

    }


}

Open in new window

0
 
LVL 19

Accepted Solution

by:
Daniel Van Der Werken earned 500 total points
ID: 41786293
The error you're seeing indicates you need to add:

using System.IO;

in the Using section. Also, you can highlight the error in the code and hit CTRL-PERIOD (CTRL-.) and it will give you a recommendation for a Using, if the reference is already there for  you.
0
 

Author Closing Comment

by:mcrmg
ID: 41786298
Thank you very much
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now