Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
using System.Data;
using System.Data.SqlClient;
using System.Web.Profile;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
using System.Collections;
using System.Linq;
using System.Net;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using System.Web;
using iTextSharp.text.xml;
using System.Xml;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Windows;
using System.Messaging;

using System.ComponentModel;
protected override void Render(HtmlTextWriter writer)
    {
        MemoryStream mem = new MemoryStream();
        StreamWriter twr = new StreamWriter(mem);
        HtmlTextWriter myWriter = new HtmlTextWriter(twr);
        base.Render(myWriter);
        myWriter.Flush();
        myWriter.Dispose();
        StreamReader strmRdr = new StreamReader(mem);
        strmRdr.BaseStream.Position = 0;
        string pageContent = strmRdr.ReadToEnd();
        strmRdr.Dispose();
        mem.Dispose();
        writer.Write(pageContent);
        CreatePDFDocument(pageContent);


    }
 public void CreatePDFDocument(string strHtml)
    {
        Table tbl = new Table();

        Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
        PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("c:\\Test10.pdf", FileMode.Create));
       StringReader se = new StringReader(data.InnerText);
     
        Paragraph paragraph = new Paragraph("'" + se + "'");
        doc.Open();
        doc.Add(paragraph);
       
        doc.Close();
    }


[edit]From OP's duplicate question:

Hello ,
I just want to save a current web page as image using c# ,
and i when i create a pdf file from the current webpage it will show the html form with content that i dont want to show i just want to show same as webpage.
[/edit]
Posted
Updated 16-Jan-12 23:27pm
v3
Comments
Richard MacCutchan 17-Jan-12 5:26am    
I assume your question is what was in your other post, which I have added here and deleted your duplicate entry. Please use the "Improve Question" link to add further information.

Hi Sharad,

I didn't checked whatz the problem with your code, but you can use below code to convert html to pdf, it works fine for me and I hope it will work for you also :).

I use ITextSharp dll version 5.1.2.0

private void Converter(string htmlText, string path)
        {
            Document doc = new Document(PageSize.A4);
            try
            {
                iTextSharp.text.pdf.PdfWriter pdf = (iTextSharp.text.pdf.PdfWriter.GetInstance(doc, new FileStream(path, FileMode.Create)));
                //pdf.SetEncryption(iTextSharp.text.pdf.PdfWriter.STRENGTH128BITS, "user password", "owner password", iTextSharp.text.pdf.PdfWriter.ALLOW_COPY);
                doc.Open();

                //make an arraylist ....with STRINGREADER since its no IO reading file...
                System.Collections.Generic.List<IElement> htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(htmlText), null);
                //add the collection to the document
                for (int k = 0; k < htmlarraylist.Count; k++)
                {
                    doc.Add((IElement)htmlarraylist[k]);
                }
                // or add the collection to an paragraph 
                // if you add it to an existing non emtpy paragraph it will insert it from
                //the point youwrite -
                //Paragraph mypara = new Paragraph();//make an emtphy paragraph as "holder"
                //mypara.IndentationLeft = 36;
                //mypara.InsertRange(0, htmlarraylist);
                //doc.Add(mypara);
                doc.Close();
            }
            catch (Exception ex)
            {
                //Log error
                try
                {
                    doc.Close();
                }
                catch (Exception e)
                {
                    //log document closing error
                }
            }
        }
 
Share this answer
 
Comments
sharad_sharma82 18-Jan-12 10:27am    
Hello ravi is it also show the Images as well as the border of the table and css applied , i have done this same right now but not able to show the css and table borders
Thanks
well in my case it was simple inline css and this piece of code worked perfectly fine their. For having a separate style sheet file, I think you need to use some overload of "ParseToList" method, one of them takes a stylesheet parameter.

I have passed null for that as i haven't used an external css.

For image thing, this code works fine till it gets absolute paths(URLs or absolute directory file path). It starts crying when given relative path, so I'll suggest you to correct image src values in html before passing it in converter method.

Hope this helps you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900