Solved

iTextSharp

Posted on 2016-09-06
3
102 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: 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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

624 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