Solved

iTextSharp

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

679 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