Solved

iTextSharp

Posted on 2016-09-06
3
52 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 20

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

895 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